【问题标题】:App keeps crashing and not running when testing ir应用程序在测试 ir 时不断崩溃并且无法运行
【发布时间】:2019-12-07 07:06:07
【问题描述】:

我正在构建一个应用程序,当我尝试在模拟器上对其进行测试时,它说不幸的是应用程序已停止,所以当我运行它时,我从 logcat 部分收到此错误,该应用程序是静态的并且包含没有 firebase 或 onesignal 的内容, 我在 DroidX(Android 4.4.4,API 17) 上运行它

这是我得到的错误:

07-30 05:25:40.650 3188-3188/com.med E/Trace:打开跟踪文件时出错: 没有这样的文件或目录 (2) 07-30 05:25:40.806 3188-3188/com.med E/dalvikvm: 找不到课程 'android.app.AppOpsManager',从方法中引用 com.google.android.gms.common.wrappers.PackageManagerWrapper.zzb 07-30 05:25:41.410 3188-3188/com.med E/AndroidRuntime: 致命异常: main java.lang.RuntimeException:无法启动活动 ComponentInfo{com.med/com.med.Splash}:java.lang.IllegalStateException: 您需要在此活动中使用 Theme.AppCompat 主题(或后代)。 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2204) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2254) 在 android.app.ActivityThread.access$600(ActivityThread.java:141) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:137) 在 android.app.ActivityThread.main(ActivityThread.java:5069) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 在 dalvik.system.NativeStart.main(本机方法) 引起:java.lang.IllegalStateException:你需要使用一个 Theme.AppCompat 主题(或后代)与此活动。 在 androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:686) 在 androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:649) 在 androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:542) 在 androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161) 在 com.med.Splash.onCreate(Splash.java:27) 在 android.app.Activity.performCreate(Activity.java:5104) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1092) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2254) 在 android.app.ActivityThread.access$600(ActivityThread.java:141) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:137) 在 android.app.ActivityThread.main(ActivityThread.java:5069) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 在 dalvik.system.NativeStart.main(本机方法)

<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.med.Splash">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name="com.med.MainActivity"></activity>
meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-"/>
</application>

<?xml version="1.0"?>










-<resources>

<!-- Base application theme. -->



-<style parent="Theme.AppCompat.Light.NoActionBar" name="AppTheme">

<!-- Customize your theme here. -->


<item name="colorPrimary">@color/colorPrimary</item>

<item name="colorPrimaryDark">@color/colorPrimaryDark</item>

<item name="colorAccent">@color/colorAccent</item>

</style>

</resources>






package com.medi;

import android.media.MediaPlayer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import java.net.MalformedURLException;
import java.net.URL;
import com.google.ads.consent.*;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

public class MainActivity extends AppCompatActivity {

ImageView play,play2,play3,play4,play5,play6,play7,play8,pause,pause2,pause3,pause4,pause5,pause6,pause7,pause8;

InterstitialAd mInterstitialAd;
MediaPlayer mPlayer;

private ConsentForm form;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String[] publisherIds = {"pub-3281091428733026"};

    ConsentInformation consentInformation = 
ConsentInformation.getInstance(getApplicationContext());
    consentInformation.requestConsentInfoUpdate(publisherIds, new 
ConsentInfoUpdateListener() {
        @Override
        public void onConsentInfoUpdated(ConsentStatus consentStatus) {
            // User's consent status successfully updated.
        }

        @Override
        public void onFailedToUpdateConsentInfo(String errorDescription) {
            // User's consent status failed to update.
        }
    });

    URL privacyUrl = null;
    try {
        // TODO: Replace with your app's privacy policy URL.
        privacyUrl = new URL("");
    } catch (MalformedURLException e) {
        e.printStackTrace();
        // Handle error.
    }
    form = new ConsentForm.Builder(getApplicationContext(), privacyUrl)
            .withListener(new ConsentFormListener() {
                @Override
                public void onConsentFormLoaded() {
                    // Consent form loaded successfully.
                    form.show();
                }

                @Override
                public void onConsentFormOpened() {
                    // Consent form was displayed.
                }

                @Override
                public void onConsentFormClosed(
                        ConsentStatus consentStatus, Boolean userPrefersAdFree) {
                    // Consent form was closed.
                }

                @Override
                public void onConsentFormError(String errorDescription) {
                    // Consent form error.
                }
            })
            .withPersonalizedAdsOption()
            .withNonPersonalizedAdsOption()
            .withAdFreeOption()
            .build();

