1

有人能告诉我为什么这个简单的代码行不通吗?在第一个活动中,我让用户选择一个将数组列表发送到下一个活动的选项。它不会执行 else 语句并强制关闭。多谢你们!

String[] quotes;

ArrayList<String> Dark = new ArrayList<String>();
ArrayList<String> Light = new ArrayList<String>();

这是代码

if (Dark.isEmpty()){

    Light = i.getStringArrayListExtra("light");
    quotes = Light.toArray(new String[Light.size()]);
} else {

    Dark = i.getStringArrayListExtra("dark");
    quotes = Dark.toArray(new String[Dark.size()]); 
};

这是我发送它的方式

 public void dark (View d){
    dark.add("Even 0 is a number, just leave him alone!");
    dark.add("Love is noting but a bad spell, cast onto your soul, never to be released into the wild...");
    dark.add("The end is near please do not make the same mistakes.");
    dark.add( "Jenna i love you, youre my little beandip, dive you!");
    dark.add( "cute hate but dont rate im no skate");
    dark.add("You will never live if you live a lie");
    Intent intent = new Intent(choose.this,Startme.class);
    intent.putStringArrayListExtra("dark",dark);
    startActivity(intent);
 }

public void light (View l) {
    light.add("Good ideas will come if you share your love");
    light.add("enter a world with no doubt, join god");
    light.add("come on, can you live a life");
    light.add( "where love is so is hate lol not");
    light.add( "watch me now!! go with me and join the lord");
    light.add("never be afrade of the darkness");

    Intent intent = new Intent(choose.this,Startme.class);
    intent.putStringArrayListExtra("light",light);
    startActivity(intent);
}

日志

07-30 17:41:33.457: E/AndroidRuntime(15638): FATAL EXCEPTION: main
07-30 17:41:33.457: E/AndroidRuntime(15638): java.lang.RuntimeException: Unable to start activity ComponentInfo{me.me.quote/me.me.quote.Startme}: java.lang.NullPointerException
07-30 17:41:33.457: E/AndroidRuntime(15638):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
07-30 17:41:33.457: E/AndroidRuntime(15638):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
07-30 17:41:33.457: E/AndroidRuntime(15638):    at android.app.ActivityThread.access$1500(ActivityThread.java:124)
07-30 17:41:33.457: E/AndroidRuntime(15638):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
07-30 17:41:33.457: E/AndroidRuntime(15638):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 17:41:33.457: E/AndroidRuntime(15638):    at android.os.Looper.loop(Looper.java:130)
07-30 17:41:33.457: E/AndroidRuntime(15638):    at android.app.ActivityThread.main(ActivityThread.java:3806)
07-30 17:41:33.457: E/AndroidRuntime(15638):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 17:41:33.457: E/AndroidRuntime(15638):    at java.lang.reflect.Method.invoke(Method.java:507)
07-30 17:41:33.457: E/AndroidRuntime(15638):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-30 17:41:33.457: E/AndroidRuntime(15638):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-30 17:41:33.457: E/AndroidRuntime(15638):    at dalvik.system.NativeStart.main(Native Method)
07-30 17:41:33.457: E/AndroidRuntime(15638): Caused by: java.lang.NullPointerException
07-30 17:41:33.457: E/AndroidRuntime(15638):    at me.me.quote.Startme.onCreate(Startme.java:164)
07-30 17:41:33.457: E/AndroidRuntime(15638):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-30 17:41:33.457: E/AndroidRuntime(15638):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
07-30 17:41:33.457: E/AndroidRuntime(15638):    ... 11 more

这是完整的代码:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.startquotes);

    Intent i = getIntent(); 

    if (Dark.isEmpty()){

        Light = i.getStringArrayListExtra("light");
        quotes = Light.toArray(new String[Light.size()]);
    } else {

        Dark = i.getStringArrayListExtra("dark");
        quotes = Dark.toArray(new String[Dark.size()]); 
    }

    Button clouds = (Button) findViewById(R.id.Change);
    TranslateAnimation cloud2 = new TranslateAnimation(0, 0, +200, 0);
    clouds.startAnimation(cloud2);
    cloud2.setDuration(1000);

    Button clouds3 = (Button) findViewById(R.id.nxt);
    TranslateAnimation cloud3 = new TranslateAnimation(+200, 0, 0, 0);
    clouds3.startAnimation(cloud3);
    cloud3.setDuration(1000);

    Button clouds4 = (Button) findViewById(R.id.back);
    TranslateAnimation cloud4 = new TranslateAnimation(-200, 0, 0, 0);
    clouds4.startAnimation(cloud4);
    cloud4.setDuration(1000);

    AdView adview = (AdView)findViewById(R.id.adView);
    AdRequest re = new AdRequest();
    re.setTesting(true);
    adview.loadAd(re);

    String ser = SerializeObject.ReadSettings(Startme.this, "myobject.dat");
    if (ser != null && !ser.equalsIgnoreCase("")) {
        Object obj = SerializeObject.stringToObject(ser);
        // Then cast it to your object and 
        if (obj instanceof ArrayList) {
            // Do something
            give = (ArrayList<String>)obj;
        }
    }

    TextView Dark=(TextView)findViewById(R.id.Quote); 

    Typeface face=Typeface.createFromAsset(getAssets(), "fonts/font1.ttf"); 
    Dark.setTypeface(face); 

    num = gen.nextInt(5);
    TextView text = (TextView) findViewById(R.id.Quote);
    text.setText( quotes[num]);

    TextView number = (TextView) findViewById(R.id.Qn);
    number.setText("#"+num);

    TextView Q=(TextView)findViewById(R.id.Qn); 
    Typeface tf=Typeface.createFromAsset(getAssets(), "fonts/titlefont.ttf"); 
    Q.setTypeface(tf); 

    }
}

第 164 行

    if (Dark.isEmpty()){

        Light = i.getStringArrayListExtra("light");
----------> quotes = Light.toArray(new String[Light.size()]);
    } else {
4

1 回答 1

2

它永远不会执行您的 else 语句,因为您的 Dark 数组列表将始终为空。你在你的活动中像那样初始化它,你从来没有从你的意图中读取它。您可能想先阅读两者,然后根据一个的“空”来决定执行 i​​f 或 else。您将在下面看到原因。

getStringArrayListExtra(key)如果您给它的键不存在,该函数将默认返回 null,这意味着您的意图中从来没有添加键“light”的额外内容。这表明您的light()函数没有被调用。

我建议检查您的课程,其中包括light()and dark()

于 2012-07-30T22:44:51.217 回答