1

私はリストビューを作成しようとしていますlistが、明らかに持っていますが、という名前のリストビューはありません。

フラグメントのレイアウト ファイル:

<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=".ListActivity" >

    <View
        android:id="@+id/line2"
        android:layout_width="fill_parent"
        android:layout_height="0.5dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:background="@android:color/black" 
        android:layout_below="@+id/list"/>

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

そして、フラグメント java (「ListActivity」はフラグメントであり、アクティビティではありません)。

別のアクティビティ内にネストされています。

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListActivity extends ListFragment {

    static final String[] MOBILE_OS = 
            new String[] { "Android", "iPhone", "WindowsMobile",
                    "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                    "Linux", "OS/2" };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return   inflater.inflate(R.layout.activity_list, container, false);    
        }
//  
//  @Override
//  public void onStart() {
//      super.onStart();
//      
//      setListAdapter(new MobileArrayAdapter(getActivity(), MOBILE_OS));
//  }

    @Override
      public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };
        ListView list = (ListView)getView().findViewById(R.id.list);
        list.setAdapter(new MobileArrayAdapter(getActivity(), values));
      }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        //get selected items
        String selectedValue = (String) getListAdapter().getItem(position);
        Toast.makeText(getActivity(), selectedValue, Toast.LENGTH_SHORT).show();

    }

    public void onBackPressed() {

    }


}

そしてアダプター:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MobileArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MobileArrayAdapter(Context context, String[] values) {
        super(context, R.layout.list_item, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_item, parent, false);
        TextView title = (TextView) rowView.findViewById(R.id.title);

        title.setText("      "+values[position]);

        // Change icon based on name
        String s = values[position];

        System.out.println(s);

        return rowView;
    }
}

LogCat:

10-05 20:37:58.824: W/dalvikvm(6515): threadid=1: thread exiting with uncaught exception (group=0x41064300)
10-05 20:37:58.828: E/AndroidRuntime(6515): FATAL EXCEPTION: main
10-05 20:37:58.828: E/AndroidRuntime(6515): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.free/com.example.free.Fragments}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.app.ActivityThread.access$600(ActivityThread.java:142)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.os.Looper.loop(Looper.java:137)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.app.ActivityThread.main(ActivityThread.java:4931)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at java.lang.reflect.Method.invokeNative(Native Method)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at java.lang.reflect.Method.invoke(Method.java:511)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at dalvik.system.NativeStart.main(Native Method)
10-05 20:37:58.828: E/AndroidRuntime(6515): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.app.Activity.performStart(Activity.java:5018)
10-05 20:37:58.828: E/AndroidRuntime(6515):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-05 20:37:58.828: E/AndroidRuntime(6515):     ... 11 more

ここで何が問題なのですか?

4

1 に答える 1

1

変化する

android:id="@+id/list"

android:id="@android:id/list"

android:id="@+id/list"の新しい Id (「リスト」) を作成しますListViewが、を使用する場合は、フレームワークから既存の Id を参照する必要がありますListFragment。したがって、その前に名前空間「android」を記述する必要があります。

于 2013-10-05T17:48:14.983 に答える