简体   繁体   中英

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

I would like to have the working ListView from the MainActivity in the ExpAct (2.Activity).

What I have: Edittext and a Button that creates a new entry to a Listview upon clicking.
The ListView on MainAct is just for checking and will be deleted if ExpAct works.
Upon clicking the "next"Btn I get to ExpAct.

I get a raw use parameter warning. -- And if I were to fix that, it gives me an NPE-crash for the ExpAct , or an empty ListView.

Is the issue that the getter Intent for the String/Array doesnt work? Or does it lie with the creation of the 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);    
    }
}

Logs since it has become red.

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'

It looks like you're trying to start the activity without putting the ArrayList as an extra.

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

When you get a NullPointerException, you should first check why would the list be null:

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

So after looking at your code, I saw that you're not passing the ArrayList with the intent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM