拥有创建应用程序的一个子类的麻烦共享多个活动数据 [英] Having trouble creating a subclass of application to share data with multiple Activities

查看:134
本文介绍了拥有创建应用程序的一个子类的麻烦共享多个活动数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了几个在我的游戏活动,现在我要开始来连接他们两个使用真正的游戏数据,而不是测试数据,我只用,以确保每一件工作。由于多个活动将需要访问本场比赛的数据,我开始研究最好的办法将这些数据传递给我的活动。

I just finished a couple of activities in my game and now I was going to start to wire them both up to use real game data, instead of the test data I was using just to make sure each piece worked. Since multiple Activities will need access to this game data, I started researching the best way to pass this data to my Activities.

我知道如何使用putExtra与意图,但我GAMEDATA类有相当多的数据,而不是只是简单的键值对。除了相当多的基本数据类型,它也有很大的阵列。我真的不希望尝试,并通过这一切,除非我可以通过整个对象,而不是仅仅键/数据对。

I know about using putExtra with intents, but my GameData class has quite a bit of data and not just simple key value pairs. Besides quite a few basic data types, it also has large arrays. I didn't really want to try and pass all that, unless I can pass the entire object, instead of just key/data pairs.

我读了下面的帖子,并认为这将是一段路要走,但到目前为止,我还没有得到它的工作。

I read the following post and thought it would be the way to go, but so far, I haven't got it to work.

如何在Android中声明全局变量?

我创建了一个简单的测试应用程序来试试这个方法了,但它总是崩溃,我的code似乎看起来一样在上面的帖子 - 除了我改了名字。这里是我得到的错误。有人可以帮助我理解我在做什么错了?

I created a simple test app to try this method out, but it keeps crashing and my code seems to look the same as in the post above - except I changed the names. Here is the error I am getting. Can someone help me understand what I am doing wrong?

12-23 00:50:49.762:ERROR / AndroidRuntime(608):java.lang.ClassCastException:产生的原因android.app.Application

12-23 00:50:49.762: ERROR/AndroidRuntime(608): Caused by: java.lang.ClassCastException: android.app.Application

据崩溃以下语句:

GameData newGameData = ((GameData)getApplicationContext());  

下面是我的code:

package mrk.examples.MainActivity;

import android.app.Application;

public class GameData extends Application {
    private int intTest;

    GameData () {
        intTest = 0;
    }

    public int getIntTest(){
        return intTest;
    }

    public void setIntTest(int value){
        intTest = value;
    }
}

//我的主要活动

// My main activity

package mrk.examples.MainActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

    int intLocal;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GameData newGameData = ((GameData) getApplicationContext());
        newGameData.setIntTest(0);
        intLocal = newGameData.getIntTest();
        Log.d("MainActivity", "IntLocal = " + intLocal);
        newGameData.setIntTest(1);
        Log.d("MainActivity", "IntLocal = " + intLocal
                + " newGameData IntTest: " + newGameData.getIntTest());
        Intent intentNew = new Intent(this, PassData2Activity.class);
        startActivity(intentNew);
    }
}

//我的测试活动,看它是否能够从上次活动访问数据和previous状态

// My test Activity to see if it can access the data and its previous state from the last activity

package mrk.examples.MainActivity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class PassData2Activity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GameData gamedataPass = ((GameData)getApplicationContext());
        Log.d("PassData2Activity", "gamedatapass IntTest =  " +
                                         gamedataPass.getIntTest());           
    }
}

下面是我的清单的相关部分:

Below is the relevant portion of my manifest:

<application android:icon="@drawable/icon"
             android:label="@string/app_name">    
    <activity android:name=".MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>            
    </activity>

    <activity android:name=".PassData2Activity"></activity>

</application> 

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

请帮助我了解为什么这个code崩溃?

Please help me understand why this code is crashing?

另外,如果你认为这仅仅是错误的做法,让多种活动可以访问相同的数据,请给你的建议。请记住,我说的是相当多的变量和一些大型的阵列。

Also, if you think this is just the wrong approach to let multiple activities have access to the same data, please give your suggestion. Please keep in mind that I am talking about quite a few variables and some large arrays.

推荐答案

我不知道,但我觉得要在清单中这些标记合并成一个单一的标签,像这样。 (您可能还需要充分阐明,而不是使用初始出您的应用程序包的名称,作为一种快捷方式。)

I'm not sure, but I think you want to combine those tags in the manifest into a single tag, like this. (You may also need to fully spell out the package name of your app, instead of using the initial . as a shortcut.)

<application android:icon="@drawable/icon"
             android:name=".GameData"
             android:label="@string/app_name">    
    <activity android:name=".StaticGameData"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>            
    </activity>

    <activity android:name=".PassData2Activity"></activity>
</application> 

(顺便说一句,我会避免使用名称 GAMEDATA 应用程序,而 StaticGameData 的活动;这些都让我觉得 StaticGameData 将是一个子类 GAMEDATA

(By the way, I'd avoid using the names GameData for the application, and StaticGameData for the activity; these make me think that StaticGameData would be a subclass of GameData.)

这篇关于拥有创建应用程序的一个子类的麻烦共享多个活动数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