Android:无法实例化应用程序

时间:2015-04-11 20:43:20

标签: java android eclipse android-manifest

我有一个简单的项目,但是当我开始它时,我收到一个错误。我猜这是Manifest中的一个简单错误,但我无法解决它。 Logcat说无法实例化应用程序。任何帮助表示赞赏。

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.meter"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:name="com.example.meter.ModelData"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <activity android:name="com.example.meter.MeterID"
            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="com.example.meter.DatenSenden" >
        </activity>
    </application>

应用:     package com.example.meter;

import android.app.Application;

    public class ModelData extends Application{

        private String meterID;
        private String name;

        public ModelData(String meterID, String name) {
            this.meterID = meterID;
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String aName) {
            name = aName;
        }

        public String getMeterID() {
            return meterID;
        }

        public void setMeterID(String aMeterID) {
            meterID = aMeterID;
        }

    }

错误日志:

04-11 22:21:05.636: D/ResourcesManager(3291): creating new AssetManager and set to /data/app/com.example.meter-2/base.apk
04-11 22:21:05.666: D/AndroidRuntime(3291): Shutting down VM
04-11 22:21:05.666: E/AndroidRuntime(3291): FATAL EXCEPTION: main
04-11 22:21:05.666: E/AndroidRuntime(3291): Process: com.example.meter, PID: 3291
04-11 22:21:05.666: E/AndroidRuntime(3291): java.lang.RuntimeException: Unable to instantiate application com.example.meter.ModelData: java.lang.InstantiationException: class com.example.meter.ModelData has no zero argument constructor
04-11 22:21:05.666: E/AndroidRuntime(3291):     at android.app.LoadedApk.makeApplication(LoadedApk.java:625)
04-11 22:21:05.666: E/AndroidRuntime(3291):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5088)
04-11 22:21:05.666: E/AndroidRuntime(3291):     at android.app.ActivityThread.access$1600(ActivityThread.java:177)
04-11 22:21:05.666: E/AndroidRuntime(3291):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1509)
04-11 22:21:05.666: E/AndroidRuntime(3291):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-11 22:21:05.666: E/AndroidRuntime(3291):     at android.os.Looper.loop(Looper.java:145)
04-11 22:21:05.666: E/AndroidRuntime(3291):     at android.app.ActivityThread.main(ActivityThread.java:5944)
04-11 22:21:05.666: E/AndroidRuntime(3291):     at java.lang.reflect.Method.invoke(Native Method)
04-11 22:21:05.666: E/AndroidRuntime(3291):     at java.lang.reflect.Method.invoke(Method.java:372)
04-11 22:21:05.666: E/AndroidRuntime(3291):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
04-11 22:21:05.666: E/AndroidRuntime(3291):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
04-11 22:21:05.666: E/AndroidRuntime(3291): Caused by: java.lang.InstantiationException: class com.example.meter.ModelData has no zero argument constructor

1 个答案:

答案 0 :(得分:1)

摆脱你的ModelData构造函数。什么都不会调用它,它正在消除你需要的默认的公共零参数构造函数。

更好的是,让ModelData不延伸Application并且不要在<application>元素的清单中注册它。您的ModelData似乎无法保证创建自定义Application子类。