繁体   English   中英

使用Intent错误启动活动

[英]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 日志 ,您可以更正我的代码并向我解释吗? 我也想学习:P预先感谢

您可以使用多种方法启动任何活动。 对于您的情况,您可以这样编写:

startActivity(new Intent(Patch.this, MainActivity.class));

您尚未声明Patch.this的意图是像

startActivity(new Intent(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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM