javaのレイアウトからテキストビューにアクセスしようとしています。それにアクセスしているときに、logcat で NullPointerException を取得しています。これが私のコードです
public class GameActivity extends Activity {
TextView tv = (TextView)findViewById(R.id.ctry);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv.setText("Text");
}
}
ここに私のlogcatがあります
05-20 12:32:57.808: D/AndroidRuntime(18776): Shutting down VM
05-20 12:32:57.808: W/dalvikvm(18776): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-20 12:32:57.808: E/AndroidRuntime(18776): FATAL EXCEPTION: main
05-20 12:32:57.808: E/AndroidRuntime(18776): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.zafar.game/com.zafar.game.GameActivity}: java.lang.NullPointerException
05-20 12:32:57.808: E/AndroidRuntime(18776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
05-20 12:32:57.808: E/AndroidRuntime(18776): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-20 12:32:57.808: E/AndroidRuntime(18776): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-20 12:32:57.808: E/AndroidRuntime(18776): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-20 12:32:57.808: E/AndroidRuntime(18776): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 12:32:57.808: E/AndroidRuntime(18776): at android.os.Looper.loop(Looper.java:130)
05-20 12:32:57.808: E/AndroidRuntime(18776): at android.app.ActivityThread.main(ActivityThread.java:3691)
05-20 12:32:57.808: E/AndroidRuntime(18776): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 12:32:57.808: E/AndroidRuntime(18776): at java.lang.reflect.Method.invoke(Method.java:507)
05-20 12:32:57.808: E/AndroidRuntime(18776): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-20 12:32:57.808: E/AndroidRuntime(18776): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-20 12:32:57.808: E/AndroidRuntime(18776): at dalvik.system.NativeStart.main(Native Method)
05-20 12:32:57.808: E/AndroidRuntime(18776): Caused by: java.lang.NullPointerException
05-20 12:32:57.808: E/AndroidRuntime(18776): at android.app.Activity.findViewById(Activity.java:1653)
05-20 12:32:57.808: E/AndroidRuntime(18776): at com.zafar.game.GameActivity.<init>(GameActivity.java:11)
05-20 12:32:57.808: E/AndroidRuntime(18776): at java.lang.Class.newInstanceImpl(Native Method)
05-20 12:32:57.808: E/AndroidRuntime(18776): at java.lang.Class.newInstance(Class.java:1409)
05-20 12:32:57.808: E/AndroidRuntime(18776): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-20 12:32:57.808: E/AndroidRuntime(18776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
05-20 12:32:57.808: E/AndroidRuntime(18776): ... 11 more