单击按钮放置在片段中时Android应用程序崩溃

时间:2014-11-12 13:28:31

标签: android button android-fragments onclick

在我目前的Android项目中,我有一个片段,其布局(headlines_view.xml)有多个按钮,如下所示:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableTop="@drawable/button_icon"
    android:text="@string/button1"
    android:onClick="openTab1" />

方法openTab1的实现放在从Fragment派生的类HeadlinesFragment中,看起来像这样:

/** Called when the user touches the button */
public void openTab1(View view) {
    // Do something in response to button click
    Tab1Fragment newFragment = new Tab1Fragment();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();

    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack so the user can navigate back
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);

    // Commit the transaction
    transaction.commit();
}

当我运行应用程序并触摸按钮时,应用程序崩溃。我在这里做错了什么?

更新

标题的完整代码片段:

public class HeadlinesFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.headlines_view, container, false);
    }

    /** Called when the user touches the button */
    public void openTab1(View view) {
        // Do something in response to button click
        Tab1Fragment newFragment = new Tab1Fragment();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();
    }

    /** Called when the user touches the button */
    public void openTab2(View view) {
        // Do something in response to button click
        Tab2Fragment newFragment = new Tab2Fragment();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();
    }

    /** Called when the user touches the button */
    public void openTab3(View view) {
        // Do something in response to button click
        Tab3Fragment newFragment = new Tab3Fragment();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();
    }

    /** Called when the user touches the button */
    public void openTab4(View view) {
        // Do something in response to button click
        Tab4Fragment newFragment = new Tab4Fragment();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();
    }

    /** Called when the user touches the button */
    public void openTab5(View view) {
        // Do something in response to button click
        Tab5Fragment newFragment = new Tab5Fragment();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();
    }

}

更新2

我从Fragment类中删除了处理单击的代码并放入了activity类。这是当前的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hello_layout);

    Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Tab1Fragment newFragment = new Tab1Fragment();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();
      }
    });
}

当我在手机中运行应用程序时,我收到了此错误:

E/AndroidRuntime(18494): FATAL EXCEPTION: main
E/AndroidRuntime(18494): Process: org.hello, PID: 18494
E/AndroidRuntime(18494): java.lang.IllegalArgumentException: No view found for id 0x7f070000 (org.hello:id/fragment_container) for fragment Tab1Fragment{4214af38 #2 id=0x7f070000}
E/AndroidRuntime(18494):        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)
E/AndroidRuntime(18494):        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
E/AndroidRuntime(18494):        at android.app.BackStackRecord.run(BackStackRecord.java:684)
E/AndroidRuntime(18494):        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
E/AndroidRuntime(18494):        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
E/AndroidRuntime(18494):        at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(18494):        at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(18494):        at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(18494):        at android.app.ActivityThread.main(ActivityThread.java:5086)
E/AndroidRuntime(18494):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(18494):        at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(18494):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
E/AndroidRuntime(18494):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
E/AndroidRuntime(18494):        at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  916):   Force finishing activity org.hello/.HelloActivity

layout article_view.xml,其中放置了具有id fragment_container的视图:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

2 个答案:

答案 0 :(得分:1)

基于堆栈跟踪的新答案

问题是您的父活动布局必须将容器作为子容器。传递给Fragment事务替换的id必须是活动setContentView

中指定的布局的子级

基本上

您命名为&#34; fragment_container&#34;的容器必须在&#34; R.layout.hello_layout&#34;的布局内您在onCreate中设置为内容视图。然后,您将使用片段替换此容器。

这是一个小例子

这是我主要活动的布局。 activity_main.xml中

    <FrameLayout 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"
   >

    <FrameLayout
        android:id="@+id/activity_main_image_selector_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

这是我的主要活动

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

            setContentView(R.layout.activity_main);

    ImageSelectorFragment imageSelectorFragment = new ImageSelectorFragment();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.activity_main_image_selector_container,imageSelectorFragment,ImageSelectorFragment.class.getName());
    fragmentTransaction.commit();
}

注意activity_main_image_selector_container如何驻留在主活动布局中,并用作替换片段的容器(将片段放在该容器中)。

答案 1 :(得分:0)

您尚未声明R.id.fragment_container!