从 Eclipse 运行我的应用程序时出现异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9746116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 17:50:40  来源:igfitidea点击:

Exception when I run my application from Eclipse

androideclipseandroid-intentandroid-4.0-ice-cream-sandwich

提问by Daniel Imms

I've been having this problem for almost 2 months now and can't figure it out. 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.'. 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.

我已经遇到这个问题将近 2 个月了,但无法弄清楚。问题是,如果我的应用程序正在运行并且我从 Eclipse 运行(重新安装)我的应用程序,我会收到一条错误消息,指出我的应用程序已崩溃“不幸的是,已停止。”。我注意到当我从我的 PC/Eclipse 运行它时它也会发生,我认为它只有在我不运行它一段时间后才会发生。

It only occurs if the app is active in the 3rd activity (BaseDiagramActivity) and then I run the app again from Eclipse. I've stripped out basically all the application except the 3 activities and It's still happening.

仅当应用程序在第三个活动 ( BaseDiagramActivity)中处于活动状态时才会发生,然后我再次从 Eclipse 运行该应用程序。除了 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). Unless of course it is an ice cream sandwich bug.

这似乎不是硬件或安卓版本问题,因为我在平板电脑(4.0.3)和手机(4.0.2,更新前在 4.0.1 上运行)。当然,除非它是冰淇淋三明治的虫子。

Let me know if any more info is required.

如果需要更多信息,请告诉我。

The exception (Tag=AndroidRuntime)

