将字符串(或数组)传递给另一个 Activity 中的 ListView(NPE 错误)

Pass String (or Array) to ListView in another Activity (NPE error)

我想从 ExpAct (2.Activity) 中的 MainActivity 获得工作的 ListView。

我有什么:Edittext 和一个按钮,它在单击时创建 新条目 到列表视图。
MainAct 上的 ListView 仅用于检查,如果 ExpAct 有效,将被删除。
单击“下一步”按钮后,我进入了 ExpAct。

我收到原始使用参数警告。 -- 如果我要解决这个问题,它会给我带来 ExpAct 或空 ListView 的 NPE 崩溃。

问题是 getter 对 String/Array 的 Intent 不起作用吗?还是在于 ArrayAdapter 的创建?

MainActivity.java

public class MainActivity extends AppCompatActivity {
     ListView LV1;
     ArrayList < String > listExp;
     Button button1, btnext;
     EditText ETbc;
     ArrayAdapter < String > arrayAdapter;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         LV1 = findViewById(R.id.LV1);
         button1 = findViewById(R.id.button1);
         ETbc = findViewById(R.id.ETbc);
         listExp = new ArrayList < String > ();
         arrayAdapter = new ArrayAdapter < String > (getApplicationContext(),
             android.R.layout.simple_list_item_1, listExp);
         button1.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 String names = ETbc.getText().toString();
                 listExp.add(names);
                 LV1.setAdapter(arrayAdapter);
                 arrayAdapter.notifyDataSetChanged();
                 Intent inter = new Intent(MainActivity.this, ExpAct.class);
                 inter.putExtra("key", listExp);
                 setResult(Activity.RESULT_OK, inter);
             }
         });
         btnext = findViewById(R.id.btnext);
         btnext.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent intent = new Intent(MainActivity.this, ExpAct.class);
                 startActivity(intent);
                 finish();
             }
         });
     }
 }

*ExpAct.java: *

public class ExpAct extends MainActivity {
    ListView LV2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exp);

        Intent i = getIntent();
        listExp = i.getStringArrayListExtra("key");

        LV2 = findViewById(R.id.LV2);
        ArrayAdapter arrayAdapter = new ArrayAdapter < String > (getApplicationContext(),
            android.R.layout.simple_list_item_1, R.id.LV2, listExp);  // here without R.id.LV2 
        LV2.setAdapter(arrayAdapter);    
    }
}

日志变红了。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.testcheckbox, PID: 20358
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testcheckbox/com.example.testcheckbox.ExpAct}. 
      java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6255)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
     Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:344)
        at android.widget.ListView.setAdapter(ListView.java:510)
        at com.example.testcheckbox.ExpAct.onCreate(ExpAct.java:27)
        at android.app.Activity.performCreate(Activity.java:6670)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) 
        at android.os.Handler.dispatchMessage(Handler.java:110) 
        at android.os.Looper.loop(Looper.java:203) 
        at android.app.ActivityThread.main(ActivityThread.java:6255) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 
Disconnected from the target VM, address: 'localhost:57662', transport: 'socket'

看起来您正在尝试启动 activity 而没有将 ArrayList 作为额外的。

btnext = findViewById(R.id.btnext);
btnext.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       Intent intent = new Intent(MainActivity.this, ExpAct.class);
       // You need to put the extra data here:
       intent.putExtra("key", listExp);
       startActivity(intent);
       finish();
    }
});

当你得到一个 NullPointerException 时,你应该首先检查为什么列表会是空的:

Attempt to invoke interface method 'int java.util.List.size()' on a null object reference

所以在查看您的代码后,我发现您没有按意图传递 ArrayList。