私はプログラミングを始めたばかりで、プログラムの何が問題なのかを突き止めるために 2 日間を費やしました。これを修正しようとして多くのことを学びましたが、どこにも行きません。
Eclipse で logcat のエラーをコピーする方法がわからなかったので、エラーを入力します。
Caused by: java.lang.NullPointerException at com.deweyx.MainActivity.OnCreate(MainActivity.java:27)
これが私のコードです:
public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.xDisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
display.setText("Your total is " + counter);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
これは xml フォルダーです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/display"
android:layout_gravity="center"
android:textSize="40sp"
android:id="@+id/xDisplay"
/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/padd"
android:layout_gravity="center"
android:textSize="20sp"
android:id="@+id/bAdd"
/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/psub"
android:layout_gravity="center"
android:textSize="20sp"
android:id="@+id/bSub"
/>
</LinearLayout>
ログキャット:
12-19 02:11:36.423: D/AndroidRuntime(531): Shutting down VM
12-19 02:11:36.423: W/dalvikvm(531): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
12-19 02:11:36.444: E/AndroidRuntime(531): FATAL EXCEPTION: main
12-19 02:11:36.444: E/AndroidRuntime(531): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.deweyx/com.deweyx.MainActivity}: java.lang.NullPointerException
12-19 02:11:36.444: E/AndroidRuntime(531): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-19 02:11:36.444: E/AndroidRuntime(531): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-19 02:11:36.444: E/AndroidRuntime(531): at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-19 02:11:36.444: E/AndroidRuntime(531): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-19 02:11:36.444: E/AndroidRuntime(531): at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 02:11:36.444: E/AndroidRuntime(531): at android.os.Looper.loop(Looper.java:137)
12-19 02:11:36.444: E/AndroidRuntime(531): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-19 02:11:36.444: E/AndroidRuntime(531): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 02:11:36.444: E/AndroidRuntime(531): at java.lang.reflect.Method.invoke(Method.java:511)
12-19 02:11:36.444: E/AndroidRuntime(531): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-19 02:11:36.444: E/AndroidRuntime(531): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-19 02:11:36.444: E/AndroidRuntime(531): at dalvik.system.NativeStart.main(Native Method)
12-19 02:11:36.444: E/AndroidRuntime(531): Caused by: java.lang.NullPointerException
12-19 02:11:36.444: E/AndroidRuntime(531): at com.deweyx.MainActivity.onCreate(MainActivity.java:27)
12-19 02:11:36.444: E/AndroidRuntime(531): at android.app.Activity.performCreate(Activity.java:4465)
12-19 02:11:36.444: E/AndroidRuntime(531): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-19 02:11:36.444: E/AndroidRuntime(531): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-19 02:11:36.444: E/AndroidRuntime(531): ... 11 more
12-19 02:11:36.653: I/dalvikvm(531): threadid=3: reacting to signal 3
12-19 02:11:36.673: I/dalvikvm(531): Wrote stack traces to '/data/anr/traces.txt'
12-19 02:11:36.983: I/dalvikvm(531): threadid=3: reacting to signal 3
12-19 02:11:36.993: I/dalvikvm(531): Wrote stack traces to '/data/anr/traces.txt'
12-19 02:11:52.452: I/dalvikvm(551): threadid=3: reacting to signal 3
12-19 02:11:52.502: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
12-19 02:11:52.783: D/AndroidRuntime(551): Shutting down VM
12-19 02:11:52.783: W/dalvikvm(551): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
12-19 02:11:52.793: E/AndroidRuntime(551): FATAL EXCEPTION: main
12-19 02:11:52.793: E/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.deweyx/com.deweyx.MainActivity}: java.lang.NullPointerException
12-19 02:11:52.793: E/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-19 02:11:52.793: E/AndroidRuntime(551): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-19 02:11:52.793: E/AndroidRuntime(551): at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-19 02:11:52.793: E/AndroidRuntime(551): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-19 02:11:52.793: E/AndroidRuntime(551): at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 02:11:52.793: E/AndroidRuntime(551): at android.os.Looper.loop(Looper.java:137)
12-19 02:11:52.793: E/AndroidRuntime(551): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-19 02:11:52.793: E/AndroidRuntime(551): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 02:11:52.793: E/AndroidRuntime(551): at java.lang.reflect.Method.invoke(Method.java:511)
12-19 02:11:52.793: E/AndroidRuntime(551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-19 02:11:52.793: E/AndroidRuntime(551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-19 02:11:52.793: E/AndroidRuntime(551): at dalvik.system.NativeStart.main(Native Method)
12-19 02:11:52.793: E/AndroidRuntime(551): Caused by: java.lang.NullPointerException
12-19 02:11:52.793: E/AndroidRuntime(551): at com.deweyx.MainActivity.onCreate(MainActivity.java:27)
12-19 02:11:52.793: E/AndroidRuntime(551): at android.app.Activity.performCreate(Activity.java:4465)
12-19 02:11:52.793: E/AndroidRuntime(551): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-19 02:11:52.793: E/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-19 02:11:52.793: E/AndroidRuntime(551): ... 11 more
12-19 02:11:52.963: I/dalvikvm(551): threadid=3: reacting to signal 3
12-19 02:11:52.963: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
12-19 02:11:53.343: I/dalvikvm(551): threadid=3: reacting to signal 3
12-19 02:11:53.353: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'