    form.load();

    play=(ImageView)findViewById(R.id.play);
    play2=(ImageView)findViewById(R.id.play2);
    play3=(ImageView)findViewById(R.id.play3);
    play4=(ImageView)findViewById(R.id.play4);
    play5=(ImageView)findViewById(R.id.play5);
    play6=(ImageView)findViewById(R.id.play6);
    play7=(ImageView)findViewById(R.id.play7);
    play8=(ImageView)findViewById(R.id.play8);
    pause=(ImageView)findViewById(R.id.pause);
    pause2=(ImageView)findViewById(R.id.pause2);
    pause3=(ImageView)findViewById(R.id.pause3);
    pause4=(ImageView)findViewById(R.id.pause4);
    pause5=(ImageView)findViewById(R.id.pause5);
    pause6=(ImageView)findViewById(R.id.pause6);
    pause7=(ImageView)findViewById(R.id.pause7);
    pause8=(ImageView)findViewById(R.id.pause8);

    AdView mAdViewB = (AdView)findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdViewB.loadAd(adRequest);

    mInterstitialAd = new InterstitialAd(getApplicationContext());
    mInterstitialAd.setAdUnitId(getResources().getString(R.string.intestital_ads));
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
        }

        @Override
        public void onAdLoaded() {
            mInterstitialAd.show();
        }
    });

    requestNewInterstitial();

    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            play.setVisibility(View.GONE);
            pause.setVisibility(View.VISIBLE);

            mPlayer = MediaPlayer.create(MainActivity.this, R.raw.one);
            mPlayer.start();
        }
    });

    play2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            play2.setVisibility(View.GONE);
            pause2.setVisibility(View.VISIBLE);
            mPlayer = MediaPlayer.create(MainActivity.this, R.raw.two);
            mPlayer.start();

        }
    });

    play3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            play3.setVisibility(View.GONE);
            pause3.setVisibility(View.VISIBLE);
            mPlayer = MediaPlayer.create(MainActivity.this, R.raw.three);
            mPlayer.start();

        }
    });

    play4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            play4.setVisibility(View.GONE);
            pause4.setVisibility(View.VISIBLE);
            mPlayer = MediaPlayer.create(MainActivity.this, R.raw.four);
            mPlayer.start();

        }
    });

    play5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            play5.setVisibility(View.GONE);
            pause5.setVisibility(View.VISIBLE);
            mPlayer = MediaPlayer.create(MainActivity.this, R.raw.five);
            mPlayer.start();

        }
    });

    play6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            play6.setVisibility(View.GONE);
            pause6.setVisibility(View.VISIBLE);
            mPlayer = MediaPlayer.create(MainActivity.this, R.raw.six);
            mPlayer.start();

        }
    });

    play7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            play7.setVisibility(View.GONE);
            pause7.setVisibility(View.VISIBLE);
            mPlayer = MediaPlayer.create(MainActivity.this, R.raw.seven);
            mPlayer.start();

        }
    });

    play8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            play8.setVisibility(View.GONE);
            pause8.setVisibility(View.VISIBLE);

            mPlayer = MediaPlayer.create(MainActivity.this, R.raw.eigth);
            mPlayer.start();

        }
    });

    pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pause.setVisibility(View.GONE);
            play.setVisibility(View.VISIBLE);
            mPlayer.stop();
        }
    });

    pause2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pause2.setVisibility(View.GONE);
            play2.setVisibility(View.VISIBLE);
            mPlayer.stop();
        }
    });

    pause3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pause3.setVisibility(View.GONE);
            play3.setVisibility(View.VISIBLE);
            mPlayer.stop();
        }
    });

    pause4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pause4.setVisibility(View.GONE);
            play4.setVisibility(View.VISIBLE);
            mPlayer.stop();
        }
    });

    pause5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pause5.setVisibility(View.GONE);
            play5.setVisibility(View.VISIBLE);
            mPlayer.stop();
        }
    });

    pause6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pause6.setVisibility(View.GONE);
            play6.setVisibility(View.VISIBLE);
            mPlayer.stop();
        }
    });

    pause7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pause7.setVisibility(View.GONE);
            play7.setVisibility(View.VISIBLE);
            mPlayer.stop();
        }
    });

    pause8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pause8.setVisibility(View.GONE);
            play8.setVisibility(View.VISIBLE);
            mPlayer.stop();
        }
    });



}

