2015-10-06 146 views
-5

我正在学习Android Studio中的Android开发。 我创建了一个应用程序,其中显示了一些文字的一些按钮上的文字。 但它显示“不幸的应用程序已停止” 另外,我只有一个活动。因此,它不可能,我忘了展示其在AndroidManifest.xml 这里是我的Java代码:执行时出现“不幸的是,应用程序已停止”

package com.example.mahe.pro2; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 
import android.widget.Button; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 
import android.support.v4.view.GestureDetectorCompat; 

public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener { 

private TextView tv = (TextView) findViewById(R.id.t); 
private GestureDetectorCompat gd; 
private Button bt = (Button) findViewById(R.id.b); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    this.gd = new GestureDetectorCompat(this,this); 

    bt.setOnClickListener(
      new Button.OnClickListener() { 
       public void onClick(View v) { 
        tv.setText("That was a Click!"); 
       } 
      } 
    ); 
} 

@Override 
public void onLongPress(MotionEvent motionEvent) { 
} 

@Override 
public boolean onSingleTapUp(MotionEvent motionEvent) { 
    return false; 
} 

@Override 
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) { 
    return false; 
} 

@Override 
public void onShowPress(MotionEvent motionEvent) { 

} 

@Override 
public boolean onDown(MotionEvent motionEvent) { 
    return false; 
} 

@Override 
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) { 
    tv.setText("That was a Swipe, Baby"); 
    return true; 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    this.gd.onTouchEvent(event); 
    return super.onTouchEvent(event); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 


    return super.onOptionsItemSelected(item); 
} 

}

这里是我的XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="YO!" 
    android:id="@+id/t" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="62dp" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Click" 
    android:id="@+id/b" 
    android:layout_below="@+id/t" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="164dp" /> 

+2

显示您的logcat错误 – Sunny

回答

2

只要移动

TextView tv = (TextView) findViewById(R.id.t); 
Button bt = (Button) findViewById(R.id.b); 

onCreate(...)下,经过setContentView(R.layout.activity_main);

+2

你是完美的 –

1

为@MD说 你必须首先创建你的看法和对的setView可以得到R.id.b

但是如果你想要将它们声明为全局变量,使用

private TextView tv; 
private Button bt 

上述onCreate方法,然后在onCreate方法和下

的setContentView(R.layout.activity_main);

使用

tv = (TextView) findViewById(R.id.t); 
bt = (Button) findViewById(R.id.b); 
相关问题