[英]Exception when I run my application from Eclipse
I've been having this problem for almost 2 months now and can't figure it out. 我已经有这个问题差不多2个月了,无法解决这个问题。 The problem is that if my application is running and I run (reinstall) my application from Eclipse, I get an error message indicating that my application has crashed 'Unfortunately, has stopped.'.
问题是,如果我的应用程序正在运行并且我从Eclipse运行(重新安装)我的应用程序,我会收到一条错误消息,指出我的应用程序崩溃了“不幸的是,已经停止了。”。 I notice that it also occurs when I run it away from my PC/Eclipse, I think that it happens only after I don't run it for a while.
我注意到当我从PC / Eclipse运行它时也会发生这种情况,我认为只有在我暂时不运行它之后才会发生这种情况。
It only occurs if the app is active in the 3rd activity ( BaseDiagramActivity
) and then I run the app again from Eclipse. 只有当应用程序在第3个活动(
BaseDiagramActivity
)中处于活动BaseDiagramActivity
,然后我再次从Eclipse运行应用程序时才会出现此问题。 I've stripped out basically all the application except the 3 activities and It's still happening. 除了3个活动之外,我基本上已经删除了所有应用程序,而且它仍在发生。
I've searched and searched for a solution to this problem but can't find any good answer or one that applies to me. 我搜索并搜索了这个问题的解决方案,但找不到任何好的答案或适用于我的答案。
It doesn't seem like a hardware or android version issue as I'm running this on my tablet (4.0.3) and my phone (4.0.2, was happening on 4.0.1 before update). 它似乎不是硬件或Android版本问题因为我在我的平板电脑(4.0.3)和我的手机(4.0.2,在更新前发生在4.0.1上)运行它。 Unless of course it is an ice cream sandwich bug.
除非当然是冰淇淋夹心虫。
Let me know if any more info is required. 如果需要更多信息,请告诉我。
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:3938)
at android.app.ActivityThread.access$1300(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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
LoadedApk.initializeJavaContextClassLoader() - Line 362 seems to be the offender LoadedApk.initializeJavaContextClassLoader() - 362行似乎是罪犯
Below are the relevant files: 以下是相关文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="[my package]"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="HomeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="LoadDiagramActivity" android:label="Load Diagram"></activity>
<activity android:name="BaseDiagramActivity" android:label="Base Diagram"></activity>
</application>
</manifest>
public class HomeActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button diagramButton = (Button)findViewById(R.id.diagram);
diagramButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(HomeActivity.this, LoadDiagramActivity.class));
}
});
}
}
public class LoadDiagramActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.load_diagram_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.add_new_diagram:
startActivity(new Intent(this, BaseDiagramActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
it doesn't actually matter what activity this is, the exception occurs as long as a 'third' activity is started (or clicking the add button on LoadDiagramActivity
. 实际上这是什么活动并不重要,只要启动“第三个”活动(或单击
LoadDiagramActivity
上的添加按钮)就会发生LoadDiagramActivity
。
public class BaseDiagramActivity extends Activity {
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/diagram"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Diagram" />
</LinearLayout>
When I stripped down my project in order to ask a simpler answer, I moved everything into the package's namespace. 当我删除我的项目以便提出更简单的答案时,我将所有内容都移到了包的命名空间中。 In the actual project there are 5 namespaces, they were still present when I was testing with the stripped down version however just not called (as far as I could see).
在实际项目中有5个名称空间,当我使用精简版本进行测试时它们仍然存在但是没有被调用(据我所见)。
Here are the packages: 这是包:
[package]
- general logic [package]
- 一般逻辑 [package].activities
- all activities and base activities [package].activities
- 所有活动和基础活动 [package].database
- all interaction with the database [package].database
- 与数据库的所有交互 [package].models
- models for saving/loading data [package].models
- 用于保存/加载数据的模型 [package].renderables
- objects drawn to a canvas [package].renderables
- 绘制到画布的对象 I have tried to add an `android:sharedUserId' attribute to the manifest and and it seemed to do nothing both times I tried. 我试图在清单中添加一个`android:sharedUserId'属性,而且在我试过的时候似乎什么也没做。 When I was initially investigating this I came to the conclusion that the shared user id only applied to different projects, not different packages.
当我最初调查这个时,我得出结论,共享用户ID仅适用于不同的项目,而不是不同的包。
Also I don't believe there was any interaction with the database when I stripped everything down. 当我剥离所有内容时,我也不相信与数据库有任何交互。 The fact that the 3rd activity could be any activity, even HomeActivity, was something against this theory.
事实上,第三项活动可能是任何活动,甚至是HomeActivity,这也违背了这一理论。
Last couple of days I've jumped back into this project, I created a brand new project in Eclipse Juno (was on Helios before) and transferred everything over manually so that Eclipse and Android tools handled almost all of the Manifest interaction but it's still occurring. 最近几天我又回到了这个项目,我在Eclipse Juno中创建了一个全新的项目(之前是在Helios上)并且手动转移所有内容,以便Eclipse和Android工具几乎处理所有Manifest交互,但它仍然在发生。 I will look at it a bit more over the next few days and update if I find anything.
在接下来的几天里,我会更多地看一下,如果我找到任何东西,我会更新。
FYI my new project is targeting the following: 仅供参考我的新项目针对以下内容:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
The new project structure also has all the activities in the root package now (ie. [package], not [package].activities). 新项目结构现在还包含根包中的所有活动(即[package],而不是[package] .activities)。 I'm also using the (new?) syntax to show the parent activity:
我也使用(new?)语法来显示父活动:
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="[my package].LoadDiagramActivity" />
It is also still occurring on my now updated Galaxy Nexus running Jellybean 4.1.2. 它仍然出现在我现在更新的运行Jellybean 4.1.2的Galaxy Nexus上。
Try adding one more thing in Manifest file, I am sure you have already tried.. 尝试在Manifest文件中添加一个东西,我相信你已经尝试过..
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="[my package]"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="com.mj.app" >
It seems like in your project you have multiple PACKAGES, like com.package.p1 , com.package.p2 ,com.package.p3
.. 在你的项目中你似乎有多个包,比如
com.package.p1 , com.package.p2 ,com.package.p3
..
And while starting you are in p1
and then moves on to p2 - p3
... and at that time you try to run it again..so the Android gives you error. 在开始的时候你在
p1
然后继续前进到p2 - p3
......当时你试着再次运行它......所以Android会给你错误。
If you put 13
in your minSdkVersion
, it should work. 如果你在你的
minSdkVersion
13
,它应该可以工作。
Either that, or you need to setDisplayHomeAsUpEnabled(false)
in the onCreate()
of your other activities. 要么是这样,要么你需要在其他活动的
onCreate()
中设置setDisplayHomeAsUpEnabled(false)
。
Both those solutions should work. 这些解决方案都应该有效。
Starting at API Level 14 according to the documentation , the setHomeButtonEnabled(true)
is no longer done for you by default, and it goes on to say that 根据文档从API级别14开始,默认情况下不再为您执行
setHomeButtonEnabled(true)
,并继续说
Setting the DISPLAY_HOME_AS_UP display option will automatically enable the home button.
设置DISPLAY_HOME_AS_UP显示选项将自动启用主页按钮。
So we can infer that setDisplayHomeAsUpEnabled(false)
works in a similar way as setHomeButtonEnabled(false)
所以我们可以推断
setDisplayHomeAsUpEnabled(false)
工作方式与setHomeButtonEnabled(false)
相似
If I am not mistaken, this only happens when we reinstall the app from Eclipse Run->As. 如果我没有记错,这只会在我们从Eclipse Run-> As重新安装应用程序时发生。 So this is unlikely to happen when a user upgrades through Play.
因此,当用户通过Play升级时,这不太可能发生。 I can say this with confidence since I did notice my app too with this exception during reinstall through Eclipse, but nothing on Crittercism.
我可以自信地说这个,因为在通过Eclipse重新安装期间我确实注意到我的应用程序这个例外,但在Crittercism上没有任何内容。
To fix this, I worked on resolving memory consumption of my app. 为了解决这个问题,我致力于解决我的应用程序的内存消耗问题。
bitmap.recycle(); bitmap = null;
bitmap.recycle(); bitmap = null;
bitmap.recycle(); bitmap = null;
in your onDestroy and before all System.exit(0). Android does a lot of background work, and reinstallation is a abrupt cleanup expectation from the app. Android做了很多后台工作,重新安装是应用程序的突然清理期望。 Memory not cleaned up can cause issues.
内存未清理可能会导致问题。 This exception is one of those
internal
ones. 这个例外就是其中的一个
internal
的。 So dont think of it to be straightforward. 所以不要认为它是直截了当的。
Wait after your application crashes when u run it second time. 等待第二次运行应用程序崩溃后等待。 The app will launch after the crashing again.
应用程序将在崩溃后再次启动。 This happens sometimes with me.
这有时发生在我身上。 If the same is the case with u dont forget to clean your project everytime before u run it.
如果情况相同,你不要忘记每次运行之前清理你的项目。
By inspecting your code I feel you are skipping to set "setContentView(rid)"
in LoadDiagramActivity
. 通过检查您的代码,我觉得您正在跳过在
LoadDiagramActivity
设置"setContentView(rid)"
。 Please try setting the view in onCreate()
. 请尝试在
onCreate()
设置视图。
Hope this will help you. 希望这会帮助你。
You need to append "." 你需要追加“。” before the
activity
name in Menifest
. 在
Menifest
的activity
名称Menifest
。
Like this 像这样
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".HomeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoadDiagramActivity" android:label="Load Diagram"></activity>
<activity android:name=".BaseDiagramActivity" android:label="Base Diagram"></activity>
</application>
I update your menifest's activity. 我更新你的menifest的活动。 Please check it...
请检查一下...
There is nothing wrong with the code you've given us so far. 到目前为止,您给我们的代码没有任何问题。 I just tried it in a new project with the package name com.test and it worked flawlessly.
我刚刚在一个名为com.test的软件包的新项目中尝试过,它运行得很完美。 The only thing I added in the files were the package declaration.
我在文件中添加的唯一内容是包声明。 (And I added the load_diagram_menu.xml, of course.)
(当然,我添加了load_diagram_menu.xml。)
If possible, it would be great if you could give us access to the complete project. 如果可能的话,如果您能让我们访问完整的项目,那就太棒了。 If the project is working on someone else's computer, it's most likely Eclipse or the Android Eclipse plugin-in that are misbehaving.
如果项目正在其他人的计算机上工作,那么Eclipse或Android Eclipse插件很可能是行为不端的。 A clean install of Eclipse would solve that.
干净的Eclipse安装可以解决这个问题。 (Though I understand how being offline could make this difficult. :-) )
(虽然我知道如何离线会让这很困难。:-))
For this, be sure to have the "Build Automatically" checked when cleaning the project. 为此,请确保在清理项目时选中“自动构建”。 (Project -> Build Automatically)
(项目 - >自动构建)
You haven't added the period to the names of your activities in manifest's android:name
attributes. 您还没有在manifest的
android:name
属性中将句点添加到您的活动android:name
。 Perhaps, this causes the problem. 也许,这会导致问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.