2

我有三个类,一个主要活动(名为 MainMap)、一个非活动类(名为 MyItemizedOverlay)和一个活动类(名为 AudioStream)。我想从非活动类开始 AudioStream 活动,但我不知道如何。我试过这是在第三类(称为MyItemizedOverlay):

            Intent myIntentA = new Intent(MainMap.this, AudioStream.class);
            myIntentA.putExtra(AUDIO_STREAM,AUDIO_STREAM_URL);
            MojProg.this.startActivity(myIntentA);

但它不起作用,说:没有 MainMap 类型的封闭实例在范围内是可访问的

我应该怎么办?我应该写什么而不是 MainMap.this?

4

2 回答 2

2

这与其说是一个 Android 问题,不如说是一个 Java 问题。除非您将“MyItemizedOverlay”设为“MainMap”的内部类(参见http://forums.sun.com/thread.jspa?threadID=690545),否则您真正需要的是 MyItemizedOverlay 存储对它要用于 inent 的 MainMap 对象。

问候,马克

于 2010-01-02T18:29:49.420 回答
0
Intent myIntentA = new Intent(MainMap.this, AudioStream.class);
myIntentA.putExtra(AUDIO_STREAM,AUDIO_STREAM_URL);
MojProg.this.startActivity(myIntentA);

这行不通。因为“this”的意思是“这个类”。您不能在其他课程上使用它(是的,您可以,但有不同的方法。请在论坛、本网站或 oracle 网站上研究“this”。)。这就是那个警告的原因。

好吧,看起来您的问题是“如何将上下文拉到非活动类?”。(Intent() 的第一个参数是 Context)。

为此,您可以在 Main Activity 中创建一个 Context 瞬间并将您的基本上下文分配给它,例如:

static Context context;

....

context = this.getBaseContext();

不要忘记那是你的主要活动。然后在你的非活动类中,你可以提取这个上下文并使用它,比如:

Context context;
Intent intent;

....Constructor: 

context = MainActivity.context;
intent = new Intent(context, YourSecondActivity.class); // you have to declare your second activity in the AndroidManifest.xml 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this is required to call "Intent()" in a non-activity class.

//And then you can call the method anywhere you like (in this class of course)

context.startActivity(intent);

好的。您已准备好再迈出一步。在 AndroidManifest.xml 中,像第一个一样声明您的第二个活动;

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity 
        android:name=".YourSecondActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
    </activity>

你现在准备好了。但是最后一个警告,不要忘记在打开另一个活动之前处理您的活动以避免滞后。玩得开心。

于 2012-12-10T16:59:22.687 回答