在android中使用片段来显示视图

时间:2013-10-31 11:53:15

标签: android android-fragments

我正在尝试在Android中使用Fragments

  • 这涉及使用片段
  • 使用它的动态方式
  • 这有两项活动

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;
    }

}

日志 ::

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
  • 我只有运行时错误

如何解决此错误?

{编辑}

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();


    }


}

我改变了班级

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;
    }

}

日志 ::

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>

1 个答案:

答案 0 :(得分: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可能不同