我有一些关于模拟器或eclipse的问题。我不知道哪个有问题。当我运行具有button
,textview
的应用程序时,它运行没有问题。但是当我添加click
- 方法时,我遇到了一个问题
应用程序意外停止。请再试一次
我有什么问题?
03-23 13:20:06.436: E/AndroidRuntime(604): FATAL EXCEPTION: main
03-23 13:20:06.436: E/AndroidRuntime(604): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.x/com.example.x.MainActivity}: java.lang.NullPointerException
03-23 13:20:06.436: E/AndroidRuntime(604): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-23 13:20:06.436: E/AndroidRuntime(604): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-23 13:20:06.436: E/AndroidRuntime(604): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-23 13:20:06.436: E/AndroidRuntime(604): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-23 13:20:06.436: E/AndroidRuntime(604): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 13:20:06.436: E/AndroidRuntime(604): at android.os.Looper.loop(Looper.java:123)
03-23 13:20:06.436: E/AndroidRuntime(604): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-23 13:20:06.436: E/AndroidRuntime(604): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 13:20:06.436: E/AndroidRuntime(604): at java.lang.reflect.Method.invoke(Method.java:521)
03-23 13:20:06.436: E/AndroidRuntime(604): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-23 13:20:06.436: E/AndroidRuntime(604): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-23 13:20:06.436: E/AndroidRuntime(604): at dalvik.system.NativeStart.main(Native Method)
03-23 13:20:06.436: E/AndroidRuntime(604): Caused by: java.lang.NullPointerException
03-23 13:20:06.436: E/AndroidRuntime(604): at com.example.x.MainActivity.onCreate(MainActivity.java:33)
03-23 13:20:06.436: E/AndroidRuntime(604): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-23 13:20:06.436: E/AndroidRuntime(604): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-23 13:20:06.436: E/AndroidRuntime(604):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
txt=(TextView)findViewById(R.id.textView1);
edt=(EditText)findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
txt.setText(edt.getText());
}
});
}
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.character.CharacterActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="16dp"
android:text="Button" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="21dp"
android:text="TextView" />
</RelativeLayout>