膨胀2时,片段给我一个错误

时间:2016-07-22 08:25:10

标签: android android-fragments

我处于片段的学习阶段,所以如果我的问题听起来很愚蠢,那么请放手让它给你建议。

所以我从Click here开发了一个非常简单的应用程序,并在我的其他类文件中发布了一些错误。

接口

     package com.example.fragmentmodularui;

public interface Communicator {
public void respond(int i);}

活动

     package com.example.fragmentmodularui;



import android.support.v7.app.AppCompatActivity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends AppCompatActivity implements Communicator {

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

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

    @Override
    public void respond(int i) {
        // TODO Auto-generated method stub

        FragmentManager manager=getFragmentManager();
        FragmentB f2=(FragmentB) manager.findFragmentById(R.id.fragment_b);
        f2.changeText(i);
    }
}

FragmentA

    package com.example.fragmentmodularui;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FragmentA extends Fragment {
    ListView lst;
    Communicator comm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_a, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        lst = (ListView) getActivity().findViewById(R.id.listView1);
        comm = (Communicator) getActivity();
        ArrayAdapter<String> adap = new ArrayAdapter<String>(getActivity(),
                R.array.title,android.R.layout.simple_list_item_1);
        lst.setAdapter(adap);

        lst.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                comm.respond(position);
            }

        });
    }
}

FragmentB

       package com.example.fragmentmodularui;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class FragmentB extends Fragment {
    EditText edt;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_b, container, false);
        return view;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        edt=(EditText)getActivity().findViewById(R.id.editText1);
    }
    public void changeText(int i)
    {
        String arr[]=getResources().getStringArray(R.array.desc);
        edt.setText(arr[i]);

    }
}

activity.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: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="com.example.fragmentmodularui.MainActivity" >

    <fragment
        android:id="@+id/fragment_a"
        android:name="com.example.fragmentdemo.FragmentA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <fragment
        android:id="@+id/fragment_b"
        android:name="com.example.fragmentdemo.FragmentB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/fragment_a"
        android:layout_marginTop="18dp" />

</RelativeLayout>

fragment_a

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#0000FF" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

fragment_b

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#00FF00" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ems="10" >

        <requestFocus />
    </EditText>

</LinearLayout>

记录错误

    07-22 04:05:03.156: E/AndroidRuntime(7176): FATAL EXCEPTION: main
07-22 04:05:03.156: E/AndroidRuntime(7176): Process: com.example.fragmentmodularui, PID: 7176
07-22 04:05:03.156: E/AndroidRuntime(7176): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentmodularui/com.example.fragmentmodularui.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.os.Looper.loop(Looper.java:136)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.app.ActivityThread.main(ActivityThread.java:5001)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at java.lang.reflect.Method.invokeNative(Native Method)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at java.lang.reflect.Method.invoke(Method.java:515)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at dalvik.system.NativeStart.main(Native Method)
07-22 04:05:03.156: E/AndroidRuntime(7176): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at com.example.fragmentmodularui.MainActivity.onCreate(MainActivity.java:17)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.app.Activity.performCreate(Activity.java:5231)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-22 04:05:03.156: E/AndroidRuntime(7176):     ... 11 more
07-22 04:05:03.156: E/AndroidRuntime(7176): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.fragmentdemo.FragmentA: make sure class name exists, is public, and has an empty constructor that is public
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.app.Fragment.instantiate(Fragment.java:597)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.app.Fragment.instantiate(Fragment.java:561)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.app.Activity.onCreateView(Activity.java:4778)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at com.example.fragmentmodularui.MainActivity.onCreateView(MainActivity.java:1)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
07-22 04:05:03.156: E/AndroidRuntime(7176):     ... 21 more
07-22 04:05:03.156: E/AndroidRuntime(7176): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.fragmentdemo.FragmentA" on path: DexPathList[[zip file "/data/app/com.example.fragmentmodularui-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.fragmentmodularui-1, /system/lib]]
07-22 04:05:03.156: E/AndroidRuntime(7176):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
07-22 04:05:03.156: E/AndroidRuntime(7176):     at android.app.Fragment.instantiate(Fragment.java:583)
07-22 04:05:03.156: E/AndroidRuntime(7176):     ... 27 more

1 个答案:

答案 0 :(得分:1)

您的android:activity.xml中的片段名称似乎与您项目的软件包名称不同?

将activity.xml中的片段更改为

<fragment
        android:id="@+id/fragment_a"
        android:name="com.example.fragmentmodularui.FragmentA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <fragment
        android:id="@+id/fragment_b"
        android:name="com.example.fragmentmodularui.FragmentB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/fragment_a"
        android:layout_marginTop="18dp" />