异常(Tag=AndroidRuntime)

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.access00(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

The Android Code

安卓代码

LoadedApk.initializeJavaContextClassLoader()- Line 362 seems to be the offender

LoadedApk.initializeJavaContextClassLoader()- 第 362 行似乎是违规者

Below are the relevant files:

以下是相关文件:

AndroidManifest.xml

AndroidManifest.xml

<?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>

HomeActivity.java

家庭活动.java

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

LoadDiagramActivity.java

LoadDiagramActivity.java

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

BaseDiagramActivity.java

基础图表活动.java

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.

public class BaseDiagramActivity extends Activity {
}

home.xml

主页.xml

<?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>

Additional information

附加信息

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].activities- all activities and base activities
  • [package].database- all interaction with the database
  • [package].models- models for saving/loading data
  • [package].renderables- objects drawn to a canvas
  • [package]- 一般逻辑
  • [package].activities- 所有活动和基地活动
  • [package].database- 与数据库的所有交互
  • [package].models- 用于保存/加载数据的模型
  • [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. When I was initially investigating this I came to the conclusion that the shared user id only applied to different projects, not different packages.

我试图向清单中添加一个 `android:sharedUserId' 属性,但我尝试的两次似乎都没有做任何事情。当我最初对此进行调查时,我得出的结论是共享用户 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,这一事实与该理论背道而驰。

Useful links

有用的链接

Update 1/11/2012

2012 年 1 月 11 日更新

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. I will look at it a bit more over the next few days and update if I find anything.

最近几天我又跳回了这个项目,我在 Eclipse Juno 中创建了一个全新的项目(之前在 Helios 上)并手动转移所有内容,以便 Eclipse 和 Android 工具处理几乎所有的 Manifest 交互,但它仍在发生. 在接下来的几天里,我会多看看它,如果我发现任何东西,我会更新。

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). I'm also using the (new?) syntax to show the parent activity:

新的项目结构现在也有根包中的所有活动(即[包],而不是[包].活动)。我还使用(新的?)语法来显示父活动:

<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 上。

回答by MKJParekh

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..

在您的项目中,您似乎有多个 PACKAGES,例如com.package.p1 , com.package.p2 ,com.package.p3..

And while starting you are in p1and 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会给你错误。

回答by Siddharth

If I am not mistaken, this only happens when we reinstall the app from Eclipse Run->As. So this is unlikely to happen when a user upgrades through 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 Run->As 重新安装应用程序时发生。因此,当用户通过 Play 升级时,这不太可能发生。我可以自信地说这句话,因为在通过 Eclipse 重新安装期间我也注意到我的应用程序有这个异常,但在 Crittercism 上没有。

To fix this, I worked on resolving memory consumption of my app.

为了解决这个问题,我致力于解决我的应用程序的内存消耗问题。

  1. If you only have 1 set of drawables, then change that. Create a drawables-mdpi and copy all the file from that 1 drawables folder (drawables, drawable-ldpi). If you have just 1 set, Android will resize it for its use on bigger screens, thus internally taking up too much memory, and cleanup of this kind of memory (bitmap) is bug prone. Sound crazy, but does wonders. Dont believe me, run it under Heap memory usage watch before and after this change, you will notice that your app is taking 25% less memory for a common scenario.
  2. If you are doing any Bitmap operations, you may want to consider downscaling. Atleast when you are setting options you definitely want to downscale it. Check hereand here. Search or downscaleing.
  3. Finally dont forget to bitmap.recycle(); bitmap = null;in your onDestroy and before all System.exit(0).
  1. 如果您只有 1 组可绘制对象,请更改它。创建一个 drawables-mdpi 并复制该 1 个 drawables 文件夹(drawables、drawable-ldpi)中的所有文件。如果你只有 1 个集合,Android 会调整它的大小以便在更大的屏幕上使用,因此在内部占用太多内存,并且清理这种内存(位图)很容易出错。听起来很疯狂,但确实有奇迹。不要相信我,在此更改前后在 Heap memory Usage watch 下运行它,您会注意到您的应用程序在常见情况下占用的内存减少了 25%。
  2. 如果您正在执行任何位图操作,您可能需要考虑缩小。至少在设置选项时,您肯定希望缩小它的规模。检查这里这里。搜索或缩小。
  3. 最后不要忘记bitmap.recycle(); bitmap = null;在你的 onDestroy 和所有 System.exit(0) 之前。

Android does a lot of background work, and reinstallation is a abrupt cleanup expectation from the app. Memory not cleaned up can cause issues. This exception is one of those internalones. So dont think of it to be straightforward.

Android 做了很多后台工作,重新安装是对应用程序的突然清理期望。未清理的内存可能会导致问题。这个例外就是其中之一internal。所以不要认为它很简单。

回答by Stephan Branczyk

If you put 13in your minSdkVersion, it should work.

如果你把13你的minSdkVersion,它应该工作。

Either that, or you need to setDisplayHomeAsUpEnabled(false)in the onCreate()of your other activities.

要么那样,要么你需要setDisplayHomeAsUpEnabled(false)onCreate()你的其他活动中。

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)

回答by a.ch.

You haven't added the period to the names of your activities in manifest's android:nameattributes. Perhaps, this causes the problem.

您尚未在清单的android:name属性中将句点添加到您的活动名称中。也许,这会导致问题。

回答by Chandrashekhar

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 Ajay Kumar Meher

By inspecting your code I feel you are skipping to set "setContentView(rid)"in LoadDiagramActivity. Please try setting the view in onCreate().

通过检查你的代码,我觉得你跳到设定"setContentView(rid)"LoadDiagramActivity。请尝试在 中设置视图onCreate()

Hope this will help you.

希望这会帮助你。

回答by V.J.

You need to append "." before the activityname in 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. Please check it...

我更新了你的menifest 活动。请检查一下...

回答by Albin

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. The only thing I added in the files were the package declaration. (And I added the load_diagram_menu.xml, of course.)

到目前为止,您提供给我们的代码没有任何问题。我刚刚在一个包名为 com.test 的新项目中尝试了它,它完美地工作。我在文件中添加的唯一内容是包声明。(当然,我添加了 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. A clean install of Eclipse would solve that. (Though I understand how being offline could make this difficult. :-) )

如果可能的话,如果您能让我们访问完整的项目,那就太好了。如果该项目在其他人的计算机上运行,​​则很可能是 Eclipse 或 Android Eclipse 插件出现了问题。全新安装 Eclipse 可以解决这个问题。(虽然我理解离线可能会使这变得困难。:-))

回答by David Quesada

For this, be sure to have the "Build Automatically" checked when cleaning the project. (Project -> Build Automatically)

为此,请确保在清理项目时选中“自动构建”。(项目 -> 自动构建)