Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.app.Application cannot be instantiated due to NullPointerException

I'm kind of a noob in the android world, and doing a pet project for exercising. It's a very simple reminder-like app with just two activities. One is a customized ListView display the existing alarms. There are some buttons in it to start the other one, which is for add/edit alarms. There is a button in it lead to the previous ListView activity.

There is a weird situation I ran into recently. My app works fine. But the problem is, whenever I trigger the add/edit activity then go back to the ListView, and re-run(or I should say re-install?) the app. An error message will popup. But it will only show up briefly and the the app will start.

The error message I caught in the log says:

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

It doesn't explicitly point out where is wrong in my codes. So I don't have a clue about how to correct it. Does anyone encountered similar problem? Any suggestion will be appreciated!

Here is codes of add/edit activity:

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

The Manifest goes like this:

<?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>
like image 203
Selkie Avatar asked Jan 27 '12 19:01

Selkie


3 Answers

I had exactly the same error, although with other and more simpler code than yours. I found out that the problem occurred only if I didn't close the app in the emulator after re-run it from eclipse. So if I close it before run it from eclipse, everything works fine. But since I am really new to android, I have no clue why this is so. I previous test apps this was never a problem.

like image 195
Valentino Ru Avatar answered Oct 21 '22 19:10

Valentino Ru


I did some more tests and lockon the problem in the anonymous inner class for Back button, since the error will appear whenever I clicked this before restarting the app. If I switch activities using the "go back" button on the phone, everything is fine.

I added one line in the inner class

EditEntry.this.finish();

It does solve the problem. I tried to test it more systematical this time. Let's say the code without the above line is A, and the code with this line is B. Scenario goes like follow

1.run A then A --> Error

2.run A then B --> Error

3.run B then A --> OK

4.run B then B --> OK

Therefore, I assume it is some kind of problem about the activity stack?

like image 22
Selkie Avatar answered Oct 21 '22 17:10

Selkie


Actually, the real solution to this problem is a folder permissions issue. Make sure you are operating in a folder that you can modify. I just fixed that same problem for my self by making sure I was in a folder with the right permissions.

like image 43
GeekedOut Avatar answered Oct 21 '22 17:10

GeekedOut