将 arrayList 从一个 activity 传递到另一个时无法启动 activity ComponentInfo

Unable to start activity ComponentInfo while passing arrayList from one activity to another

在查阅了之前的大部分答案后,我仍然很困惑我哪里做错了。我正在将 JSON 对象转换为字符串,然后将它们放入 doInBackground 方法中的 arrayList,然后在 onPostExecte 中调用 intent,这样我就可以将此 arrayList 传递给另一个 activity,它扩展了 ListActivity, 有些地方我出错了并收到错误:

05-03 03:43:24.956  31502-31502/com.example.lijo.medicinemagic E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lijo.medicinemagic/com.example.lijo.medicinemagic.Activity2}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
            at android.app.ActivityThread.access0(ActivityThread.java:150)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5283)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
            at android.app.ListActivity.onContentChanged(ListActivity.java:243)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:367)
            at android.app.Activity.setContentView(Activity.java:1930)
            at com.example.lijo.medicinemagic.Activity2.onCreate(Activity2.java:22)
            at android.app.Activity.performCreate(Activity.java:5283)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
            at android.app.ActivityThread.access0(ActivityThread.java:150)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5283)
            at java.lang.reflect.Method.invokeNative(Native Method)

我的主要 Activity 是:

public class MainActivity extends ActionBarActivity {

    private Button search;
    private EditText med_name;
    ArrayList<String> med_List = new ArrayList<>();

    JSONArray suggestion = null;
    private static final String TAG_RESPONSE = "response";
    private static final String TAG_SUGGESTIONS = "suggestions";
    private static final String TAG_Med = "suggestion";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        med_name = (EditText) findViewById(R.id.medicine);
        search = (Button) findViewById(R.id.search);

        search.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String med = med_name.getText().toString();
                new APICall().execute(new String[]{med});

            }
        });
    }

    class APICall extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
           //calls another class which returns JSON Objects which is conveted to arrayList "med_List" so I'm not showing the code

                return null;
            }

            @Override
            protected void onPostExecute (String result){
                Intent intent = new Intent(MainActivity.this, Activity2.class);
                intent.putStringArrayListExtra("key", med_List);
                startActivity(intent); //is this kind of intent correct

            }
        }
}

现在Activity2.java

public class Activity2 extends ListActivity{

    private ListView lv;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_item);
        Intent i = getIntent();
        ArrayList<String> list =i.getStringArrayListExtra("key");
        for (int j=0;j<list.size();j++)
            Log.d("arr >", list.get(j));

        lv = (ListView) findViewById(R.id.list);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                this,
                R.layout.list_item2, R.id.med_name,
                list);
        lv.setAdapter(arrayAdapter);
    }
}

list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

list_item2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/med_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:textSize="20sp" />
</LinearLayout>

Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lijo.medicinemagic" >

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Activity2">
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="in.wptrafficanalyzer.Activity2" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我在 Activity2 中调用新布局的方式是否正确,我认为我的 ListView 有一些错误

像这样重命名您的 ListView 的 ID,

<ListView android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

因为您使用的是 ListActivity

并通过

找到它
lv = (ListView) findViewById(android.R.id.list);

来自您的堆栈跟踪:

Your content must have a ListView whose id attribute is 'android.R.id.list'

list_item.xml - 您需要使用列表的 android id 参考 - 而不是创建新的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

把它放在你的 xml 中,因为你正在使用 listActivity

    <ListView
  android:id="@android:id/list"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
</ListView> 

在java代码中

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);//because it is listActivity