使用 Intent 错误启动 activity
Starting an activity using Intent error
我是新手,所以如果一开始我的问题不太好请见谅 :D
这是我的代码,效果很好:
package com.github.chenxiaolong.dualbootpatcher;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class Patch extends Activity{
MediaPlayer mpbuttonclick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.patcher);
mpbuttonclick = MediaPlayer.create(this, R.raw.keypress);
Button sumbitButton = (Button) findViewById(R.id.submitbutton);
sumbitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
EditText passwordEditText = (EditText) findViewById(R.id.passwordedittext);
if(passwordEditText.getText().toString().equals("1234")){
startActivity(new Intent(".MainActivity"));
mpbuttonclick.start();
}}});
}}
这是我的 AndroidManifest:
<activity
android:name=".MainActivity"
android:label="@string/app_name_release"
android:theme="@style/DrawerActivityTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Patch"
android:label="@string/app_name_release"
android:theme="@style/DrawerActivityTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
它编译得很好但是当我输入 1234 到文本时它强制关闭应用程序这里是 logcats
logs
你能更正我的代码并向我解释吗?我也想学习 :P 提前致谢
您可以使用多种方式启动任何 activity。
对于你的情况,你可以这样写:
startActivity(new Intent(Patch.this, MainActivity.class));
您还没有声明 Patch.this 在侧面意图中使用它
startActivity(新意图(Patch.this,MainActivity.class));
希望它能奏效..
尝试删除 AndroidManifest.xml
中第二个 Activity 的 "intent-filter" 部分
您的 AndroidManifest.xml 应如下所示:
<activity
android:name=".MainActivity"
android:label="@string/app_name_release"
android:theme="@style/DrawerActivityTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Patch"
android:label="@string/app_name_release"
android:theme="@style/DrawerActivityTheme">
</activity>
我是新手,所以如果一开始我的问题不太好请见谅 :D 这是我的代码,效果很好:
package com.github.chenxiaolong.dualbootpatcher;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class Patch extends Activity{
MediaPlayer mpbuttonclick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.patcher);
mpbuttonclick = MediaPlayer.create(this, R.raw.keypress);
Button sumbitButton = (Button) findViewById(R.id.submitbutton);
sumbitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
EditText passwordEditText = (EditText) findViewById(R.id.passwordedittext);
if(passwordEditText.getText().toString().equals("1234")){
startActivity(new Intent(".MainActivity"));
mpbuttonclick.start();
}}});
}}
这是我的 AndroidManifest:
<activity
android:name=".MainActivity"
android:label="@string/app_name_release"
android:theme="@style/DrawerActivityTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Patch"
android:label="@string/app_name_release"
android:theme="@style/DrawerActivityTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
它编译得很好但是当我输入 1234 到文本时它强制关闭应用程序这里是 logcats logs 你能更正我的代码并向我解释吗?我也想学习 :P 提前致谢
您可以使用多种方式启动任何 activity。 对于你的情况,你可以这样写:
startActivity(new Intent(Patch.this, MainActivity.class));
您还没有声明 Patch.this 在侧面意图中使用它
startActivity(新意图(Patch.this,MainActivity.class));
希望它能奏效..
尝试删除 AndroidManifest.xml
中第二个 Activity 的 "intent-filter" 部分您的 AndroidManifest.xml 应如下所示:
<activity
android:name=".MainActivity"
android:label="@string/app_name_release"
android:theme="@style/DrawerActivityTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Patch"
android:label="@string/app_name_release"
android:theme="@style/DrawerActivityTheme">
</activity>