2015-05-02 56 views
0

在諮詢了大多數以前的答案之後,我仍然對我出錯的地方感到困惑。我將JSON對象轉換爲字符串,然後將它們放到doListBackground方法中的arrayList中,然後在onPostExecte中調用intent,以便我可以將此arrayList傳遞到擴展ListActivity的另一個活動中,在那裏我會出錯並獲取錯誤:無法在將arrayList從一個活動傳遞到另一個活動時啓動活動ComponentInfo

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> 

是我打電話活性2新佈局正確的方式,我覺得我的ListView有一些錯誤

回答

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); 
+0

我應該在LV寫什麼=(ListView控件)findViewById(R.id。清單); 代替R.id.list? – lijo050

+0

非常感謝,你是我的救世主:)過去4小時打破了我的頭 – lijo050

+0

是的,並且在你的佈局中使用'@android:id/list'代替'@ + id/list' –

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> 
+0

你可以發現我的風格有任何其他錯誤,因爲我是全新的,我在Android自學。提前致謝。 – lijo050

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 
相關問題