开发者

TextView problems

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

Why the first(1) DOES NOT work and the second works?

In the first activity im asking a user to press the button, then im using an Intent to go to another layout And there im getting a force close error ? what the hell?

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!

public class Game extends Activity { 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;


try this..

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