简体   繁体   English

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

[英]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. 如果需要更多信息,请告诉我。

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

The Android Code Android代码

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 HomeActivity.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 BaseDiagramActivity.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上的添加按钮)就会发生LoadDiagramActivity

public class BaseDiagramActivity extends Activity {
}

home.xml home.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] - 一般逻辑
  • [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,这也违背了这一理论。

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. 最近几天我又回到了这个项目,我在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. 为了解决这个问题,我致力于解决我的应用程序的内存消耗问题。

  1. If you only have 1 set of drawables, then change that. 如果您只有一组drawable,那么改变它。 Create a drawables-mdpi and copy all the file from that 1 drawables folder (drawables, drawable-ldpi). 创建drawables-mdpi并从该1个drawables文件夹(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. 如果你只有一套,Android会调整它的大小,因为它在更大的屏幕上使用,因此在内部占用太多的内存,并且这种内存(位图)的清理很容易出错。 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. 不要相信我,在此更改之前和之后的堆内存使用情况下运行它,您会注意到您的应用程序的内存减少了25%。
  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 here and here . 点击这里这里 Search or downscaleing. 搜索或缩小范围。
  3. Finally dont forget to bitmap.recycle(); bitmap = null; 最后别忘了bitmap.recycle(); bitmap = null; bitmap.recycle(); bitmap = null; in your onDestroy and before all System.exit(0). 在你的onDestroy和所有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 . Menifestactivity名称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.

相关问题 当我在eclipse中运行我的Android应用程序时,我收到此错误 - When I run my Android application in eclipse, i get this error 运行应用程序(改装)时发生致命异常? - Fatal exception when I run my application (retrofit)? 当我运行我的应用程序时出现未知错误 - unknown error when i run my application 当我尝试从Eclipse导出Android应用程序时aapt.exe崩溃 - aapt.exe crashes when I try to export my Android application from Eclipse 如何从Eclipse运行带有Genymotion的android移动应用程序? - how can i run an android mobile application with Genymotion from eclipse? 当我从 Android Studio 在我的 Android 手机上运行我的应用程序时,它显示空白屏幕 - When I run my application on my Android Phone from Android Studio, it shows blank screen 有没有办法在eclipse上添加我的签名,以便我可以从Eclipse运行我的代码? - Is there any way to add my signature on the eclipse, so that i can run my code from Eclipse? PHP URL在浏览器上正常工作,但是当我尝试从我的android应用程序运行它时InputStream为null - PHP URL is working on broswer but when i tried to run it from my android application InputStream is null 如何从Eclipse在我的活动android设备中运行我开发的android应用程序? - How to run my developed android application in my active android device from eclipse? 我的应用程序布局在我运行时没有出现 - My application layout doesn't appear when i run it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM