我创建了一个名为 CommentInfo 的类,它扩展了 Application。这个类应该为我的评论保存全局变量。我已经在 Manifest 文件中声明了它,但它仍然会导致应用程序崩溃。CommentInfo 不在 DashboardActivity 中。
评论信息类
package com.example;
import android.app.Application;
class CommentInfo extends Application {
private String commentID;
private int gatheredComments;
public String getCommentID(){
return commentID;
}
public void setCommentID(String c){
commentID = c;
}
public int getGatheredComments(){
return gatheredComments;
}
public void setGatheredComments(int forNumber){
gatheredComments = forNumber;
}
}
我尝试访问另一个名为 DashboardActivity 的活动中的变量,这是一个小例子,
仪表板活动
public class DashboardActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//This is how I call the class
final CommentInfo CommentInfoClass = ((CommentInfo)getApplicationContext());
//This is how I set the variables
CommentInfoClass.setCommentID("0");
}
}
然后我在 Manifest 文件中声明 CommentInfo 名称,这是我在 Manifest 文件中唯一的应用程序标记,它包含我的所有活动。
清单文件
<application
android:name="CommentInfo"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >
然后这是我从 LogCat 收到的错误代码,
记录猫错误
08-23 00:01:13.486: E/AndroidRuntime(24880): FATAL EXCEPTION: main
08-23 00:01:13.486: E/AndroidRuntime(24880): java.lang.RuntimeException: Unable to instantiate application com.example.CommentInfo: java.lang.IllegalAccessException: access to class not allowed
08-23 00:01:13.486: E/AndroidRuntime(24880): at android.app.LoadedApk.makeApplication(LoadedApk.java:529)
08-23 00:01:13.486: E/AndroidRuntime(24880): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4442)
08-23 00:01:13.486: E/AndroidRuntime(24880): at android.app.ActivityThread.access$1300(ActivityThread.java:139)
08-23 00:01:13.486: E/AndroidRuntime(24880): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
08-23 00:01:13.486: E/AndroidRuntime(24880): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 00:01:13.486: E/AndroidRuntime(24880): at android.os.Looper.loop(Looper.java:154)
08-23 00:01:13.486: E/AndroidRuntime(24880): at android.app.ActivityThread.main(ActivityThread.java:4945)
08-23 00:01:13.486: E/AndroidRuntime(24880): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 00:01:13.486: E/AndroidRuntime(24880): at java.lang.reflect.Method.invoke(Method.java:511)
08-23 00:01:13.486: E/AndroidRuntime(24880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-23 00:01:13.486: E/AndroidRuntime(24880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-23 00:01:13.486: E/AndroidRuntime(24880): at dalvik.system.NativeStart.main(Native Method)
08-23 00:01:13.486: E/AndroidRuntime(24880): Caused by: java.lang.IllegalAccessException: access to class not allowed
08-23 00:01:13.486: E/AndroidRuntime(24880): at java.lang.Class.newInstanceImpl(Native Method)
08-23 00:01:13.486: E/AndroidRuntime(24880): at java.lang.Class.newInstance(Class.java:1319)
08-23 00:01:13.486: E/AndroidRuntime(24880): at android.app.Instrumentation.newApplication(Instrumentation.java:963)
08-23 00:01:13.486: E/AndroidRuntime(24880): at android.app.Instrumentation.newApplication(Instrumentation.java:948)
08-23 00:01:13.486: E/AndroidRuntime(24880): at android.app.LoadedApk.makeApplication(LoadedApk.java:520)