在Android的错误充气类片段 [英] Error inflating class fragment in Android

查看:142
本文介绍了在Android的错误充气类片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发Android程序新我被困在试图完成从介绍片段专业的Andr​​oid 4.0书的练习。我收到以下错误:

I am new at developing Android programs and am stuck on trying to complete an exercise from the Professional Android 4 book that introduces fragments. I get the following error:

InflateException: Binary XML file line #6: Error inflating class fragment

下面是我的主要活动的Java程序:

Below is my main activity java program:

package com.example.todolist;
import java.util.ArrayList;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class MainActivity extends Activity 
implements NewItemFragment.OnNewItemAddedListener {
private ArrayAdapter<String> aa;
private ArrayList<String> todoItems;

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

    // Get references to the Fragments

    FragmentManager fm = getFragmentManager();
    ToDoListFragment todoListFragment =                        
    (ToDoListFragment)fm.findFragmentById(R.id.ToDoListFragment);
  aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
  todoListFragment.setListAdapter(aa);
  @Override
public void onNewItemAdded(String newItem) {
    // TODO Auto-generated method stub
    todoItems.add(newItem);
    aa.notifyDataSetChanged();
}

}

该ToDoListFragment类如下所示:

The ToDoListFragment class is shown below:

package com.example.todolist;
import android.app.ListFragment;

public class ToDoListFragment extends ListFragment{

}

下面是我NewItemFragment类。

Below is my NewItemFragment class.

package com.example.todolist;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;


public class NewItemFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.new_item_fragment, container, false);

    final EditText myEditText = (EditText)view.findViewById(R.id.myEditText);

    myEditText.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
          if(event.getAction() == KeyEvent.ACTION_DOWN)
              if((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||  
                  (keyCode == KeyEvent.KEYCODE_ENTER)) {
                  String newItem = myEditText.getText().toString();
                  onNewItemAddedListener.onNewItemAdded(newItem);
                  myEditText.setText("");
                  return true;
        } 
      return false;

}
    });
    return view;
}

public interface OnNewItemAddedListener {
    public void onNewItemAdded(String newItem);

}

private OnNewItemAddedListener onNewItemAddedListener;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {
        onNewItemAddedListener = (OnNewItemAddedListener)activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString());
    }
}

}

下面是我的activity_main.xml中的文件:

Below is my activity_main.xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.paad.todolist.NewItemFragment"
    android:id="@+id/NewItemFragment" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/addItemHint"

    />
<fragment android:name="com.paad.todolist.ToDoListFragment" 
   android:id="@+id/ToDoListFragment"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"

   />
  </LinearLayout>

最后,这里是我的new_itemFragment xml文件:

Finally, here is my new_itemFragment xml file:

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myEditText" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/addItemHint"
    />

如果谁能告诉我为什么我收到充气片段的错误,我会大大AP preciate它。我试着用进口android.support.v4.app.FragmentManager但没有工作,并尝试使用android.support.v4.app.ListFragment而且也不能工作。我一直在编程多年在其他语言,但Android是真的,到目前为止使用Eclipse对我来说是一个挑战。是他们更好的IDE开发Android项目?

If anyone can tell me why I am getting the inflating fragment error I will greatly appreciate it. I tried using import android.support.v4.app.FragmentManager but that didn't work and try using android.support.v4.app.ListFragment and that didn't work either. I have been programming for many years in other languages but Android is really a challenge for me so far using Eclipse. Are their better IDEs for developing Android projects?

我尝试导入android.support.v4.app片段类,但是当我做getFragmentManager功能没有得到解决。下面是logcat的转储。

I try importing the android.support.v4.app fragment classes but when I do the getFragmentManager function is unresolved. Below is a dump of the logcat.

  02-21 20:53:27.769: E/AndroidRuntime(1280): java.lang.RuntimeException: Unable to     start activity    ComponentInfo{com.example.todolist/com.example.todolist.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
  02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.os.Handler.dispatchMessage(Handler.java:99)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.os.Looper.loop(Looper.java:137)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.app.ActivityThread.main(ActivityThread.java:5039)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at java.lang.reflect.Method.invokeNative(Native Method)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at java.lang.reflect.Method.invoke(Method.java:511)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at dalvik.system.NativeStart.main(Native Method)
 02-21 20:53:27.769: E/AndroidRuntime(1280): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.app.Activity.setContentView(Activity.java:1881)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at com.example.todolist.MainActivity.onCreate(MainActivity.java:20)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.app.Activity.performCreate(Activity.java:5104)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    ... 11 more
 02-21 20:53:27.769: E/AndroidRuntime(1280): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.paad.todolist.NewItemFragment: make sure class name exists, is public, and has an empty constructor that is public
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.app.Fragment.instantiate(Fragment.java:592)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.app.Fragment.instantiate(Fragment.java:560)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.app.Activity.onCreateView(Activity.java:4709)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    ... 21 more
 02-21 20:53:27.769: E/AndroidRuntime(1280): Caused by:        java.lang.ClassNotFoundException: Didn't find class "com.paad.todolist.NewItemFragment" on  path: /data/app/com.example.todolist-1.apk
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
 02-21 20:53:27.769: E/AndroidRuntime(1280):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)

运行,我发现异常在这些指令抛出的调试器:结果
         AA =新ArrayAdapter(这一点,android.R.layout.simple_list_item_1,todoItems);结果
         todoListFragment.setListAdapter(AA);

Running the debugger I found that the exception was thrown at these instructions:
aa = new ArrayAdapter(this, android.R.layout.simple_list_item_1, todoItems);
todoListFragment.setListAdapter(aa);

我注意到,todoItems具有null值,所以我想知道如果该元素与一个值进行初始化。

I noted that todoItems has a value of null so I am wondering if that element has to be initialized with a value.

推荐答案

它寻找一个片段在这里: com.paad.todolist.NewItemFragment

It's looking for a Fragment here: com.paad.todolist.NewItemFragment

这就是你在你的XML文件中。

which is what you have in your xml file.

不过,包的是,实施实际上是在这里:

However, the package that the implementation of that is actually in is in here:

package com.example.todolist;

所以,你需要改变你的XML文件是:

So, you need to change your xml file to be:

<fragment android:name="com.example.todolist.NewItemFragment"

检查 ToDoListFragment 了。

这篇关于在Android的错误充气类片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