private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder().build();

    mInterstitialAd.loadAd(adRequest);
}

}

package com.med;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;



public class Splash extends AppCompatActivity {



InterstitialAd mInterstitialAd;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    AdView mAdViewB = (AdView)findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdViewB.loadAd(adRequest);

    mInterstitialAd = new InterstitialAd(getApplicationContext());
        mInterstitialAd.setAdUnitId(getResources().getString(R.string.intestital_ads));
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
        }

        @Override
        public void onAdLoaded() {
            mInterstitialAd.show();
        }
    });

    requestNewInterstitial();


    Thread thread=new Thread(){
        public void run(){
            try {
                Thread.sleep(4000);


                Intent intent= new Intent(Splash.this,MainActivity.class);
                startActivity(intent);
                finish();


            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    };
    thread.start();


}
private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder().build();

    mInterstitialAd.loadAd(adRequest);
}

}

【问题讨论】:

    标签: java android android-studio kotlin apk


    【解决方案1】:

    您发布的错误日志中给出了解决方案:

    FATAL EXCEPTION: main java.lang.RuntimeException: Unable to >start activity ComponentInfo{com.med/com.med.Splash}: >java.lang.IllegalStateException: 您需要使用 >Theme.AppCompat 主题(或后代)与此活动。

    您正在使用 AppCompatActivity,因此您必须使用 Appcompat 主题作为父主题。

    转到styles.xml文件,有一个名为AppTheme的样式标签, 将其parent 属性更改为此主题或类似主题:

    <style
    name= "AppTheme"
    parent="@style/Theme.AppCompat.Light"
    ...
    />
    

    【讨论】:

    • @MoukrimTaha 你能给我看看你的styles.xml 文件吗?
    • - -
    • @MoukrimTaha 尝试将此行放入您的 Splash 活动标签中:android:theme="@style/AppTheme" 像这样:&lt;activity android:name="com.med.Splash" android:theme="@style/AppTheme" &gt;
    • @MoukrimTaha 另外,请通过编辑您的问题发布Splash 课程的代码,这可能是因为使用了错误的上下文。
    • schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:ads="schemas.android.com/apk/res-auto" android:background="#ffc95c">
    【解决方案2】:

    ...'在此活动中使用 Theme.AppCompat 主题(或后代)。' Activity 需要在清单中声明的​​ App Compatible 主题。 添加到清单中的活动声明:

    <activity android:name="com.med.Splash">
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    

    添加到styles.xml:

    <resources>
    
        <style name="AppTheme.NoActionBar">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
            <item name="android:windowFullscreen">true</item>
        </style>
    
    </resources>
    

    【讨论】:

    • 如果不设置 parent 属性,这将无法工作,如果我错了,请纠正我。
    • 您可以使用任何与 App 兼容的主题,也可以制作自己的主题并使用父主题进行扩展。然后,您只需要将样式链接到清单中的活动。
    • 是的,但是这个 AppTheme.NoActionBar 只是一个名称,它不扩展任何兼容的主题。
    • 我相信这是一个兼容的主题,而不是 100% 的主题是 App Compat,如果它不起作用,请尝试类似:
    • 对不起@vasmos 我不是问这个问题的人,我只是在验证你的答案是否基于我不知道的东西。我刚刚和你同时回答了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2014-04-08
    • 2018-12-23
    • 2014-12-24
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    相关资源
    最近更新 更多