0

我试图在片段中实现 lisadapter,但 Java 不允许扩展两个超类。因此我无法将 Fragment 和 ListActivity 扩展到同一个类。然后我改变了我的应用程序。我用 ListFragment 替换了所有片段。但是当我运行我的应用程序时,它崩溃了。早些时候它运行良好。你能帮帮我吗?请在 logcat 条目下方找到

我在下面粘贴了 protab1.xml:

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

     <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

         <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
             >


        </LinearLayout>
    </ScrollView>

</LinearLayout>

日志猫:

E/AndroidRuntime(959): FATAL EXCEPTION: main
E/AndroidRuntime(959): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pro1/com.example.pro1.Secactivity}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
E/AndroidRuntime(959):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
E/AndroidRuntime(959):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime(959):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime(959):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime(959):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(959):  at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(959):  at android.app.ActivityThread.main(ActivityThread.java:5039)
E/AndroidRuntime(959):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(959):  at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(959):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(959):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(959):  at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(959): Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
E/AndroidRuntime(959):  at android.app.ListFragment.ensureList(ListFragment.java:402)
E/AndroidRuntime(959):  at android.app.ListFragment.onViewCreated(ListFragment.java:203)
E/AndroidRuntime(959):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:899)
E/AndroidRuntime(959):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
E/AndroidRuntime(959):  at android.app.BackStackRecord.run(BackStackRecord.java:682)
E/AndroidRuntime(959):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
E/AndroidRuntime(959):  at android.app.Activity.performStart(Activity.java:5113)
E/AndroidRuntime(959):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
E/AndroidRuntime(959):  ... 11 more

Activity 类持有 ListFragments protab1 和 protab2

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

public class Secactivity extends Activity {
    public static Context mycontext ;

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.promain);
        mycontext = getApplicationContext();

        //ActionBar
            ActionBar actionbar = getActionBar();
            actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
            ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");

            ListFragment PlayerFragment = new Protab1();
            ListFragment StationsFragment = new Protab2();

            PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
            StationsTab.setTabListener(new MyTabsListener(StationsFragment));

            actionbar.addTab(PlayerTab);
            actionbar.addTab(StationsTab);
        }


}

class MyTabsListener implements ActionBar.TabListener {
    public ListFragment fragment;

    public MyTabsListener(ListFragment fragment) {
        this.fragment = fragment;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        Toast.makeText(Secactivity.mycontext, "Reselected!", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.fragment_container, fragment);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
    }

}

protab1 列表片段

package com.example.pro1;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.app.ListFragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Protab1 extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.protab1, container, false);
    }



}

protab2 列表片段

package com.example.pro1;

import android.annotation.TargetApi;
import android.app.Fragment;
import android.app.ListFragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Protab2 extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.protab2, container, false);
    }
}
4

2 回答 2

1

更改此行并让我知道它是否不起作用:(从 Activity 到 FragmentActivity)

public class Secactivity extends FragmentActivity
于 2013-01-09T06:23:05.010 回答
0

将此包含添加到您的列表布局中:

<include layout="@android:layout/list_content" />
<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white" />

这对我有用....

android 网站上的文档:http: //developer.android.com/reference/android/app/ListFragment.html

public View onCreateView (LayoutInflater inflater, ViewGroup 容器, Bundle savedInstanceState)

在 API 级别 11 中添加 提供默认实现以返回简单的列表视图。子类可以覆盖以替换为自己的布局。如果这样做,返回的视图层次结构必须有一个 ID 为 android.R.id.list 的 ListView,并且可以选择在列表为空时显示一个兄弟视图 id android.R.id.empty。

*如果您使用自己的自定义内容覆盖此方法,请考虑在您的 layout* 文件中包含标准布局 list_content,以便继续保留 ListFragment 的所有标准行为。特别是,这是目前显示内置不确定进度状态的唯一方法。

于 2013-09-13T22:45:17.317 回答