gpt4 book ai didi

Android Fragments 变化

转载 作者:行者123 更新时间:2023-11-29 21:19:08 26 4
gpt4 key购买 nike

我使用 Android Studio 构建 Android 应用。

我的问题是:当 fragment 改变时,应用程序崩溃。

控制台输出[控制台]

01-06 18:35:21.952  27756-27756/pl.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: pl.example.app, PID: 27756
java.lang.IllegalArgumentException: No view found for id 0x7f07003d (pl.example.app:id/fragment_next) for fragment FragmentNext{4201c798 #1 id=0x7f07003d}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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 dalvik.system.NativeStart.main(Native Method)

容器布局代码[容器.xml]

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="pl.example.app.MainActivity"
tools:ignore="MergeRootFrame" >

</FrameLayout>

fragment Activity 布局代码[ fragment Activity .xml]

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="pl.example.app.MainActivity$PlaceholderFragment">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="onButtonClick"
android:layout_gravity="center"/>

</FrameLayout>

主要 Activity 代码[主 Activity .java]

package pl.example.app;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends FragmentActivity {

private FragmentManager fragmentManager = null;
private FragmentTransaction fragmentTransaction = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.container);

if (savedInstanceState == null) {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, new Container());
fragmentTransaction.commit();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

public static class Container extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_activity, container, false);
}

}

public void onButtonClick(View view) {
changefragment(R.id.fragment_next, new FragmentNext());
}

private void changefragment(int fragmentID, Fragment fragment) {
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(fragmentID, fragment);
fragmentTransaction.commit();
}
}

fragment 下一个布局代码[ fragment _next.xml]

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="pl.example.app.MainActivity"
tools:ignore="MergeRootFrame">

<TextView
android:text="Fragment Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>

</RelativeLayout>

fragment 代码[FragmentNext.java]

package pl.example.app;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* Created by Nerus on 06.01.14.
*/
public class FragmentNext extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_next, container, false);
}

}

最佳答案

container.xml 中没有id为fragment_next的 View 。

您的FrameLayout 是具有id android:id="@+id/container" 的容器。所以在 lcik 上,如果你想用一个新的替换,你应该使用相同的 id。

更改为

 changefragment(R.id.container, new FragmentNext());

关于Android Fragments 变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20956107/

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