无法将意图传递给另一个类

时间:2014-05-14 00:36:30

标签: android

跟进代码,无法找到应用程序发生故障的位置,我意识到数据已到达ListCat.java类,但是

我的课程扩展了片段

05-13 20:26:00.777  12398-12398/com.learn2crack.tab E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.learn2crack.tab, PID: 12398
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learn2crack.tab/com.learn2crack.tab.ListaCat}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:133)
            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:293)
            at android.app.Activity.setContentView(Activity.java:1937)
            at com.learn2crack.tab.ListaCat.onCreate(ListaCat.java:17)
            at android.app.Activity.performCreate(Activity.java:5248)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:133)
            at dalvik.system.NativeStart.main(Native Method)

class Categoria.java

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

        final View categoria = inflater.inflate(R.layout.android_frag, container, false);

final ListView  lv = (ListView) categoria.findViewById(R.id.list);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {


    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        // getting values from selected ListItem

        TextView txt = (TextView) parent.getChildAt(position - lv.getFirstVisiblePosition()).findViewById(R.id.idCAT);
        String tagId = txt.getText().toString();


        // Starting single contact activity
        Intent in = new Intent(getActivity().getApplicationContext(),ListaCat.class);
        in.putExtra(TAG_ID, tagId);
        startActivity(in);

List_item.xml

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="name"
            android:id="@+id/name"
            android:textColor="#ff7900"
            android:layout_marginLeft="10dp"
            android:textSize="20dp"
            android:gravity="fill_horizontal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/idCAT"
            android:visibility="visible" />
    </LinearLayout>

android_frag.xml

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

2 个答案:

答案 0 :(得分:1)

看起来您使用的是ListActivity,但未在布局文件中使用ID ListView定义@android:id/list。要么这样做,要么使用正常的Activity。有关ListActivity here的更多信息。

答案 1 :(得分:0)

您的主要错误是

 Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

退出getActivity().getApplicationContext()

Intent in = new Intent(getActivity(),ListaCat.class);
in.putExtra(TAG_ID, tagId);
startActivity(in);

检查您的列表是否在{/ 1}}

android.R.id.list

您需要从项目中导入R.java而不是从android.R

导入
相关问题