即使我使用其他类(如编辑文本),我也遇到此问题。
我确信id。
这是代码:
public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bshow = (Button)findViewById(R.id.show_msg);
bshow.setText("Showing");
}
}
这是fragment_main.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: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.exampleknight283.e003.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/show_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="44dp"
android:text="@string/showv" />
</RelativeLayout>
我在退出时得到了这个
07-16 11:27:32.155: E/Trace(1382): error opening trace file: No such file or directory (2)
07-16 11:27:32.625: D/AndroidRuntime(1382): Shutting down VM
07-16 11:27:32.625: W/dalvikvm(1382): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-16 11:27:32.645: E/AndroidRuntime(1382): FATAL EXCEPTION: main
07-16 11:27:32.645: E/AndroidRuntime(1382): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exampleknight283.e003/com.exampleknight283.e003.MainActivity}: java.lang.NullPointerException
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.os.Looper.loop(Looper.java:137)
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-16 11:27:32.645: E/AndroidRuntime(1382): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 11:27:32.645: E/AndroidRuntime(1382): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 11:27:32.645: E/AndroidRuntime(1382): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-16 11:27:32.645: E/AndroidRuntime(1382): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-16 11:27:32.645: E/AndroidRuntime(1382): at dalvik.system.NativeStart.main(Native Method)
07-16 11:27:32.645: E/AndroidRuntime(1382): Caused by: java.lang.NullPointerException
07-16 11:27:32.645: E/AndroidRuntime(1382): at com.exampleknight283.e003.MainActivity.onCreate(MainActivity.java:29)
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.Activity.performCreate(Activity.java:5104)
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
我是android dev的新手。但我在java方面很好,现在我遵循一些教程,我做得非常好,但是我有错误,就像我说的那样,我和其他类有同样的问题,比如&#34;编辑班&#34;
答案 0 :(得分:0)
尝试:
public class MainActivity extends ActionBarActivity
{
Button bshow;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bshow = (Button) findViewById(R.id.show_msg);
bshow.setText("Showing");
}
}