【问题标题】:java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' [duplicate]java.lang.RuntimeException:您的内容必须有一个 ID 属性为“android.R.id.list”的 ListView [重复]
【发布时间】:2016-03-25 12:50:01
【问题描述】:

我想使用列表视图在我的应用程序中显示已发送的项目,但是当我运行它时,我的应用程序崩溃并且我在我的 logcat 上收到以下消息“java.lang.RuntimeException:您的内容必须有一个 ListView,其id 属性是'android.R.id.list'”。 我已经在这个论坛上搜索了解决方案,并且我已经有一个带有所需 ID 的列表视图,如您所见,但仍然会弹出错误。我哪里错了?请帮忙。

这是我的 DisplaySentItemsActivity:

package zw.ac.msu.kuda.e_servives;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class DisplaySentItemsActivity extends AppCompatActivity {
String json_sentitems;
JSONObject jsonObject;
JSONArray jsonArray;
SentItemsAdapter sentItemsAdapter;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_sent_items);
    listView =(ListView) findViewById(android.R.id.list);
    listView.setAdapter(sentItemsAdapter);
    sentItemsAdapter=new SentItemsAdapter(this,R.layout.fragment_sentitems);                           
    json_sentitems=getIntent().getExtras().getString("json_data");
    try {

        jsonObject=new JSONObject(json_sentitems);
        jsonArray=jsonObject.getJSONArray("server_response");
        int count=0;
        int id;
        String email, dateSent, subject, body, attachments;
        while(count<jsonArray.length()) {
            JSONObject finalObject = jsonArray.getJSONObject(0);
            id = finalObject.getInt("id");
            email = finalObject.getString("email");
            dateSent = finalObject.getString("dateSent");
            subject = finalObject.getString("subject");
            body = finalObject.getString("body");
            attachments = finalObject.getString("attachments");
            SentItems sentItems=new  SentItems(subject,dateSent,body,attachments,id);
            sentItemsAdapter.add(sentItems);
            count++;
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="zw.ac.msu.kuda.e_servives.DisplaySentItemsActivity">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

</RelativeLayout>

【问题讨论】:

    标签: java android listview


    【解决方案1】:

    改变

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

    还有这个

    listView =(ListView) findViewById(android.R.id.list);
    

    有了这个

    listView =(ListView) findViewById(R.id.list);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多