gpt4 book ai didi

android - 改变方向时 fragment 膨胀错误

转载 作者:太空宇宙 更新时间:2023-11-03 10:26:27 24 4
gpt4 key购买 nike

好的,问题来了,我有一个 Fragment Activity,里面有两个 Fragment。一个是 ListFragment,另一个是 Fragment。所以,我想根据选择的项目显示 fragment 。这就是问题所在。如果我以横向模式或纵向模式启动此 fragment Activity ,它工作正常。不管怎样,当我换到另一个方向时,它总是崩溃。错误与充气机有关,类似于 onCreateView,但我查看了其他帖子,但没有其他解决方案,例如 onConfigurationChanges、onSaveInstances,从名称更改为类。都有同样的错误。所以,我希望有人能帮助我。我确实有 layout-land 和布局,但仍然无法正常工作。

这是我的代码。

activity_pontix(陆地和肖像模式)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCFF99"
android:orientation="vertical" >

<TextView android:id="@+id/Pontix"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:text="@string/pontix"
android:background="#ffffff"
android:textColor="#000000"
android:textSize="40dp" />

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

<fragment
android:id="@+id/sideMenu"
android:name="com.example.pontix.SideMenu"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />

<fragment
android:id="@+id/content"
android:name="com.example.pontix.ContentPreview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>

</LinearLayout>

侧边菜单.java

public class SideMenu extends ListFragment
{
OnMenuSelectedListener setSelected;

public interface OnMenuSelectedListener
{
public void onItemSelected(int position);
}

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

int layout = R.layout.side_menu_item;

setListAdapter(new ArrayAdapter<String>(getActivity(), layout, MenuItem.Items));
}

@Override
public void onStart()
{
super.onStart();

if (getFragmentManager().findFragmentById(R.id.sideMenu) != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}

@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);

try
{
setSelected = (OnMenuSelectedListener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString() + " must implement OnMenuSelectedListener");
}
}

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

// Set the item as checked to be highlighted when in two-pane layout
getListView().setItemChecked(position, true);
}
}

内容预览.java

public class ContentPreview extends Fragment
{
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (savedInstanceState != null)
{
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}

return inflater.inflate(R.layout.content_view, container, false);
}

@Override
public void onStart()
{
super.onStart();

Bundle args = getArguments();
if (args != null)
{
updateContentPreview(args.getInt(ARG_POSITION));
}
else if (mCurrentPosition != -1)
{
updateContentPreview(mCurrentPosition);
}
}

public void updateContentPreview(int position)
{
switch(position)
{
case 1:
{
TextView article = (TextView) getActivity().findViewById(R.id.contentView);
article.setText("Voce estava na aba Gincana");
mCurrentPosition = position;
break;
}
case 2:
{
TextView article = (TextView) getActivity().findViewById(R.id.contentView);
article.setText("Voce estava na aba Tarefas");
mCurrentPosition = position;
break;
}
case 3:
{
TextView article = (TextView) getActivity().findViewById(R.id.contentView);
article.setText("Voce estava na aba Ranking");
mCurrentPosition = position;
break;
}
case 4:
{
TextView article = (TextView) getActivity().findViewById(R.id.contentView);
article.setText("Voce estava na aba Amigos");
mCurrentPosition = position;
break;
}
case 5:
{
TextView article = (TextView) getActivity().findViewById(R.id.contentView);
article.setText("Voce estava na aba Perfil");
mCurrentPosition = position;
break;
}
case 6:
{
TextView article = (TextView) getActivity().findViewById(R.id.contentView);
article.setText("Voce estava na aba Sobre");
mCurrentPosition = position;
break;
}
}
}

@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);

outState.putInt(ARG_POSITION, mCurrentPosition);
}
}

PontixActivity.java

public class PontixActivity extends FragmentActivity implements SideMenu.OnMenuSelectedListener 
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pontix);

Intent intent = getIntent();
int code = intent.getIntExtra(GincanaActivity.EXTRA_CONTENT,0);

SideMenu sideMenu = new SideMenu();

sideMenu.setArguments(getIntent().getExtras());

getSupportFragmentManager().beginTransaction().add(R.id.sideMenu, sideMenu).commit();

