通胀期间申请崩溃

时间:2013-04-10 09:22:04

标签: android android-layout

我正在尝试按照一本书及其示例来学习Android。我陷入了实施涉及碎片的应用程序的困境。此应用程序应作为Todo列表,包含EditText以插入新项目和List以显示它们。

当我启动Application时,我在主Activity中的setContentView()上遇到了问题,我收到消息“不幸的是,TodoList2已停止”。我尝试使用支持库并编译API级别11或更高版本,但我得到了同样的错误。

以下是我在崩溃后看到的logcat:

E/AndroidRuntime(4185): FATAL EXCEPTION: main
E/AndroidRuntime(4185): java.lang.RuntimeException: Unable to start activity        ComponentInfo{it.aledev.todolist2/it.aledev.todolist2.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
E/AndroidRuntime(4185):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
E/AndroidRuntime(4185):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime(4185):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime(4185):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime(4185):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(4185):     at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(4185):     at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime(4185):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(4185):     at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(4185):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(4185):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(4185):     at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(4185): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
E/AndroidRuntime(4185):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
E/AndroidRuntime(4185):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
E/AndroidRuntime(4185):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime(4185):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime(4185):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E/AndroidRuntime(4185):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
E/AndroidRuntime(4185):     at android.app.Activity.setContentView(Activity.java:1881)
E/AndroidRuntime(4185):     at it.aledev.todolist2.MainActivity.onCreate(MainActivity.java:18)
E/AndroidRuntime(4185):     at android.app.Activity.performCreate(Activity.java:5104)
E/AndroidRuntime(4185):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
E/AndroidRuntime(4185):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
E/AndroidRuntime(4185):     ... 11 more
E/AndroidRuntime(4185): Caused by: java.lang.NullPointerException
E/AndroidRuntime(4185):     at it.aledev.todolist2.NewItemFragment.onCreateView(NewItemFragment.java:19)
E/AndroidRuntime(4185):     at android.app.Fragment.performCreateView(Fragment.java:1695)
E/AndroidRuntime(4185):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
E/AndroidRuntime(4185):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
E/AndroidRuntime(4185):     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
E/AndroidRuntime(4185):     at android.app.Activity.onCreateView(Activity.java:4717)
E/AndroidRuntime(4185):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
E/AndroidRuntime(4185):     ... 21 more

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="it.aledev.todolist2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="it.aledev.todolist2.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这里有我的主要活动xml(activity_main.xml):

<LinearLayout 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=".MainActivity"
    android:orientation="vertical"
    >

    <fragment
        android:name="it.aledev.todolist2.NewItemFragment"
        android:id="@+id/new_item_fragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

    <fragment
        android:name="it.aledev.todolist2.ToDoListFragment"
        android:id="@+id/todo_list_fragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        />

</LinearLayout>

这里有我的xml片段包含EditText:

<?xml version="1.0" encoding="utf-8"?>
<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:contentDescription="@string/add_item_content_decription"
    >


</EditText>

它包含一个警告,因为没有指定提示属性,但放置它并不能解决我的问题。

这是MainActivity代码:

package it.aledev.todolist2;

import java.util.ArrayList;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.Menu;
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 fragment manager
        FragmentManager fm=getFragmentManager();
        ToDoListFragment todoListFragment=(ToDoListFragment)fm.findFragmentById(R.id.todo_list_fragment);

        // Create the array list of to do items
        todoItems=new ArrayList<String>();

        // Create the array adapter to bind the array to the ListView
        aa=new ArrayAdapter<String>(this,android.R.id.list,todoItems);

        // Bind the ArrayAdapter to the ListView
        todoListFragment.setListAdapter(aa);

    }

    @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 void onNewItemAdded(String newItem) {
        // TODO Auto-generated method stub
        todoItems.add(newItem);
        aa.notifyDataSetChanged();

    }

}

我可以在onCreate()中调用setContentView(),但执行永远不会到达下一行代码。

这里有与NewFragmentItem相关的代码:

package it.aledev.todolist2;

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

public class NewItemFragment extends Fragment {
    private OnNewItemAddedListener onNewItemAddedListener;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        super.onCreateView(inflater, container, savedInstanceState);
        View view=inflater.inflate(R.layout.new_item_fragment, container, false);
        final EditText myEditText=(EditText)getActivity().findViewById(R.id.myEditText);
        myEditText.setOnKeyListener(new View.OnKeyListener(){

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                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;
    }

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

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

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

我在涉及Google地图的应用程序中发现了类似的问题,但我没有使用地图。所有问题似乎都来自不连贯,例如不使用支持库或在仿真器/设备上使用错误的API。 我正在编译API级别11或更高级别,目标SDK设置为17.我为模拟器设置了API级别17。

我真的不明白我的错误在哪里。 非常感谢你的帮助。

1 个答案:

答案 0 :(得分:5)

更改

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

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

您正在获取该edittext的空指针异常。当您使用view中的edittext扩充xml文件时,需要使用view而不是getActivity()。并确保在drawable文件夹中有ic_launcher图像。它不会被误删除。希望它有所帮助。