由于NullPointerException,无法实例化android.app.Application

时间:2012-01-27 19:35:57

标签: android nullpointerexception

我在Android世界中是一个菜鸟,做一个锻炼的宠物项目。这是一个非常简单的类似提醒的应用程序,只有两个活动。一个是定制的ListView显示现有的警报。其中有一些按钮用于启动另一个按钮,用于添加/编辑警报。它中有一个按钮,可以导致之前的ListView活动。

最近我遇到了一个奇怪的情况。我的应用运行正常。但问题是,每当我触发添加/编辑活动,然后返回到ListView,然后重新运行(或者我应该说重新安装?)应用程序。将弹出一条错误消息。但它只会短暂显示,应用程序将启动。

我在日志中收到的错误消息说:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
at android.app.LoadedApk.makeApplication(LoadedApk.java:482)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3909)
at android.app.ActivityThread.access$1300(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1184)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:362)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:305)
at android.app.LoadedApk.makeApplication(LoadedApk.java:474)
... 11 more

它没有明确指出我的代码在哪里出错。所以我不知道如何纠正它。有没有人遇到类似的问题?任何建议将不胜感激!

以下是添加/修改活动的代码:

public class EditEntry extends Activity
{
    private AutoCompleteTextView foodNameTextView;
    private DatePicker datePicker;
    // store values in AutoCompleteTextView & DatePicker
    private String foodName;
    private Calendar foodDate;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit);
        // dummy selections for AutoCompleteTextView
        String[] foodList = new String[]{"meat", "fruit", "vega"};
        // instantiate AutoCompleteTextView & DatePicker
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.food_list_dropdown, foodList);
        foodNameTextView = (AutoCompleteTextView)findViewById(R.id.foodName);
        foodNameTextView.setAdapter(arrayAdapter);
        datePicker = (DatePicker)findViewById(R.id.date_picker);
        // get intent from ReminderList.
        Intent intent = getIntent();
        // get extras from intent. Return null if intent is sent from "add" action.
        foodName = intent.getStringExtra("foodName");
        foodDate = (Calendar) intent.getSerializableExtra("foodDate");
        // set default values for widgets if it is an "edit" action.
        if (null != foodName)
        {
            foodNameTextView.setText(foodName);
            datePicker.init(foodDate.get(Calendar.YEAR), foodDate.get(Calendar.MONTH), foodDate.get(Calendar.DAY_OF_MONTH), 
                    new OnDateChangedListener()
                    {   // will implement date input check later.
                        @Override
                        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)   {}
                    });
        }

        // Submit will add/modify the data in xml file. Back will start ReminderList activity
        Button submit = (Button)findViewById(R.id.entry_submit);
        Button back = (Button)findViewById(R.id.entry_back);

        submit.setOnClickListener(new SubmitButtonListener());
        back.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent();
                intent.setClass(EditEntry.this, FoodReminderList.class);
                startActivity(intent);
            }
        });
    }

    // add or modify data in xml file
    @SuppressWarnings("unused")
    class SubmitButtonListener implements OnClickListener
    {
        Calendar foodDate = Calendar.getInstance();

        @Override
        public void onClick(View v)
        {
            XmlUtil xmlUtil = new XmlUtil();
            // determine if it is an "edit" action.
            if (null != foodName)
            {
                FoodInfo foodInfo = new FoodInfo(foodName, foodDate);
                // delete the old data entry
                xmlUtil.deleteEntry(foodInfo);
                // cancel old alarm
                FoodReceiver alarm = new FoodReceiver(EditEntry.this, foodDate, false);
            }
            // get new input values 
            foodName = foodNameTextView.getText().toString();
            foodDate.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 0, 0, 0);
            // update xml file
            FoodInfo foodInfo = new FoodInfo(foodName, foodDate);
            xmlUtil.updateEntry(foodInfo);
            // set new alarm
            FoodReceiver alarm = new FoodReceiver(EditEntry.this, foodDate, true);
            // popup toast confirming the submit
            Toast.makeText(EditEntry.this, "Reminder Added", Toast.LENGTH_SHORT).show();
            // clear widgets
            foodNameTextView.setText("");
            Calendar currentDate = Calendar.getInstance();
            datePicker.updateDate(currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH));
        }
    }
}

Manifest是这样的:

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

<uses-sdk android:minSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".FoodReminderList" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:label="@string/app_name"
        android:name=".EditEntry" >
    </activity>
    <activity 
        android:label='@string/app_name'
        android:name=".FoodReminder"></activity>

    <receiver android:name="ca.maxiao.Food.FoodReceiver">
        <intent-filter>
            <action android:name="Alarm_Setting" />
        </intent-filter>
    </receiver>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
</manifest>

3 个答案:

答案 0 :(得分:3)

我有完全相同的错误,尽管其他代码比你的更简单。我发现只有在从eclipse重新运行它后才在仿真器中关闭应用程序时才会出现问题。因此,如果我在从eclipse运行之前关闭它,一切正常。但由于我对android很新,我不知道为什么会这样。我以前的测试应用程序这从来都不是问题。

答案 1 :(得分:2)

我在Back按钮的匿名内部类中做了一些测试并锁定问题,因为每次在重新启动应用程序之前单击此错误都会出现错误。如果我使用手机上的“返回”按钮切换活动,一切都很好。

我在内部类中添加了一行

EditEntry.this.finish();

确实解决了这个问题。我这次尝试更系统地测试它。假设没有上面一行的代码是A,而这行的代码是B.场景如下:

1.运行A然后A - >;错误

2.运行A然后B - &gt;错误

3.运行B然后A - >;行

4.运行B然后B - &gt;行

因此,我认为这是关于活动堆栈的某种问题?

答案 2 :(得分:1)

实际上,此问题的真正解决方案是文件夹权限问题。确保您在可以修改的文件夹中操作。我通过确保我在一个具有正确权限的文件夹中为我自己解决了同样的问题。