0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Android - Java】Myエラーノート

Last updated at Posted at 2025-07-02

ErrorList.png

💥 android.content.ActivityNotFoundException

📄 エラーログ

Logcat
    android.content.ActivityNotFoundException:
    Unable to find explicit activity class
    {sample/sample.ui.sample.sampleActivity};
    have you declared this activity in your AndroidManifest.xml, 
    or does your intent not match its declared <intent-filter>?

🛠 解消手順

🔎 詳細を見る

AndroidManifest.xmlに以下の記述を追加する

AndroidManifest.xml
    <activity
        android:name=".ui.sample.SampleActivity"
        android:exported="false" />

手動でActivityのクラスファイルを追加時、AndroidManifest.xmlに自動登録されないので注意
AndroidManifestにある場合は、パッケージ名などを確認する。

それ以外にはパッケージ名が間違っている時などにも発生する場合がある。

https://b0npu.hatenablog.com/entry/2016/09/18/182623

💥 java.lang.RuntimeException

📄 エラーログ

Logcat
    java.lang.RuntimeException: Unable to start activity
    ComponentInfo{sample/sample.ui.sample.sample.sample.SampleActivity}:
    java.lang.NullPointerException: Attempt to invoke virtual method
    'sample.usecase.UseCases sample.MyApplication.getUseCases()'
    on a null object reference

🛠 解消手順

🔎 詳細を見る

AndroidManifest.xmlに以下の記述を確認する

AndroidManifest.xml
    <application
        android:name=".MyApplication"

この部分が間違っていると発生する場合がある。

変数定義や呼出元などを確認する

SampleActivity.java
    private MyApplication app;
    
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        app = MyApplication.of(getApplication());
    
        viewModel = ViewModelProviders
            .of(this, new SampleViewModel.Factory(
                    app.getUseCases()
                ))
            .get(SampleViewModel.class);
        // 以下略
    }

今回は MyApplication の変数定義部分を以下の書き方をしていたことが原因で発生していた。

🔎 原因の記述を見る
SampleActivity.java
// ここが原因
private MyApplication app = MyApplication.of(getApplication());

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    viewModel = ViewModelProviders
        .of(this, new SampleViewModel.Factory(
                app.getUseCases()
            ))
        .get(SampleViewModel.class);
    // 以下略   
}

今回変数定義の private MyApplication app; の記述と代入部分の app = MyApplication.of(getApplication()); をひとまとめに記述したことで発生していた。

SampleActivity.java
@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // これはOK(IDEの補完機能による修正)
    MyApplication app = MyApplication.of(getApplication());

    viewModel = ViewModelProviders
        .of(this, new SampleViewModel.Factory(
                app.getUseCases()
            ))
        .get(SampleViewModel.class);
    // 以下略   
}

それ以外にも渡している値の型が違う場合やクラスを呼出す時のクラス名が間違っている場合などに発生する。

https://qiita.com/tsukitoro0505/items/e16485918158f5d74d6f

💥 It looks like you just edited the targetSdkVersion from 34 to 35 in the editor

📄 エラーログ

Logcat
    It looks like you just edited the `targetSdkVersion` from 34 to 35 
    in the editor.
    Be sure to consult the documentation on the behaviors that change as 
    result of this.
    The Android SDK Upgrade Assistant can help with safely migrating.

🛠 解消手順

🔎 詳細を見る

compileSdkのバージョンを上げる

build.gradle(:app)
    applay plugin: 'com.android.application'

    android {
     compileSdk = 35
     buildToolsVersion "35.0.0"
 
     defaultConfig {
      applicationId "com.sample.sample"
      minSdk = 29 // OS10
      targetSdk = 35 // OS15
     }
    }

compileSdk のバージョンを 35 にあげる。

Android Gradle Pluginのバージョンを上げる

build.gradle(SAMPLEAPP-Android)
    buildscript {
     repositories{
      // 省略
     }
     dependencies {
      classpath 'com.android.tools.build:gradle:8.6.1'
     }
    }

AGP Upgrade Assistant を使用し、Android Gradle Plugin のバージョンを最低でも AGP 8.6.1 にあげる。

https://developer.android.com/about/versions/15/setup-sdk

🔎 ステーターバーの表示を元に戻す方法

Android15以降を搭載する端末で、targetSdkを35以降にすると Edge To Edge 表示がデフォルトになる。
これによりステータスバーの後ろにアプリの要素が回り込んだ表示となるが、場合によってはステータスバーの表示内容とアプリの要素が重なってしまう。

以下の記述を設定することで Edge To Edge を無効化して従来の表示形式でステータスバーを表示可能になる。

styles.xml
<!-- 以下を記述する -->
<item name="android:windowOptOutEdgeToEdgeEnforcement" tools:targetApi="35">true</item>

💥 Your build is currently configured to use Java XX and Gradle XX

📄 エラーログ

Logcat
    Your build is currently configured to use Java 17.0.1 and Gradle 6.7.1

🛠 解消手順

🔎 詳細を見る

使用中のGradleと対応するJDKをダウンロードする

File -> Project Structure -> Project からプロジェクトで使用中のGradleバージョンを確認する。

ProjectStracture.PNG

Settings -> Build, Execution, Deployment -> Buiild Tools -> Gradle を開き、
Gradle JDK の1つ目の項目を開いてDownload JDK...を開き、対応するJDKバージョンをダウンロードする。

Gradle.PNG

Gradle_2.PNG

https://stackoverflow.com/questions/76177599

https://docs.gradle.org/current/userguide/compatibility.html

https://zenn.dev/enjoy_nori/articles/252c903c504cbb

💥 can only be called from within the same library group prefix

📄 エラーログ

Logcat
    WindowCallbackWarpper can only be called from within the same library group 
    prefix
    (referenced groupId=androidx.appcompat with prefix androidx from 
    groupId=`SAMPLE`)

🛠 解消手順

🔎 詳細を見る

継承するクラスををエラーで指定されたものに変更する
もしくは別途専用で自作する

SampleWindowCallback.java
- public SampleWindowCallback extends WindowCallbackWarpper { ~ }
+ public SampleWindowCallback extends AppCompatActivity { ~ }

https://stackoverflow.com/questions/71533256/componentactivity-can-only-be-called-from-within-the-same-library-group-prefix

💥 android.view.WindowManager$BadTokenException

📄 エラーログ

Logcat
    android.view.WindowManager$BadTokenException:
    Unable to add window -- token null is not valid; is your activity running?

🛠 解消手順

🔎 詳細を見る

Activityを渡す
もしくはダイアログを別のクラスファイル(Activity)で定義してそこに遷移させる

https://qiita.com/le_skamba/items/455d5c37f7a2e853bc67

今回はダイアログを別クラスファイルで定義して呼び出す形で対応

MyApplication.java
    startActivity(new Intent(this,
    SampleDialog.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

合わせて以下のスタイル定義を記述し、

styles.xml
    <!-- 背景半透明化スタイル -->
    <style name="TransparencyTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

AndroidManifest.xmlに以下の様に記述

AndroidManifest.xml
    <activity
        android:exported="false"
        android:name=".ui.common.SampleDialog"
        android:theme="@style/TransparencyTheme" />
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?