I use Android Studio to build android app.

My problem is: when fragment change, app crash.

Console output [Console]

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)

Container layout code [container.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 layout code [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>

Main activity code [MainActivity.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 next layout code [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 code [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);
    }

}
有帮助吗?

解决方案

There is no view with id fragment_next in container.xml.

Your FrameLayout is the container which has id android:id="@+id/container". So on lcik if you want to replace with a new one you should use the same id.

Change to

 changefragment(R.id.container, new FragmentNext());
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top