Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

android - Navigation component not merging with current codebase

I have an issue am stuck on reading the usage of Navigation component, dagger and databinding.

my stackTrace on the error throw is

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: go.ke.antifgmboard.androidapp, PID: 16021
    java.lang.RuntimeException: Unable to start activity ComponentInfo{go.ke.antifgmboard.androidapp/go.ke.antifgmboard.androidapp.ui.signup.AuthActivity}: android.view.InflateException: Binary XML file line #36 in go.ke.antifgmboard.androidapp:layout/activity_auth: Binary XML file line #36 in go.ke.antifgmboard.androidapp:layout/activity_auth: Error inflating class androidx.fragment.app.FragmentContainerView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: android.view.InflateException: Binary XML file line #36 in go.ke.antifgmboard.androidapp:layout/activity_auth: Binary XML file line #36 in go.ke.antifgmboard.androidapp:layout/activity_auth: Error inflating class androidx.fragment.app.FragmentContainerView
     Caused by: android.view.InflateException: Binary XML file line #36 in go.ke.antifgmboard.androidapp:layout/activity_auth: Error inflating class androidx.fragment.app.FragmentContainerView
     Caused by: java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
        at go.ke.antifgmboard.androidapp.viewModel.MyViewModelFactory.create(MyViewModelFactory.java:37)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
        at go.ke.antifgmboard.androidapp.ui.signup.LandingFragment.onCreate(LandingFragment.java:34)
        at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
        at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175)
        at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2629)
        at androidx.fragment.app.FragmentManager.dispatchCreate(FragmentManager.java:2571)
        at androidx.fragment.app.Fragment.onCreate(Fragment.java:1685)
        at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:264)
        at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
        at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175)
        at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
        at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1818)
E/AndroidRuntime:     at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:303)
        at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:166)
        at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:51)
        at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
        at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
        at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:335)
        at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1069)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
        at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:303)
        at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284)
        at go.ke.antifgmboard.androidapp.ui.signup.AuthActivity.onCreate(AuthActivity.java:32)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
        at go.ke.antifgmboard.androidapp.util.PreferenceManager.<init>(PreferenceManager.java:12)
        at go.ke.antifgmboard.androidapp.di.AppModule.providesPreferenceManager(AppModule.java:19)
        at go.ke.antifgmboard.androidapp.di.AppModule_ProvidesPreferenceManagerFactory.providesPreferenceManager(AppModule_ProvidesPreferenceManagerFactory.java:37)
        at go.ke.antifgmboard.androidapp.di.AppModule_ProvidesPreferenceManagerFactory.get(AppModule_ProvidesPreferenceManagerFactory.java:27)
        at go.ke.antifgmboard.androidapp.di.AppModule_ProvidesPreferenceManagerFactory.get(AppModule_ProvidesPreferenceManagerFactory.java:10)
        at go.ke.antifgmboard.androidapp.repository.AuthenticationRepository_Factory.get(AuthenticationRepository_Factory.java:26)
        at go.ke.antifgmboard.androidapp.repository.AuthenticationRepository_Factory.get(AuthenticationRepository_Factory.java:9)
        at go.ke.antifgmboard.androidapp.viewModel.AuthenticationViewModel_Factory.get(AuthenticationViewModel_Factory.java:22)
        at go.ke.antifgmboard.androidapp.viewModel.AuthenticationViewModel_Factory.get(AuthenticationViewModel_Factory.java:8)
        at go.ke.antifgmboard.androidapp.viewModel.MyViewModelFactory.create(MyViewModelFactory.java:35)
            ... 55 more
I/Process: Sending signal. PID: 16021 SIG: 9

The complain Areas are

ViewModelFactory:

 public <T extends ViewModel> T create(Class<T> modelClass) {
        Provider<? extends ViewModel> creator = creators.get(modelClass);
        if (creator == null) {
            for (Map.Entry<Class<? extends ViewModel>, Provider<ViewModel>> entry : creators.entrySet()) {
                if (modelClass.isAssignableFrom(entry.getKey())) {
                    creator = entry.getValue();
                    break;
                }
            }
        }
        if (creator == null) {
            throw new IllegalArgumentException("unknown model class " + modelClass);
        }
        try {
            return (T) creator.get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

the Activity xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>
        <variable
            name="viewModel"
            type="go.ke.antifgmboard.androidapp.viewModel.AuthenticationViewModel" />
    </data>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.signup.AuthActivity">
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
        app:navGraph="@navigation/authentication_navigation"
        tools:ignore="FragmentTagUsage" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

the Activity

public class AuthActivity extends DaggerAppCompatActivity {
@Inject
MyViewModelFactory factory;
private ActivityAuthBinding binding;
private AuthenticationViewModel viewModel;

private NavHostFragment finalHost;
private NavController navController;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this,R.layout.activity_auth);
    viewModel = ViewModelProviders.of(this,factory).get(AuthenticationViewModel.class);
    binding.setViewModel(viewModel);
    finalHost = NavHostFragment.create(R.navigation.authentication_

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try to define your startDestination in your navigation.xml file.

Also, check if you have added this:

implementation "androidx.fragment:fragment:1.2.5"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...