我正在尝试从java中的布局访问textview。访问它时,我在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
答案 0 :(得分:4)
只有在findViewById(R.id.ctry)
代码中致电setContentView(R.layout.main)
后才能致电onCreate()
答案 1 :(得分:3)
你不能打电话
TextView tv = (TextView)findViewById(R.id.ctry);
前
setContentView
答案 2 :(得分:2)
TextView tv = (TextView)findViewById(R.id.ctry);
之后
setContentView(R.layout.main);
使用
public class GameActivity extends Activity {
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.ctry);
tv.setText("Text");
}
}
<强>更新强>
main.xml
中的
<Button android:id="@+id/match" ... />