0

我创建了一个名为 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)
4

3 回答 3

0

公共类 DataHolderClass {

private static DataHolderClass dataObject= null;

private DataHolderClass()

{
    // left blank intentionally
}


public static DataHolderClass getInstance()

{
    if(dataObject==null)

        dataObject = new DataHolderClass();|

    return dataObject;

}

私人字符串评论ID;私人 int 聚集评论;

  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 。

于 2013-08-23T05:11:11.030 回答
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();

   }
于 2013-08-23T04:12:25.610 回答
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");
于 2013-08-23T04:18:44.660 回答