简体   繁体   中英

Getting NullPointerException while accessing TextView

I am trying to access textview from layout in java. While accessing it I am getting NullPointerException in my logcat. Here is my code

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");
}
}

Here is my 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

仅在onCreate()代码中调用setContentView(R.layout.main)后,才应调用findViewById(R.id.ctry)

You cannot call

TextView tv = (TextView)findViewById(R.id.ctry);

before

setContentView
TextView tv = (TextView)findViewById(R.id.ctry);

above line after

setContentView(R.layout.main);

use like

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");
    }

}

UPDATE

in main.xml

<Button android:id="@+id/match" ... />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM