当我尝试使用以下代码在活动之间切换时,我的程序崩溃了:
final Button switchButton = (Button) findViewById(R.id.change_mode);
switchButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent runOptionSelect = new Intent(TheDecider.this, OptionSelect.class);
startActivity(runOptionSelect);
return;
}
});
我认为这段代码很好所以它一定是manifest.xml的问题吗?我不明白何时使用哪个活动常数。如果我的目的只是切换到不同的布局和类,我应该选择什么?
此外,MAIN和LAUNCHER仅用于要运行的初始活动吗?
很抱歉提出这样一个基本问题,但我花了太多时间研究这个问题无济于事。谢谢。
答案 0 :(得分:4)
请检查manifest.xml文件中的以下代码
<activity android:name=".TheDecider"
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=".OptionSelect"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
答案 1 :(得分:3)
在意图中,第一个参数是当前上下文(您可以
TheDecider.this
或
getApplicationContext()
那里)第二个是你想要达到的活动的类。
你这样做是对的。在你的清单中你应该添加
<activity android:name=".OptionSelect"
android:label="@string/app_name" />
你必须在你的清单中添加每个活动,否则它会崩溃。在不知道你的logcat内容的情况下,我就可以说了。