当我从Eclipse运行我的应用程序时出现异常 [英] Exception when I run my application from Eclipse

查看:212
本文介绍了当我从Eclipse运行我的应用程序时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



它仅当应用程序在第3个活动( BaseDiagramActivity )中处于活动状态时才会发生,然后再从Eclipse运行应用程序。除了3个活动,我已经基本删除了所有的应用程序,它仍在发生。



我搜索并搜索了解决此问题的解决方案,但找不到任何适用于我的好的答案。



我的平板电脑(4.0.3)和我的手机(4.0.2,是在更新前4.0.1发生)。除非当然是冰淇淋三明治臭虫。



如果需要更多信息,请通知我。



异常(Tag = AndroidRuntime)



 致命异常:main 
java.lang.RuntimeException:无法实例化应用程序android.app.Application:java.lang.NullPointerException
在android.app.LoadedApk.makeApplication(LoadedApk.java:482)
在android.app.ActivityThread.handleBindApplication(ActivityThread.java:3938)
at android.app.ActivityThread.access $ 1300(ActivityThread.java:123)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1185)
在android.os.Handler .dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4424)
java.lang.reflect.Method.invokeNative(Native Method)
在java.lang.reflect.Method.invoke(Method.java:511)
在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)
导致:java.lang.NullPointerException
在android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:362)
在android.app.LoadedApk.getClassLoader(LoadedApk.java:305)
at android.app.LoadedApk.makeApplication(LoadedApk.java:474)
... 11更多



Android代码



LoadedApk.initializeJavaContextClassLoader() - 第362行似乎是罪犯



以下是相关文件:



AndroidManifest.xml



 <?xml version =1.0encoding =utf-8?> 
< manifest xmlns:android =http://schemas.android.com/apk/res/android
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 =LoadDiagramActivityandroid:label =Load Diagram>< / activity>
< activity android:name =BaseDiagramActivityandroid:label =Base Diagram>< / activity>
< / application>

< / manifest>



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



  public class LoadDiagramActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState){
super。的onCreate(savedInstanceState);

ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(菜单菜单){
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.load_diagram_menu,menu);
返回true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case android.R.id .home:
finish();
返回true;
case R.id.add_new_diagram:
startActivity(new Intent(this,BaseDiagramActivity.class));
返回true;
default:
return super.onOptionsItemSelected(item);
}
}
}



BaseDiagramActivity.java



这是什么活动并不重要,只要启动第三活动(或单击 LoadDiagramActivity上的添加按钮,就会发生异常

  public class BaseDiagramActivity extends Activity {
}



home.xml



  ;?xml version =1.0encoding =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>



形成



当我删除我的项目以便提出一个更简单的答案时,我将所有内容都移动到包的命名空间中。在实际的项目中,有5个命名空间,当我用剥离的版本测试时,它们仍然存在,但是没有被调用(据我所见)。



以下是软件包:




  • [package] - 一般逻辑

  • [package] .activities - 所有活动和基础活动

  • [package ] .database - 与数据库的所有交互

  • [package] .models - 保存/加载数据

  • [package] .renderables - 绘制到画布的对象



我已经尝试在清单中添加一个android:sharedUserId属性,并且似乎在我尝试过的时候都不做任何事情。当我最初调查这个时候,我得出结论,共享的用户ID只适用于不同的项目,而不是不同的包。



另外我不相信有没有当我把所有东西都掉下来时,与数据库的交互。



有用的链接





更新1/11/2012



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



我的新项目目标是:

 < uses-sdk 
android:minSdkVersion =14
android:targetSdkVersion =15/>

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

 < meta-data 
android:name =android.support.PARENT_ACTIVITY
android:value =[我的包] .LoadDiagramActivity/>

它现在还在我现在更新的Galaxy Nexus上运行Jellybean 4.1.2。

解决方案

尝试在Manifest文件中添加一件事,我相信你已经尝试过了..

 < manifest xmlns:android =http://schemas.android.com/apk/res/android
package =[我的包]
android:versionCode =1
android:versionName =1.0
android:sharedUserId =com.mj.app>

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



开始时你在 p1 然后移动到 p2 - p3 ...
,当时您尝试再次运行它。 Android给你错误。


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.

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.

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.

Let me know if any more info is required.

The exception (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

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

Below are the relevant files:

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

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

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

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.

public class BaseDiagramActivity extends Activity {
}

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

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

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.

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.

Useful links

Update 1/11/2012

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.

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.

解决方案

Try adding one more thing in Manifest file, I am sure you have already tried..

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

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.

这篇关于当我从Eclipse运行我的应用程序时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