应用程序不会启动下一个活动和崩溃

时间:2018-04-20 02:57:45

标签: java android

我想按下我的“表主题”按钮以启动“表主题活动”,它最终导致应用程序崩溃但我的代码看起来对我来说完全没问题,我希望你们可以帮助我解决我的代码。 Timing按钮用于启动其他Activity,但现在我添加了Table Topics的代码,如果按下任何按钮,它就会崩溃所有内容

这是我的MainActivity.java

package com.iprex.light;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

static MediaPlayer mp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();
    setContentView(R.layout.activity_main);

}

public void onClickGreen(View view) {
    startActivity(new Intent("com.iprex.green"));

}

public void onClickYellow(View view) {
    startActivity(new Intent("com.iprex.yellow"));

}

public void onClickRed(View view) {
    startActivity(new Intent("com.iprex.red"));

}

public void onClickTBTWhite(View view){
    startActivity(new Intent("com.iprex.tbtwhite"));

}

}

这是我的AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iprex.light">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Green" android:label="@string/app_name">
        <intent-filter>
            <action android:name="com.iprex.green"/>

        </intent-filter>
    </activity>

    <activity android:name=".Yellow" android:label="@string/app_name">
        <intent-filter>
            <action android:name="com.iprex.yellow"/>

        </intent-filter>
    </activity>

    <activity android:name=".Red" android:label="@string/app_name">
        <intent-filter>
            <action android:name="com.iprex.red"/>

        </intent-filter>
    </activity>

    <activity android:name=".TBTWhite" android:label="Times">
        <intent-filter>
            <action android:name="com.iprex.tbtwhite"/>

        </intent-filter>
    </activity>

    <activity android:name=".TBTGreen" android:label="Times">
        <intent-filter>
            <action android:name="com.iprex.tbtgreen"/>

        </intent-filter>
    </activity>

    <activity android:name=".TBTYellow" android:label="Times">
        <intent-filter>
            <action android:name="com.iprex.tbtyellow"/>

        </intent-filter>
    </activity>

    <activity android:name=".TBTRed" android:label="Times">
        <intent-filter>
            <action android:name="com.iprex.tbtred"/>

        </intent-filter>
    </activity>

</application>

</manifest>

这是我按下按钮后的运行日志

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.iprex.light, PID: 20558
              java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:6207)
                  at android.widget.TextView.performClick(TextView.java:11094)
                  at android.view.View$PerformClick.run(View.java:23639)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6688)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:6207) 
                  at android.widget.TextView.performClick(TextView.java:11094) 
                  at android.view.View$PerformClick.run(View.java:23639) 
                  at android.os.Handler.handleCallback(Handler.java:751) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6688) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
               Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.iprex.tbtwhite launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } }
                  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1839)
                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1531)
                  at android.app.Activity.startActivityForResult(Activity.java:4389)
                  at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:65)
                  at android.app.Activity.startActivityForResult(Activity.java:4348)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:711)
                  at android.app.Activity.startActivity(Activity.java:4672)
                  at android.app.Activity.startActivity(Activity.java:4640)
                  at com.iprex.light.MainActivity.onClickTBTWhite(MainActivity.java:40)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:6207) 
                  at android.widget.TextView.performClick(TextView.java:11094) 
                  at android.view.View$PerformClick.run(View.java:23639) 
                  at android.os.Handler.handleCallback(Handler.java:751) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6688) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

1 个答案:

答案 0 :(得分:1)

启动其他活动的代码不正确。将您的代码更改为

public void onClickGreen(View view) {
    startActivity(new Intent(this, Green.class));
}

public void onClickYellow(View view) {
    startActivity(new Intent(this, Yellow.class));
}

public void onClickRed(View view) {
    startActivity(new Intent(this, Red.class));
}

public void onClickTBTWhite(View view){
    startActivity(new Intent(this, TBTWhite.class));
}

如果要使用包名和类名启动其他活动。然后你可以尝试

public void onClickGreen(View view) {
    openActivity("com.iprex.green", "com.iprex.green.Green");
}

public void onClickYellow(View view) {
    openActivity("com.iprex.yellow", "com.iprex.yellow.Yellow");
}

public void onClickRed(View view) {
    openActivity("com.iprex.red", "com.iprex.red.Green");
}

public void onClickTBTWhite(View view){
    openActivity("com.iprex.tbtwhiten", "com.iprex.tbtwhite.TBTWhite");
}

private void openActivity(String packageName, String className) {
    Intent intent = new Intent();
    intent.setClassName(packageName, className);
    startActivity(intent);
}

您可以将以上代码应用于其他活动

快乐的编码!