问题描述
我是那种在Android世界小白的,和做一个宠物项目行使。它只有两个活动,一个很简单的提醒般的应用程序。一个是一个定制的ListView显示现有的警报。有在它的一些按钮来启动其他一个,这是对于添加/编辑报警。有一个在它的一个按钮导致previous ListView的活动。
有一个奇怪的情况我遇到了最近。我的应用程序工作正常。但问题是,每当我触发添加/编辑活动,然后回到ListView控件,并重新运行(或者我应该说重新安装?)的应用程序。一个错误信息会弹出。但它只会出现短暂的应用程序将启动。
我赶上了日志中的错误消息说:
致命异常:主要
java.lang.RuntimeException的:无法实例化应用android.app.Application:显示java.lang.NullPointerException
在android.app.LoadedApk.makeApplication(LoadedApk.java:482)
在android.app.ActivityThread.handleBindApplication(ActivityThread.java:3909)
在android.app.ActivityThread.access $ 1300(ActivityThread.java:122)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1184)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4340)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:511)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
在dalvik.system.NativeStart.main(本机方法)
显示java.lang.NullPointerException:产生的原因
在android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:362)
在android.app.LoadedApk.getClassLoader(LoadedApk.java:305)
在android.app.LoadedApk.makeApplication(LoadedApk.java:474)
... 11更多
它并没有明确指出哪里是错误的,我codeS。所以,我没有一个关于如何纠正它的线索。有没有人遇到过类似的问题?任何建议将AP preciated!
下面是codeS添加/编辑活动:
公共类EditEntry扩展活动
{
私人AutoCompleteTextView foodNameTextView;
私人的DatePicker日期选择器;
//在AutoCompleteTextView及放大器存储值;的DatePicker
私人字符串foodName;
私人日历foodDate;
@覆盖
保护无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.edit);
//虚拟选择的AutoCompleteTextView
的String [] foodList =新的String [] {肉,果,织女};
//实例化AutoCompleteTextView和放大器;的DatePicker
ArrayAdapter<字符串> arrayAdapter =新的ArrayAdapter<字符串>(这一点,R.layout.food_list_dropdown,foodList);
foodNameTextView =(AutoCompleteTextView)findViewById(R.id.foodName);
foodNameTextView.setAdapter(arrayAdapter);
日期选择器=(DatePicker的)findViewById(R.id.date_picker);
//从ReminderList获得意图。
意向意图= getIntent();
//从意图获得临时演员。返回null如果意图是从增加的行动派。
foodName = intent.getStringExtra(foodName);
foodDate =(日历)intent.getSerializableExtra(foodDate);
//为小部件设置的默认值,如果它是一个编辑的行动。
如果(NULL!= foodName)
{
foodNameTextView.setText(foodName);
datePicker.init(foodDate.get(Calendar.YEAR),foodDate.get(Calendar.MONTH),foodDate.get(Calendar.DAY_OF_MONTH)
新OnDateChangedListener()
{//将在稍后实施日期输入检查。
@覆盖
公共无效onDateChanged(DatePicker的观点,年整型,诠释monthOfYear,诠释DAYOFMONTH){}
});
}
//提交将在XML文件中添加/修改数据。回到开始ReminderList活动
按钮提交=(按钮)findViewById(R.id.entry_submit);
按钮回到=(按钮)findViewById(R.id.entry_back);
submit.setOnClickListener(新SubmitButtonListener());
back.setOnClickListener(新OnClickListener()
{
@覆盖
公共无效的onClick(视图v)
{
意向意图=新的意图();
intent.setClass(EditEntry.this,FoodReminderList.class);
startActivity(意向);
}
});
}
//添加或修改XML文件中的数据
@燮pressWarnings(未使用)
类SubmitButtonListener实现OnClickListener
{
日历foodDate = Calendar.getInstance();
@覆盖
公共无效的onClick(视图v)
{
XmlUtil xmlUtil =新XmlUtil();
//确定它是否是一个编辑的行动。
如果(NULL!= foodName)
{
FoodInfo foodInfo =新FoodInfo(foodName,foodDate);
//删除旧数据录入
xmlUtil.deleteEntry(foodInfo);
//取消旧报警
FoodReceiver报警=新FoodReceiver(EditEntry.this,foodDate,假);
}
//获取新的输入值
。foodName = foodNameTextView.getText()的toString();
foodDate.set(datePicker.getYear(),datePicker.getMonth(),datePicker.getDayOfMonth(),0,0,0);
//更新XML文件
FoodInfo foodInfo =新FoodInfo(foodName,foodDate);
xmlUtil.updateEntry(foodInfo);
//设置新的警报
FoodReceiver报警=新FoodReceiver(EditEntry.this,foodDate,真正的);
//弹出敬酒确认提交
Toast.makeText(EditEntry.this,添加提醒,Toast.LENGTH_SHORT).show();
//明确的部件
foodNameTextView.setText();
日历的currentdate = Calendar.getInstance();
datePicker.updateDate(currentDate.get(Calendar.YEAR),currentDate.get(Calendar.MONTH),currentDate.get(Calendar.DAY_OF_MONTH));
}
}
}
该清单是这样的:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=ca.maxiao.Food
安卓版code =1
机器人:VERSIONNAME =1.0>
<使用-SDK安卓的minSdkVersion =8/>
<应用
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME>
<活动
机器人:标签=@字符串/ APP_NAME
机器人:名称=FoodReminderList。>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:标签=@字符串/ APP_NAME
机器人:名称=EditEntry。>
< /活性GT;
<活动
机器人:标签='@字符串/ APP_NAME
机器人:名称=FoodReminder。>< /活性GT;
<接收器的Android版本:NAME =ca.maxiao.Food.FoodReceiver>
<意向滤光器>
<作用机器人:名称=Alarm_Setting/>
&所述; /意图滤光器>
< /接收器>
< /用途>
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
<使用-权限的Android:名称=android.permission.VIBRATE/>
< /舱单>
我做了一些更多的测试和lockon在匿名内部类后退按钮的问题,因为会出现错误,每当我重新启动应用程序之前单击此。如果我切换使用手机上的返回按钮活动,一切都很好。
我在内部类中增加了一个行
EditEntry.this.finish();
它确实解决问题。我想测试它更系统这个时候。假设没有上述行code是A,而code这条线是B.情景是这样的后续
1.运行A,则A - >错误
2.运行A则B - >错误
3.run B,则A - >确定
4.run B,则乙 - >确定
所以,我认为这是某种形式的问题有关的活动栈?
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>
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?
这篇关于android.app.Application不能因NullPointerException异常实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!