TextView问题

时间:2011-07-01 10:22:47

标签: android textview

(1)

@Override
       protected void onCreate(Bundle savedInstanceState)
     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);

        health = (TextView) findViewById(R.id.lblHealth);
        gold = (TextView) findViewById(R.id.lblGold);
        steel = (TextView) findViewById(R.id.lblSteel);
        wood = (TextView) findViewById(R.id.lblWood);

        health.setText("fsdfsd");

     }

(2)

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        txt = (TextView) findViewById(R.id.txtv);
        txt.setText("fsddfs");
    }

为什么第一个(1)不起作用,第二个起作用?

在第一个要求用户按下按钮的活动中,然后我使用Intent转到另一个布局 我有一个力量关闭错误? 搞什么鬼?

07-01 14:29:17.321: ERROR/AndroidRuntime(1384): Uncaught handler: thread main exiting due to uncaught exception
07-01 14:29:17.371: ERROR/AndroidRuntime(1384): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.androstrategy/com.androstrategy.Game}: java.lang.NullPointerException
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2497)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at android.os.Looper.loop(Looper.java:123)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at android.app.ActivityThread.main(ActivityThread.java:4595)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at java.lang.reflect.Method.invokeNative(Native Method)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at java.lang.reflect.Method.invoke(Method.java:521)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at dalvik.system.NativeStart.main(Native Method)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384): Caused by: java.lang.NullPointerException
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at com.androstrategy.Game.<init>(Game.java:75)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at java.lang.Class.newInstanceImpl(Native Method)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at java.lang.Class.newInstance(Class.java:1479)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2489)
07-01 14:29:17.371: ERROR/AndroidRuntime(1384):     ... 11 more
07-01 14:29:17.431: ERROR/dalvikvm(1384): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
07-01 14:29:19.621: ERROR/PackageInstallationReceiver(247): Remove /data/local/tmp/com.androstrategy.apk Fail!
07-01 14:29:21.261: ERROR/ActivityManager(77): fail to set top app changed!

                                                                                                                  

公共类游戏扩展活动 {     private static int res_health;     private static int res_wood;     private static int res_gold;     private static int res_steel;

private static int stat_str;
private static int stat_int;
private static int stat_wit;
private static int stat_maxHp;
private static int stat_otvaga;

private static TextView health;
private static TextView gold;
private static TextView steel;
private static TextView wood;

1 个答案:

答案 0 :(得分:0)

试试这个..

health = (TextView) findViewById(R.id.lblHealth);
        gold = (TextView) findViewById(R.id.lblGold);
        steel = (TextView) findViewById(R.id.lblSteel);
        wood = (TextView) findViewById(R.id.lblWood);

        health.setText("fsdfsd");