我在一个项目中做了一个简单的Activity,上面只有一个按钮,然后我在main.xml添加了id是mybutton,在R.java中也可以看见mybutton,一下为部分代码截图:package com.zhou;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class FirstActivity extends Activity {
/** Called when the activity is first created. */
private Button myButton;
// private TextView myTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button) findViewById(R.id.mybutton);
myButton.setText("Enter SecondActivity.");
myButton.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
startActivity(intent);
}
}
}
然后再setText那里一直报错,是java.lang.NullPointerException,求解释为什么会这样?我该添加的都应该添加了,之前出现过这种情况,我把main.xml删掉之后重写了一边就可以了,现在我重写也不行,求助啊!!!!!
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class FirstActivity extends Activity {
/** Called when the activity is first created. */
private Button myButton;
// private TextView myTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button) findViewById(R.id.mybutton);
myButton.setText("Enter SecondActivity.");
myButton.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
startActivity(intent);
}
}
}
然后再setText那里一直报错,是java.lang.NullPointerException,求解释为什么会这样?我该添加的都应该添加了,之前出现过这种情况,我把main.xml删掉之后重写了一边就可以了,现在我重写也不行,求助啊!!!!!
setContentView(R.layout.main);你可能加载的这个main activity不是你你放mybutton的那个activity
最好把你的Layout的xml传上来看看
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:name="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:name="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:name="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
2、发main.xml文件上来。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:name="@+id/mybutton"
android:text="Enter Second Activity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>08-25 02:37:40.723: WARN/dalvikvm(582): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): FATAL EXCEPTION: main
08-25 02:37:40.753: ERROR/AndroidRuntime(582): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zhou/com.zhou.FirstActivity}: java.lang.NullPointerException
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at android.os.Looper.loop(Looper.java:123)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at java.lang.reflect.Method.invoke(Method.java:521)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at dalvik.system.NativeStart.main(Native Method)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): Caused by: java.lang.NullPointerException
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at com.zhou.FirstActivity.onCreate(FirstActivity.java:24)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-25 02:37:40.753: ERROR/AndroidRuntime(582): ... 11 more
myButton.setText("Enter SecondActivity.");
Log.i("Test", "按钮测试B");然后重新编译运行,检查输出结果。
android:name="@+id/mybutton"
android:text="Enter Second Activity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
修改为:
<Button
android:id="@+id/mybutton"
android:text="Enter Second Activity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
请给100分我吧