DoFlamingo
DoFlamingo

Reputation: 232

extend my own BaseActivity cause java.lang.NoClassDefFoundError

Im new in AndroidStudio and want to create an app where i can compare QR-Codes.

I have an Activity called ScanActivity. This Activity scan a QR-Code an display the result. Now i want to add more Activities and for this i created a BaseActivity and all the other Activities extend this Activity. I've created all the Activities and the BaseActivitie but now when i change extend AppCompatActivity to extend BaseActivity i get this Error:

Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener; 

I've never saw that Error befor. After googled for hours and tried to set the sdkVersion down or change the action.MAIN activtiy to a complet empty but still get the same Error

Here is my BaseActivity when i start my Application this Activity check if the activity i want to open is already open

 public abstract class BaseActivity extends AppCompatActivity{

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

        }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {return true;}



        protected abstract void onCreateSpecific(Bundle savedInstanceState);

        protected abstract void next();

        protected abstract void back();


        private void switchToState(Bundle savedInstanceState) {
            switch (Persister.getAppState()){
                case NOTLOGGED:
                    checkActivity(savedInstanceState, MainActivity.class);
                case SCAN:
                    checkActivity(savedInstanceState, ScanActivity.class);
                case SECOND:
                    checkActivity(savedInstanceState, CheckActivity.class);
            }



        }

        private void checkActivity(Bundle savedInstanceState, Class destination) {
            if (getClass().equals(destination)) {
                callSpecific(savedInstanceState);
            } else {
                System.out.println("Start Activity");
                startActivity(new Intent(getApplicationContext(), destination));
            }
        }

        protected final void callSpecific(Bundle savedInstanceState){
            onCreateSpecific(savedInstanceState);
        }




    }

and this is my MainActivity witch is the first Activity gets called

public class MainActivity extends BaseActivity {
    @Override
    protected void onCreateSpecific(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void next() {

    }

    @Override
    protected void back() {

    }
}

and this is my gradle-file where i think the error occurs

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.mdk_prototype"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}



dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
   // implementation 'com.android.support:appcompat-v7:27.1.0'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

Thank for heelping

Upvotes: 0

Views: 147

Answers (1)

Jin-oh Kang
Jin-oh Kang

Reputation: 347

  1. You should really leave persistent state management to the Android framework to keep subtleties of Android from messing up the user experience, such as the split-screen mode and activity stacks. You can use startActivity directly to fire up another one, and call finish() beforehand if you don't want the user to come back to the original activity. If that's not an option, you can use fragments to avoid handling all the hastle of handling back button presses while still retaining the state transition and the structure of multiple "activities" under your control. For other states, please store them in the savedInstanceState, or if it shall survive across reboots, use shared preferences.
  2. The source of your problem, the androidx support library, is a little out of date. Please change the line:

    implementation 'androidx.appcompat:appcompat:1.0.2'
    

    to:

    implementation 'androidx.appcompat:appcompat:1.1.0'
    
  3. Although the error you posted clutters your log a lot, it may not be directly related to the problem and there's probably some other reason that happened to occur simultaneously with the OnUnhandledKeyEventListener problem. The error is caused by Android Runtime (the "Java VM") not being able to initialize some nested class properly since it uses the aforementioned new API that isn't supported by your emulator or phone your app runs on. The initialization is (somehow) triggered once again in this case, which fails because the VM remembers that an error has happened. There's an related issue tracker ticket but so far it seems to have no significant progress. It's probably a benign issue anyway, and I encourage you to post follow-up error logs so that we can investigate this issue a bit furthur. Remember that you can put them into GitHub Gists or Pastebin and link to it so that you can keep the question post tidy.

Upvotes: 1

Related Questions