我是Android開發新手,正在努力製作一個簡單的計算器。但是當我運行該應用程序時,它說它停止工作。這裏是Java代碼和XML代碼。我不明白爲什麼這段代碼不起作用(空指針異常)
TextView txtView1 = (TextView)findViewById(R.id.textView1);
Button Button01;
Button Button02;
Button Button03;
Button Button04;
Button Button05;
Button Button06;
Button Button07;
Button Button08;
public void onClickButton (View view)
{
if (view == findViewById(R.id.Button01)){
txtView1.setText("7");
} else if (view == findViewById(R.id.Button02)){
txtView1.setText("8");
} else if (view == findViewById(R.id.Button03)){
txtView1.setText("9");
} else if (view == findViewById(R.id.Button04)){
txtView1.setText("/");
} else if (view == findViewById(R.id.Button05)){
txtView1.setText("4");
} else if (view == findViewById(R.id.Button06)){
txtView1.setText("5");
} else if (view == findViewById(R.id.Button07)){
txtView1.setText("6");
} else if (view == findViewById(R.id.Button08)){
txtView1.setText("+");
} else if (view == findViewById(R.id.Button09)){
txtView1.setText("1");
} else if (view == findViewById(R.id.Button10)){
txtView1.setText("2");
} else if (view == findViewById(R.id.Button11)){
txtView1.setText("3");
} else if (view == findViewById(R.id.Button12)){
txtView1.setText("-");
}
}
和XML:
<LinearLayout 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"
tools:context=".MainActivity"
android:baselineAligned="false"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.5"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="onClickButton" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="onClickButton" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="onClickButton" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:onClick="onClickButton" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:onClick="onClickButton" />
<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:onClick="onClickButton" />
<Button
android:id="@+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:onClick="onClickButton" />
<Button
android:id="@+id/Button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:onClick="onClickButton" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="@+id/Button05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:onClick="onClickButton" />
<Button
android:id="@+id/Button06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:onClick="onClickButton" />
<Button
android:id="@+id/Button07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:onClick="onClickButton" />
<Button
android:id="@+id/Button08"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:onClick="onClickButton" />
</LinearLayout>
</LinearLayout>
當我運行它,我看到一個LogCat空指針異常。但是我經歷了所有的變量,並確保它們都在那裏並且正確引用。
的logcat的:
12-08 12:12:20.255: D/AndroidRuntime(5954): Shutting down VM
12-08 12:12:20.255: W/dalvikvm(5954): threadid=1: thread exiting with uncaught exception (group=0x41517b90)
12-08 12:12:20.265: I/Process(5954): Sending signal. PID: 5954 SIG: 9
12-08 12:12:20.265: E/AndroidRuntime(5954): FATAL EXCEPTION: main
12-08 12:12:20.265: E/AndroidRuntime(5954): Process: com.example.mysecondapp, PID: 5954
12-08 12:12:20.265: E/AndroidRuntime(5954): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mysecondapp/com.example.mysecondapp.MainActivity}: java.lang.NullPointerException
12-08 12:12:20.265: E/AndroidRuntime(5954): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
12-08 12:12:20.265: E/AndroidRuntime(5954): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-08 12:12:20.265: E/AndroidRuntime(5954): at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-08 12:12:20.265: E/AndroidRuntime(5954): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-08 12:12:20.265: E/AndroidRuntime(5954): at android.os.Handler.dispatchMessage(Handler.java:102)
12-08 12:12:20.265: E/AndroidRuntime(5954): at android.os.Looper.loop(Looper.java:137)
12-08 12:12:20.265: E/AndroidRuntime(5954): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-08 12:12:20.265: E/AndroidRuntime(5954): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 12:12:20.265: E/AndroidRuntime(5954): at java.lang.reflect.Method.invoke(Method.java:515)
12-08 12:12:20.265: E/AndroidRuntime(5954): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-08 12:12:20.265: E/AndroidRuntime(5954): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-08 12:12:20.265: E/AndroidRuntime(5954): at dalvik.system.NativeStart.main(Native Method)
12-08 12:12:20.265: E/AndroidRuntime(5954): Caused by: java.lang.NullPointerException
12-08 12:12:20.265: E/AndroidRuntime(5954): at android.app.Activity.findViewById(Activity.java:1883)
12-08 12:12:20.265: E/AndroidRuntime(5954): at com.example.mysecondapp.MainActivity.<init>(MainActivity.java:13)
12-08 12:12:20.265: E/AndroidRuntime(5954): at java.lang.Class.newInstanceImpl(Native Method)
12-08 12:12:20.265: E/AndroidRuntime(5954): at java.lang.Class.newInstance(Class.java:1208)
12-08 12:12:20.265: E/AndroidRuntime(5954): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-08 12:12:20.265: E/AndroidRuntime(5954): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
12-08 12:12:20.265: E/AndroidRuntime(5954): ... 11 more
發佈logcat ..什麼是調用onClick?.. – user2511882
http://stackoverflow.com/questions/3264610/findviewbyid-returns-null –
你使用'findViewById(R.id.textView1)'裏面你的onCreate,你設置你的XML文件爲contentView或之前? – bbuecherl