扩展类片段时出错在测试片段Activity时

时间:2016-08-08 08:46:08

标签: java android android-studio android-fragments android-fragmentactivity

我正在尝试测试实施Activity的{​​{1}}。

当我尝试运行我的应用时出现错误:

  

错误扩充类片段

如果你扩展Fragment而不是主Activity中的FragmentActivity,我会读到它,但我在活动本身中继承了正确的东西。 可能是什么问题?

我的活动:

Activity

活动布局:

package apps.radwin.zxingprojectfragmenttwo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;

public class MainActivity extends FragmentActivity {

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

    public void selectFrag(View view) {
        Fragment objFragment = new FragmentOne();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
            .replace(R.id.fragment_Two_Id, objFragment)
            .commit();
    }
}

完整堆栈跟踪:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="apps.radwin.zxingprojectfragmenttwo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView6" />


    <fragment
        android:id="@+id/fragment_One_Id"
        android:name="apps.radwin.zxingprojectfragmenttwo.FragmentOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment_fragment_one"
        android:layout_below="@+id/fragment_Two_Id"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />


    <fragment
        android:id="@+id/fragment_Two_Id"
        android:name="apps.radwin.zxingprojectfragmenttwo.FragmentTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment_fragment_two"
        android:layout_below="@+id/textView6"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity Button Change First"
        android:id="@+id/button2"
        android:onClick="selectFrag"
        android:layout_below="@+id/fragment_One_Id"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity Button Change Second"
        android:id="@+id/button1"
        android:onClick="selectFrag"
        android:layout_below="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />


</RelativeLayout>

我的FragmentOne类:

FATAL EXCEPTION: main
Process: apps.radwin.zxingprojectfragmenttwo, PID: 23530
java.lang.RuntimeException: Unable to start activity ComponentInfo {
    apps.radwin.zxingprojectfragmenttwo / apps.radwin.zxingprojectfragmenttwo.MainActivity
}: android.view.InflateException: Binary XML file line #19: Binary XML file line # 19: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 3349)
at android.app.ActivityThread.access$1100(ActivityThread.java: 221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1794)
at android.os.Handler.dispatchMessage(Handler.java: 102)
at android.os.Looper.loop(Looper.java: 158)
at android.app.ActivityThread.main(ActivityThread.java: 7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 1120)
Caused by: android.view.InflateException: Binary XML file line #19: Binary XML file line # 19: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java: 551)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 429)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 380)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java: 474)
at android.app.Activity.setContentView(Activity.java: 2387)
at apps.radwin.zxingprojectfragmenttwo.MainActivity.onCreate(MainActivity.java: 15)
at android.app.Activity.performCreate(Activity.java: 6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java: 221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1794) 
at android.os.Handler.dispatchMessage(Handler.java: 102) 
at android.os.Looper.loop(Looper.java: 158) 
at android.app.ActivityThread.main(ActivityThread.java: 7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 1120) 
Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:380) 
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474) 
at android.app.Activity.setContentView(Activity.java:2387) 
at apps.radwin.zxingprojectfragmenttwo.MainActivity.onCreate(MainActivity.java:15) 
at android.app.Activity.performCreate(Activity.java:6876) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: java.lang.RuntimeException: apps.radwin.zxingprojectfragmenttwo.MainActivity@44cf550 must implement OnFragmentInteractionListener
at apps.radwin.zxingprojectfragmenttwo.FragmentOne.onAttach(FragmentOne.java:83)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:766)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:527) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:380) 
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474) 
at android.app.Activity.setContentView(Activity.java:2387) 
at apps.radwin.zxingprojectfragmenttwo.MainActivity.onCreate(MainActivity.java:15) 
at android.app.Activity.performCreate(Activity.java:6876) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

1 个答案:

答案 0 :(得分:2)

问题发生在Fragment的{​​{1}}:

onAttach()

您正在检查if (context instanceof OnFragmentInteractionListener) { mListener = (OnFragmentInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } 是否附加Context(本例中为Fragment)是否实现MainActivity,但它没有,因此{{1}抛出。

根据您要实现的目标,从OnFragmentInteractionListener移除上述代码,或者使RuntimeException实施onAttach()