ContentPreview contentFragment = (ContentPreview) getSupportFragmentManager().findFragmentById(R.id.content);

if (contentFragment != null)
{
contentFragment.updateContentPreview(code);

}
}

public void onItemSelected(int position)
{
switch(position)
{
case 0:
{
Intent contentView = new Intent(this, GincanaActivity.class);
startActivity(contentView);
break;
}
case 1:
{
Intent contentView = new Intent(this, TarefaActivity.class);
startActivity(contentView);
break;
}
case 2:
{
Intent contentView = new Intent(this, RankingActivity.class);
startActivity(contentView);
break;
}
case 3:
{
Intent contentView = new Intent(this, MainActivity.class);
startActivity(contentView);
break;
}
case 4:
{
Intent contentView = new Intent(this, MainActivity.class);
startActivity(contentView);
break;
}
case 5:
{
Intent contentView = new Intent(this, MainActivity.class);
startActivity(contentView);
break;
}
}
}
}

这是错误日志。

08-03 13:13:58.745: W/IInputConnectionWrapper(13847): showStatusIcon on inactive InputConnection
08-03 13:14:09.065: D/AbsListView(13847): Get MotionRecognitionManager
08-03 13:14:09.065: D/AbsListView(13847): Get MotionRecognitionManager
08-03 13:14:12.310: D/AndroidRuntime(13847): Shutting down VM
08-03 13:14:12.310: W/dalvikvm(13847): threadid=1: thread exiting with uncaught exception (group=0x40c661f8)
08-03 13:14:12.340: E/AndroidRuntime(13847): FATAL EXCEPTION: main
08-03 13:14:12.340: E/AndroidRuntime(13847): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pontix/com.example.pontix.PontixActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class fragment
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3365)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.app.ActivityThread.access$700(ActivityThread.java:128)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1165)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.os.Looper.loop(Looper.java:137)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.app.ActivityThread.main(ActivityThread.java:4514)
08-03 13:14:12.340: E/AndroidRuntime(13847): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 13:14:12.340: E/AndroidRuntime(13847): at java.lang.reflect.Method.invoke(Method.java:511)
08-03 13:14:12.340: E/AndroidRuntime(13847): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
08-03 13:14:12.340: E/AndroidRuntime(13847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
08-03 13:14:12.340: E/AndroidRuntime(13847): at dalvik.system.NativeStart.main(Native Method)
08-03 13:14:12.340: E/AndroidRuntime(13847): Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class fragment
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-03 13:14:12.340: E/AndroidRuntime(13847): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:280)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.app.Activity.setContentView(Activity.java:1892)
08-03 13:14:12.340: E/AndroidRuntime(13847): at com.example.pontix.PontixActivity.onCreate(PontixActivity.java:13)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.app.Activity.performCreate(Activity.java:4562)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
08-03 13:14:12.340: E/AndroidRuntime(13847): ... 12 more
08-03 13:14:12.340: E/AndroidRuntime(13847): Caused by: java.lang.IllegalStateException: Fragment com.example.pontix.SideMenu did not create a view.
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:295)
08-03 13:14:12.340: E/AndroidRuntime(13847): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
08-03 13:14:12.340: E/AndroidRuntime(13847): ... 23 more
08-03 13:14:12.355: D/dalvikvm(13847): GC_CONCURRENT freed 152K, 3% free 13194K/13575K, paused 1ms+3ms
08-03 13:14:12.355: D/AbsListView(13847): [unregisterDoubleTapMotionListener]
08-03 13:14:12.355: I/MotionRecognitionManager(13847): .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@419278a8
08-03 13:14:12.355: D/AbsListView(13847): [unregisterDoubleTapMotionListener]
08-03 13:14:12.355: I/MotionRecognitionManager(13847): .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@4192d5e0
08-03 13:14:12.360: D/AbsListView(13847): [unregisterDoubleTapMotionListener]
08-03 13:14:12.360: I/MotionRecognitionManager(13847): .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@41963500

最佳答案

您的两个 fragment 都需要公共(public)空构造函数:

public class SideMenu extends ListFragment
{
public SideMenu(){}
...

public class ContentPreview extends Fragment
{
public ContentPreview(){}
...

关于android - 改变方向时 fragment 膨胀错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11799602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com