我试图在片段中实现 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);
}
}