0

Application を拡張する CommentInfo というクラスを作成しました。このクラスは、コメント用のグローバル変数を保持することになっています。マニフェスト ファイルで宣言しましたが、それでもアプリがクラッシュします。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");

    }
}

次に、マニフェスト ファイル内で CommentInfo 名を次のように宣言します。これは、マニフェスト ファイルにある唯一のアプリケーション タグであり、すべてのアクティビティをラップします。

マニフェストファイル

<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

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 に答える
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 に答える