全局变量不起作用 - Android

时间:2013-08-23 04:04:16

标签: android global-variables

我创建了一个名为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的活动中的变量,这是一个小例子,

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");

    }
}

然后我在Manifest文件中声明CommentInfo名称, 这是我在Manifest文件中唯一的应用程序标记,它包含了我的所有活动。

预告文件

<application 
        android:name="CommentInfo"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >

然后这是我从LogCat收到的错误代码,

记录CAT错误

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)

3 个答案:

答案 0 :(得分:0)

替换<application android:name="CommentInfo" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" >

<application android:name="<PACKAGENAME>.CommentInfo" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" >

在CommentInfo @Override OnCreate内部

 @Override
   public void onCreate() {

        super.onCreate();

   }

答案 1 :(得分:0)

为什么CommentInfo类会扩展Application?只需将它定为普通课程。

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;
          }
    }

然后你可以像这样使用它:

CommentInfo info = new CommentInfo();
info.setCommentID("0");

答案 2 :(得分:0)

public class 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;       private int gatherComments;

  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;
      }

}

现在在您的活动类中设置值使用: DataHolderClass.getInstance()。setCommandId(Your String);

并从dataholderclass获取值

DataHolderClass.getInstance()。commentID。