使用getStringArray时的java.lang.NullPointerException

时间:2014-08-23 21:19:37

标签: java android

我正在开发一个可以生成宝藏清单的应用程序。

这是我将列表存储到的字符串数组:

String treasure[] = {"", "", "", "", "", "", "", "", "", "",
        "", "", "", "", ""} ;

这就是我填写值的方式

treasure[0] = coinvalue + " copper pieces";

这是我将编译列表发送到下一部分的地方:

Bundle treasurelist = new Bundle();
        treasurelist.putStringArray("loot", treasure);
        Intent a = new Intent(TreasureGen.this, Treasure_Results.class);
        a.putExtras(treasurelist);
        startActivity(a);

这是我在下一阶段拿到名单的地方:

public class Treasure_Results extends ListActivity {
        String getTreasure[] ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle gettreasurelist = getIntent().getExtras();
        getTreasure = gettreasurelist.getStringArray("loot");
        setListAdapter(new ArrayAdapter<String>(Treasure_Results.this, android.R.layout.simple_list_item_1, getTreasure));


    }
}

这是我得到的错误代码:

Caused by: java.lang.NullPointerException
            at com.giraffelitis.ptindustries.dungeonmasterassistant.Treasure_Results.onCreate(Treasure_Results.java:24)
            at android.app.Activity.performCreate(Activity.java:5431)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
            at android.app.ActivityThread.access$900(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

当我点击链接时,它会将我发送到这行代码

getTreasure = gettreasurelist.getStringArray("loot");

由于这只是我尝试使用Java编写代码的第二天,我不知道该怎么做...我已经浏览了论坛,试图找到解决方案,但有一些类似的问题,但我不能从那些答案中找出我的问题。

更新

我已尝试过以下建议......以下是适用于此功能的TreasureGen活动代码。

roll.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    treasurecr = counter;
                    int d100;
                    int coinvalue;

                    String treasure[] = {"", "", "", "", "", "", "", "", "", "",
                            "", "", "", "", ""} ;
                    Bundle treasurelist = new Bundle();
                    treasurelist.putStringArray("loot", treasure);
                    Intent a = new Intent(TreasureGen.this, Treasure_Results.class);
                    a.putExtras(treasurelist);
                    startActivity(a);

                    Intent openStartPoint = new                   Intent("com.giraffelitis.ptindustries.TREASUREGEN2");
                    startActivity(openStartPoint);
                }
            });

这是我的整个Treasure_Results活动

public class Treasure_Results extends ListActivity {
        String getTreasure[] ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle treasurelist = getIntent().getExtras();
        getTreasure = treasurelist.getStringArray("loot");
        System.out.println("getTreasure = " + getTreasure);
        setListAdapter(new ArrayAdapter<String>(Treasure_Results.this, android.R.layout.simple_list_item_1, getTreasure));

    }
    //@Override
    //public void onListItemClick(ListView l, View v, int position, long id) {
        // do something with the data
    //}
}

这是我的错误日志,关于getTreasure数组中的内容

08-24 17:02:34.040  25764-25764/com.giraffelitis.ptindustries.dungeonmasterassistant I/System.out﹕ getTreasure = [Ljava.lang.String;@429bcae0

谢谢你的时间!

1 个答案:

答案 0 :(得分:0)

如果要在两个活动之间传输数组字符串,可以使用以下代码(在初始活动中):

Intent intent = new Intent();
in.putExtra("loot", treasure);

同时,在您要处理数组的活动中:

Intent intent = getIntent();
String treasure[] = intent.getStringArrayExtra("loot");