0

I am trying to use Fragments In Android

  • This involves using fragments
  • Dynamic way of using it
  • This has two activities

MainActivity.java

package com.example.simpledynamicfragment;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity{

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

        Fragment frag=new Fragment();
        FragmentManager FM = getSupportFragmentManager();       
        FragmentTransaction transaction=FM.beginTransaction();
        transaction.add(R.layout.fragment,frag,"fragment1");
        transaction.commit();


    }


}

FragmentActivity.java

package com.example.simpledynamicfragment;

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


public class FragmentActivity extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view=inflater.inflate(R.layout.fragment, container, false);
        return view;
    }

}

Log::

10-31 17:17:41.927: E/AndroidRuntime(570): FATAL EXCEPTION: main
10-31 17:17:41.927: E/AndroidRuntime(570): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simpledynamicfragment/com.example.simpledynamicfragment.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f030001 (com.example.simpledynamicfragment:layout/fragment) for fragment Fragment{45fc8ee0 #0 id=0x7f030001 fragment1}
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.os.Looper.loop(Looper.java:123)
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-31 17:17:41.927: E/AndroidRuntime(570):  at java.lang.reflect.Method.invokeNative(Native Method)
10-31 17:17:41.927: E/AndroidRuntime(570):  at java.lang.reflect.Method.invoke(Method.java:521)
10-31 17:17:41.927: E/AndroidRuntime(570):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-31 17:17:41.927: E/AndroidRuntime(570):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-31 17:17:41.927: E/AndroidRuntime(570):  at dalvik.system.NativeStart.main(Native Method)
10-31 17:17:41.927: E/AndroidRuntime(570): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f030001 (com.example.simpledynamicfragment:layout/fragment) for fragment Fragment{45fc8ee0 #0 id=0x7f030001 fragment1}
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.app.Activity.performStart(Activity.java:3781)
10-31 17:17:41.927: E/AndroidRuntime(570):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
10-31 17:17:41.927: E/AndroidRuntime(570):  ... 11 more
  • I have only run time errors

How to resolve this error?

{EDIT}

MainActivity.java

package com.example.simpledynamicfragment;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity{

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

        My_Fragment  frag=new My_Fragment();
        FragmentManager FM = getSupportFragmentManager();       
        FragmentTransaction transaction=FM.beginTransaction();
        transaction.add(R.layout.fragment_container,frag,"fragment1");
        transaction.commit();


    }


}

I have changed the class

My_Fragment.java

public class My_Fragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view=inflater.inflate(R.layout.fragment, container, false);
        return view;
    }

}

Log::

10-31 17:31:13.037: E/AndroidRuntime(665): FATAL EXCEPTION: main
10-31 17:31:13.037: E/AndroidRuntime(665): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simpledynamicfragment/com.example.simpledynamicfragment.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f030001 (com.example.simpledynamicfragment:layout/fragment) for fragment My_Fragment{45fc91a0 #0 id=0x7f030001 fragment1}
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.os.Looper.loop(Looper.java:123)
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-31 17:31:13.037: E/AndroidRuntime(665):  at java.lang.reflect.Method.invokeNative(Native Method)
10-31 17:31:13.037: E/AndroidRuntime(665):  at java.lang.reflect.Method.invoke(Method.java:521)
10-31 17:31:13.037: E/AndroidRuntime(665):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-31 17:31:13.037: E/AndroidRuntime(665):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-31 17:31:13.037: E/AndroidRuntime(665):  at dalvik.system.NativeStart.main(Native Method)
10-31 17:31:13.037: E/AndroidRuntime(665): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f030001 (com.example.simpledynamicfragment:layout/fragment) for fragment My_Fragment{45fc91a0 #0 id=0x7f030001 fragment1}
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.app.Activity.performStart(Activity.java:3781)
10-31 17:31:13.037: E/AndroidRuntime(665):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
10-31 17:31:13.037: E/AndroidRuntime(665):  ... 11 more

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/Yellow"
    android:id="@+id/main_layout" >



</RelativeLayout>
4

1 回答 1

1

改变这个

  public class FragmentActivity extends Fragment{

  public class MyFragment extends Fragment{ // as pointed by Ed George

回答这个

  Fragment frag=new Fragment();

  MyFragment frag=new MyFragment();

也改变这个

  transaction.add(R.layout.fragment,frag,"fragment1");

  transaction.add(R.id.fragment_container,frag,"fragment1");

fragment_container 是您添加片段的容器,在您的情况下 id 可能不同

于 2013-10-31T11:57:55.813 回答