Global Variables not working - Android

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I have created a class called CommentInfo that extends 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.

  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,

  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.

  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,

  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)

Answers

Why does your CommentInfo class extend 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");

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/18394499/global-variables-not-working-android

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils