尝试使用从另一个活动传递的arraylist填充listview时,我一直收到错误。我输入的适配器代码与我在另一个活动中完全相同,它接受用户输入,添加到数组并填充列表工作正常但不在这里。我这里有两个列表,因为我一直在尝试测试其中一个。
班级:
package com.liftlover.mitchell.workoutapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class RoutineDetail extends AppCompatActivity {
TextView routineTxt;
ListView lv;
List<String> excersiseList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_routine_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intentData = getIntent();
String getRoutine = intentData.getStringExtra("routine");
routineTxt = (TextView) findViewById(R.id.routineText);
routineTxt.setText(getRoutine);
lv = (ListView) findViewById(R.id.routineListView);
excersiseList = intentData.getStringArrayListExtra("excersiseList");
List<String> list = new ArrayList<String>();
list.add("wegewgW");
list.add("robnwgn");
list.add("q[3gng");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(RoutineDetail.this,android.R.layout.simple_list_item_1,list );
lv.setAdapter(arrayAdapter);
}
}
错误(我无法正确格式化抱歉):
Process: com.liftlover.mitchell.workoutapplication, PID: 2195
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liftlover.mitchell.workoutapplication/com.liftlover.mitchell.workoutapplication.RoutineDetail}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
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 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.liftlover.mitchell.workoutapplication.RoutineDetail.onCreate(RoutineDetail.java:49)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
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)