Android:使用2个ArrayAdapters错误

时间:2017-03-18 16:38:33

标签: android listview click adapter

我现在收到一个新错误。

这是我的代码:

lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int i, long id) {
        Toast.makeText(getApplicationContext(), beaconAdap.getItem(i).toString() + " Added to history", Toast.LENGTH_SHORT).show();
        historyBeacons.add(beaconAdap.getItem(i).toString());
        Intent intent = new Intent(getBaseContext(), HistoryActivity.class);
        intent.putStringArrayListExtra("beacons", historyBeacons);
        startActivity(intent);
        return true;
    }
});

}

所以我将int i添加到一个arraylist,我已经测试过它进入了它的确如此。然后开始历史活动的新意图并发送arraylist。

这是历史中的代码:

ListView historyLv = (ListView) findViewById(R.id.historyView);
ArrayAdapter historyAdap;
ArrayList<String> historyBeacons = getIntent().getExtras().getStringArrayList("beacons");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);
    historyAdap = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, historyBeacons);
    historyLv.setAdapter(historyAdap);
}

但是我收到了这个错误:

03-18 17:44:41.238 18641-18641/nsa.com.museum E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: nsa.com.museum, PID: 18641
                                                                java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{nsa.com.museum/nsa.com.museum.HistoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                    at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
                                                                    at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
                                                                    at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
                                                                    at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
                                                                    at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
                                                                    at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:203)
                                                                    at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
                                                                    at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
                                                                    at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
                                                                    at nsa.com.museum.HistoryActivity.<init>(HistoryActivity.java:12)
                                                                    at java.lang.Class.newInstance(Native Method)
                                                                    at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                    at android.os.Looper.loop(Looper.java:148) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
03-18 17:44:43.362 18846-18846/nsa.com.museum W/System: ClassLoader referenced unknown path: /data/app/nsa.com.museum-2/lib/x86_64

1 个答案:

答案 0 :(得分:0)

你可以告诉错误吗? 我想你忘了添加

    ListAdapter.notifyDataSetChanged();

为lisy

添加值后

............................................... ..........

NullPointerException

此错误在Android中显示原因您可能未初始化适配器 确保初始化第二个适配器