1

私はこのチュートリアルに従っていました。

CRUD機能やメインメニューがないなど、アプリのニーズに合わせて変更しました-アプリの起動時に実行されるメインアクティビティ中にすべてのアイテムをリストしたいだけです。

コードにエラーはないようですが、実行するとUnfortunately, MyFirstApp has stoppedVM で次のようになります。

LogCatは私にこれを与えます:

E/AndroidRuntime(910): java.lang.RuntimeException: アクティビティ ComponentInfo を開始できません {com.example.myfirstproject/com.example.myfirstproject.MainActivity}: java.lang.RuntimeException: コンテンツには、id 属性が「android.R.id.list」

どうする?.xml レイアウトを確認して変更を加えましたが、アプリがまだクラッシュします。

MainActivity.java

package com.example.myfirstproject;

//imports

public class MainActivity extends ListActivity implements OnItemClickListener {

    // Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> carsList;

    // url to get all products list
    private static String url_all_cars = "http://localhost/webservice/get_all_cars.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_CARS = "cars";
    private static final String TAG_NAME = "name";

    // products JSONArray
    JSONArray cars = null;

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

        // Hashmap for ListView
        carsList = new ArrayList<HashMap<String, String>>();

        // Loading products in Background Thread
        new LoadAllcars().execute();

        // Get listview
        ListView lv = getListView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    /**
     * Background Async Task to Load all product by making HTTP Request
     * */
    class LoadAllcars extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading cars. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting All products from url
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_cars, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All cars: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    cars = json.getJSONArray(TAG_CARS);

                    // looping through All Products
                    for (int i = 0; i < cars.length(); i++) {
                        JSONObject c = cars.getJSONObject(i);

                        // Storing each json item in variable
                        String title = c.getString(TAG_NAME);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_NAME, name);

                        // adding HashList to ArrayList
                        carsList.add(map);
                    }
                } else {
                    // no products found
                    pDialog = new ProgressDialog(MainActivity.this);
                    pDialog.setMessage("No cars found");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(false);
                    pDialog.show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            MainActivity.this, carsList,
                            android.R.id.list, new String[] {TAG_NAME},
                            new int[] { R.id.title });
                    // updating listview
                    setListAdapter(adapter);
                }
            });

        }
    }
}

activity_main.xml (リストビューを使用したメインアクティビティのレイアウト):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </ListView>   
</LinearLayout>

list_item.xml (個々のリスト項目のレイアウト):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <!-- Name Label -->
    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="6dip"
        android:textSize="17dip"
        android:textStyle="bold" />

</LinearLayout>
4

5 に答える 5

5

こちらのListActivityドキュメントでレイアウトがどのように定義されているかをご覧ください。

あなたのListViewIDはandroid:id="@+id/list"であり、android:id="@android:id/list"

さらに、ListAdapterがクラッシュします

ListAdapter adapter = new SimpleAdapter(
                            MainActivity.this, carsList,
                            android.R.id.list, new String[] {TAG_NAME},
                            new int[] { R.id.title });

アイテムビューとしてAndroidListViewを使用するようにアダプターに指示しています。このためにlist_item.xmlIDを渡し、適切なTextView ID(名前)を使用する必要があります。

元:

ListAdapter adapter = new SimpleAdapter(
                                MainActivity.this, carsList,
                                R.layout.list_item, new String[] {TAG_NAME},
                                new int[] { R.id.name});
于 2012-11-02T21:12:40.863 に答える
2

あなたのactivity_main.xml使用android:id="@android:id/list"ではなくandroid:id="@+id/list"、それは動作するはずです。

今のところ、ListViewのIDはですyourapplicationpackage.R.id.list

于 2012-11-02T21:12:49.147 に答える
2

android:id="@android:id/list"xml で id を割り当てるときに使用します。

于 2012-11-02T21:25:49.343 に答える
1

ListView の ID は、 ListActivity のドキュメントに従って必要@android:id/listな参照にする必要があることに注意してください。android.R.id.list

対照的に、あなたの id は@+id/list新しい id を作成しますcom.example.myfirstproject.R.id.list

于 2012-11-02T21:21:22.357 に答える
0

これは古い投稿ですが、ここで同じ問題に直面した他の人にとっては私の解決策です:

リスト ビューまたは作成したその他のオブジェクトの ID がレイアウトに存在することを確認した場合は、gen ソースに移動して、R.java ファイルの ID の名前を確認します。そこにない場合は、アイテムを作成していません。- 次に、Java コードで、android.R がインポート リストに含まれていないことを確認します。だったら脱いで。次に、パッケージ R.java import com.yourpackagename.R; を手動で追加します。これで、項目を Java コードに追加できるようになりました。 exp: ListView lvitems = (ListView) findViewById(R.id.nameofit);

このような状況で Project>Clean を使用することはお勧めしません。生成された R.java ファイルが簡単に失われる可能性があります。

于 2013-10-27T00:10:30.683 に答える