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

时间:2015-05-02 22:38:22

标签: android listview android-intent android-activity arraylist

在查阅了之前的大部分答案之后,我仍然对出错的地方感到困惑。我正在将JSON对象转换为字符串,然后将它们转换为doInBackground方法中的arrayList,然后在onPostExecte中调用intent,以便我可以将此arrayList传递给另一个扩展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.access$700(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.access$700(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)

我的主要活动是:

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有一些错误

3 个答案:

答案 0 :(得分:1)

重命名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);

答案 1 :(得分:1)

从堆栈跟踪中:

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>

答案 2 :(得分:1)

将它放在你的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