我正在尝试设置一个全局变量类,但它正在崩溃。我遗失的任何东西?

时间:2016-08-07 19:56:29

标签: java android

错误日志:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sellingbcom.myapplicationa/com.example.sellingbcom.myapplicationa.DisplayActivity}:
     

java.lang.ClassCastException:无法转换android.app.Application   com.example.sellingbcom.myapplicationa.GlobalActivity                                                                                                       在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)                                                                                                       在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)                                                                                                       在android.app.ActivityThread.access $ 700(ActivityThread.java:143)                                                                                                       在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1241)                                                                                                       在android.os.Handler.dispatchMessage(Handler.java:99)                                                                                                       在android.os.Looper.loop(Looper.java:137)

                                                                                                at android.app.ActivityThread.main(ActivityThread.java:4950)
                                                                                                at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                                at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                                at
     

com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1004)                                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)                                                                                                       在dalvik.system.NativeStart.main(本地方法)

                                                                                             Caused by: java.lang.ClassCastException: android.app.Application
     

无法投射到   com.example.sellingbcom.myapplicationa.GlobalActivity                                                                                                       在   com.example.sellingbcom.myapplicationa.DisplayActivity.onCreate(DisplayActivity.java:17)                                                                                                       在android.app.Activity.performCreate(Activity.java:5177)                                                                                                       在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)                                                                                                       在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)                                                                                                       在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)

                                                                                              at android.app.ActivityThread.access$700(ActivityThread.java:143) 
                                                                                                at
     

android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1241)                                                                                                       在android.os.Handler.dispatchMessage(Handler.java:99)                                                                                                       在android.os.Looper.loop(Looper.java:137)                                                                                                       在android.app.ActivityThread.main(ActivityThread.java:4950)                                                                                                       at java.lang.reflect.Method.invokeNative(Native Method)                                                                                                       在java.lang.reflect.Method.invoke(Method.java:511)

我正在尝试设置全局变量类,但它正在崩溃。我需要知道我错过了什么?

/ * Global Class

 public  class  GlobalActivity extends AppCompatActivity {

private int albumId;

    public int getAlbumId() {
        return albumId;
    }

    public void setAlbumId(int albumId) {
        this.albumId = albumId;
    }

  }

/ *清单

   <activity android:name=".DisplayActivity"></activity> 
<activity  android:name=".GlobalActivity"></activity>

/ *活动主要

public class MainActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    int x=10;
    final GlobalActivity globalAlbum = (GlobalActivity) getApplicationContext();
    globalAlbum.setAlbumId(x);
 }

 public void dispAct(View view) {
    Intent i = new Intent(this, DisplayActivity.class);
    startActivity(i);

 }
}

/ * DisplayActivity

public class DisplayActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display);

    final GlobalActivity globalAlbum = (GlobalActivity) getApplicationContext();
    int x = globalAlbum.getAlbumId();

    TextView songLyric = (TextView) findViewById(R.id.disp);
    songLyric.setText(x);
 }
}

/ *活动XML

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.sellingbcom.myapplicationa.MainActivity">



        <Button
            android:id="@+id/tgl_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:focusable="false"
            android:clickable="false"
            android:onClick="dispAct"
            android:text="PICK"

            />
    </LinearLayout>

/ *显示XML

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/disp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />
    </LinearLayout>

3 个答案:

答案 0 :(得分:0)

您可以扩展基础android.app.Application类并添加

等成员变量
public class MyApplication extends Application {

private String someVariable;

public String getSomeVariable() {
    return someVariable;
}

public void setSomeVariable(String someVariable) {
    this.someVariable = someVariable;
}

现在在清单中执行此操作  必须声明实现android.app.Application的类(将android:name =“。MyApplication”属性添加到现有的应用程序标记) 如下所示

<application  android:name=".MyApplication" android:icon="@drawable/icon"  android:label="@string/app_name">

然后在您的活动中,您可以获得并设置如下所述

((MyApplication) this.getApplication()).setSomeVariable("foo");
String s = ((MyApplication) this.getApplication()).getSomeVariable();

答案 1 :(得分:0)

该行:

final GlobalActivity globalAlbum = (GlobalActivity) getApplicationContext();

是错误的,主要是因为您无法将应用程序上下文转换为特定的activity类。如果你真的想要一个全局类,你必须在使用之前确保它的一个实例存在。

其中一个选项是使用静态字段创建普通Java类。这些应用程序初始化后即可使用,但是,您必须小心不要保存任何可能包含context变量的内容,因为它会要求内存泄漏。

第二种方法是使用application类。 @hatib已经发布了它,而我输入了这个:D。我此后会说同样的话。

答案 2 :(得分:0)

我正在使用这种方式

  1. 将变量声明为public static
  2. 使用类名
  3. 在任何地方调用它

    例如

    public class Main12 extends AppCompatActivity {
    
    public static int age;
    }
    

    通过

    调用它
    Main12.age = 4;
    

    你甚至可以在同一个应用程序的其他活动中调用它