[英]Global Variables not working - Android
I have created a class called CommentInfo that extends Application. 我创建了一个名为CommentInfo的类,该类扩展了Application。 This class is suppose to hold global variables for my comments.
该类假定保留用于我的注释的全局变量。 I have declared it in the Manifest file but it still causes the app to crash.
我已经在清单文件中声明了它,但它仍然导致应用程序崩溃。 CommentInfo is not inside the DashboardActivity.
CommentInfo不在DashboardActivity中。
COMMENT INFO CLASS 评论信息类
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;
}
}
I try to access the variables in another activity called DashboardActivity here is a small example, 我尝试在另一个名为DashboardActivity的活动中访问变量,这是一个小示例,
DASHBOARD ACTIVITY 仪表板活动
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");
}
}
Then I declare the CommentInfo name inside the Manifest file as this, This is the only application tag I have in the Manifest file, it wraps around all of my Activities. 然后,我在清单文件中声明CommentInfo名称,这是清单文件中唯一的应用程序标签,它包装了我的所有Activity。
MANIFEST FILE 清单文件
<application
android:name="CommentInfo"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >
Then this is the error code I receive from my LogCat, 这是我从LogCat收到的错误代码,
LOG CAT ERROR 日志记录错误
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)
Replace <application android:name="CommentInfo" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" >
替换
<application android:name="CommentInfo" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" >
with 与
<application android:name="<PACKAGENAME>.CommentInfo" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" >
Inside the CommentInfo @Override OnCreate as well 在CommentInfo @Override OnCreate里面
@Override
public void onCreate() {
super.onCreate();
}
Why does your CommentInfo class extend Application? 为什么您的CommentInfo类扩展了Application? Just make it a regular class.
使其成为普通班。
class CommentInfo {
public CommentInfo() {}
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;
}
}
Then you can use it like this: 然后,您可以像这样使用它:
CommentInfo info = new CommentInfo();
info.setCommentID("0");
public class DataHolderClass { 公共类DataHolderClass {
private static DataHolderClass dataObject= null;
private DataHolderClass()
{
// left blank intentionally
}
public static DataHolderClass getInstance()
{
if(dataObject==null)
dataObject = new DataHolderClass();|
return dataObject;
}
private String commentID; 私有字符串commentID; private int gatheredComments;
私人诠释
public String getCommentID(){
return commentID;
}
public void setCommentID(String c){
this.commentID = c;
}
public int getGatheredComments(){
return gatheredComments;
}
public void setGatheredComments(int forNumber){
this.gatheredComments = forNumber;
}
} }
now in your activity class to set values use: DataHolderClass.getInstance().setCommandId(Your String); 现在在您的活动类中设置值,请使用:DataHolderClass.getInstance()。setCommandId(Your String);
and to get values from dataholderclass 并从dataholderclass获取值
DataHolderClass.getInstance().commentID . DataHolderClass.getInstance()。commentID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.