I want to use databinding at the entrance to the navigation.
When I use fragment(android:name="NavHostFragment") in the activity, the program can run normally;
but when I use fragment in the activity (android:name="NavHostFragment") + databinding, the program will not run.
Please help me, I hope to use databinding at the entrance of the navigation, please tell me how to use it, thank you! !
Exception:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appnavigationtodo/com.example.appnavigationtodo.MainActivity}: android.view.InflateException: Binary XML file line #21: Binary XML file line #21: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3092)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3235)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6990)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
Caused by: android.view.InflateException: Binary XML file line #21: Binary XML file line #21: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #21: Duplicate id 0x7f080050, tag null, or parent id 0xffffffff with another fragment for androidx.navigation.fragment.NavHostFragment
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3752)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:120)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:405)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:387)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:303)
at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284)
at com.example.appnavigationtodo.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:7326)
at android.app.Activity.performCreate(Activity.java:7317)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3235)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6990)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding activityMainBinding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
}
}
activity_main.xml:
<layout 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">
<data>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ToolbarTheme" />
<fragment
android:id="#+id/fragmentMain"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_main" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/menu_bottom_navigation_view" />
</LinearLayout>
</layout>
You're calling setContentView twice, once with setContentView and a second time with DataBindingUtil.setContentView(). Instead, just use DataBindingUtil.setContentView(), which calls setContentView() itself internally:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding activityMainBinding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
}
}
Related
There are 3 causes of errors that I get in my logcat
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: java.lang.reflect.InvocationTargetException
below is my logcat.
Process: pnj.uas.aufar, PID: 4529
java.lang.RuntimeException: Unable to start activity ComponentInfo{pnj.uas.aufar/pnj.uas.aufar.TambahCatatanActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.google.android.material.textfield.TextInputLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2914)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at pnj.uas.aufar.TambahCatatanActivity.onCreate(TambahCatatanActivity.java:27)
at android.app.Activity.performCreate(Activity.java:7140)
at android.app.Activity.performCreate(Activity.java:7131)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:241)
at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:215)
2022-01-22 07:32:04.703 4529-4529/pnj.uas.aufar E/AndroidRuntime: at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:143)
at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:113)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:474)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:433)
... 28 more
Below file for TambahCatatanActivity.java
package pnj.uas.aufar;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class TambahCatatanActivity extends AppCompatActivity {
EditText edJudul, edJumlah, edTanggal;
Button btnSimpan;
RealmHelper realm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tambah_catatan);
realm = new RealmHelper(TambahCatatanActivity.this);
edJudul = findViewById(R.id.ed_judul);
edJumlah = findViewById(R.id.ed_jumlah);
edTanggal = findViewById(R.id.ed_tanggal);
btnSimpan = findViewById(R.id.btn_simpan);
edTanggal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//date picker
final Calendar calendar = Calendar.getInstance();
int nowYear = calendar.get(Calendar.YEAR);
int nowMonth = calendar.get(Calendar.MONTH);
int nowDay = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(TambahCatatanActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar cal = Calendar.getInstance();
cal.set(year, month, dayOfMonth);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
edTanggal.setText(dateFormat.format(cal.getTime()));
}
}, nowYear, nowMonth, nowDay);
dialog.show();
}
});
btnSimpan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CatatanModel catatan = new CatatanModel();
catatan.setId((int) realm.getNextId());
catatan.setJudul(edJudul.getText().toString());
catatan.setJumlahhutang(edJumlah.getText().toString());
catatan.setTanggal(edTanggal.getText().toString());
realm.insertData(catatan);
}
});
}
}
And the last one is the file for activity_tambah_catatan.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".TambahCatatanActivity">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/ed_judul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hutang Anda" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/ed_jumlah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Jumlah Hutang"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/ed_tanggal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:hint="Tanggal Jatuh Tempo" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="#+id/btn_simpan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:text="Simpan"
android:textColor="#android:color/white" />
</LinearLayout>
build.gradle add implementation com.google.android.material:material:1.3.0
I've been struggling on this the whole day and still can't make it work. In an existing Single Activity app, I'm trying to replace all fragment transactions by implementing the Navigation component.
I've already created a Navigation graph and included the FragmentContainerView as a child of the root ViewGroup as it was proposed in the official docs.
activity_main.xml
<FrameLayout
android:id="#+id/container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/appBarLayout">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/main_content"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/appBarLayout"
app:navGraph="#navigation/nav_graph" />
</FrameLayout>
Now the problem that I'm currently facing is that the SharedViewModel which I am using through the whole app can't be instantiated in the BaseFragment anymore.
BaseFragment.kt
abstract class BaseFragment<E : ViewDataBinding, T : BaseViewModel> : Fragment() {
lateinit var viewModel: T
lateinit var dataBinding: E
abstract fun getLayoutResource(): Int
abstract fun getViewModelClass(): Class<T>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity?.let {
// java.lang.RuntimeException: Cannot create an instance of class SharedViewModel
viewModel = ViewModelProvider(it).get(getViewModelClass())
// viewModel = ViewModelProvider(getParentFragment()).get(getViewModelClass()) also doesn't work
}
}
...
}
BaseViewModel.kt
abstract class BaseViewModel : ViewModel(), LifecycleObserver
SharedViewModel.kt
abstract class SharedViewModel : BaseViewModel() {
...
}
SharedViewModelImpl.kt
class SharedViewModelImpl(private val repository: AppRepository) : SharedViewModel(){
...
}
I've read posts on SO and as well as Medium articles that recommend creating the SharedViewModel by using by navGraphViewModels like this
val viewModel: SharedViewModel by navGraphViewModels(R.id.navigation_graph)
but it seems that I still can't combine this approach with the generics that I already have.
My assumption is that the Navigation component can't deal with abstract classes.
How can I overcome this problem by keeping my existing app architecture? Many thanks in advance.
UPDATE:
#ianhanniballake asked me to post the entire stack trace. So here it is:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: at.bwappsandmore.doitagain, PID: 13528
java.lang.RuntimeException: Unable to start activity ComponentInfo{at.bwappsandmore.doitagain/at.bwappsandmore.doitagain.ui.MainActivity}: android.view.InflateException: Binary XML file line #44: Binary XML file line #44: Error inflating class androidx.fragment.app.FragmentContainerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #44: Binary XML file line #44: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: java.lang.RuntimeException: Cannot create an instance of class at.bwappsandmore.doitagain.viewModel.SharedViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:106)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at at.bwappsandmore.doitagain.base.BaseFragment.onCreate(BaseFragment.kt:31)
at at.bwappsandmore.doitagain.ui.DisplayDataFragment.onCreate(DisplayDataFragment.kt:85)
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)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:303)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:166)
E/AndroidRuntime: 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.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
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 at.bwappsandmore.doitagain.base.BaseActivity.onCreate(BaseActivity.kt:27)
at at.bwappsandmore.doitagain.ui.MainActivity.onCreate(MainActivity.kt:46)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.InstantiationException: java.lang.Class<at.bwappsandmore.doitagain.viewModel.SharedViewModel> cannot be instantiated
at java.lang.Class.newInstance(Native Method)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
... 60 more
Whenever I assign an android:onClick attribute to an AutoCompleteTextView, my application immediately crashes upon loading the activity. I've tested AutoCompleteTextView without assigning an onClick method and it seems to work fine, as does assigning onClick to other views (including EditText, Spinners, TextViews, Buttons and ImageButtons). I've managed to narrow down the error to specifically to line 24 (setContentView(R.layout.activity_main)). Can anyone shed some light on this?
MainActivity.java
package com.example.matt.testapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
public AutoCompleteTextView actv;
public String[] continents = {"North America",
"South America",
"Antarctica",
"Europe",
"Africa",
"Asia",
"Australia"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actv = (AutoCompleteTextView) findViewById(R.id.actv);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, continents);
actv.setAdapter(adapter);
}
public void actvClick (View view) {}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent" >
<AutoCompleteTextView
android:id="#+id/actv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="actvClick" >
</AutoCompleteTextView>
</LinearLayout>
Logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.matt.testapp/com.example.matt.testapp.MainActivity}: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class AutoCompleteTextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class AutoCompleteTextView
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class AutoCompleteTextView
Caused by: java.lang.NullPointerException: Attempt to write to field 'android.view.View$OnClickListener android.widget.AutoCompleteTextView$PassThroughClickListener.mWrapped' on a null object reference
at android.widget.AutoCompleteTextView$PassThroughClickListener.-set0(Unknown Source:0)
at android.widget.AutoCompleteTextView.setOnClickListener(AutoCompleteTextView.java:307)
at android.view.View.<init>(View.java:5008)
at android.widget.TextView.<init>(TextView.java:824)
at android.widget.EditText.<init>(EditText.java:88)
at android.widget.AutoCompleteTextView.<init>(AutoCompleteTextView.java:220)
at android.widget.AutoCompleteTextView.<init>(AutoCompleteTextView.java:193)
at android.widget.AutoCompleteTextView.<init>(AutoCompleteTextView.java:172)
at android.support.v7.widget.AppCompatAutoCompleteTextView.<init>(AppCompatAutoCompleteTextView.java:68)
at android.support.v7.widget.AppCompatAutoCompleteTextView.<init>(AppCompatAutoCompleteTextView.java:64)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:130)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1026)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1083)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:772)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.matt.testapp.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
This looks like to be a bug in AutoCompleteTextView.
What happens:
Inflating the XML invokes AutoCompleteTextView constructor, and it invokes View constructor.
View constructor sees the onClick attribute and calls setOnClickListener().
AutoCompleteTextView has overridden setOnClickListener() which writes to an internal mPassThroughClickListener field. It is only initialized at the end of AutoCompleteTextView constructor, and this init code has not been run yet.
Reference
To work around it, remove the onClick attrtibute from your XML and call setOnClickListener() programmatically in your code.
I filed a bug about this in Android issue tracker.
I am trying to add a bottom navigation view of support library in my app. Here is the code below. I am getting error like
Caused by: android.view.InflateException: Binary XML file line #52: Binary XML file line #52: Error inflating class android.support.design.widget.BottomNavigationView
But my xml code is looking fine also. And the gradle dependency version of support and appcompat are also equal. I couldn't figure out the error. Can someone help me to figure out the issue?
XML code is
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:design="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/rl_container">
</RelativeLayout>
</LinearLayout>
<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bottom_navigation"
android:layout_alignParentBottom="true"
app:itemIconTint="#color/drawer_item"
app:itemBackground="#color/brand_black"
app:itemTextColor="#color/drawer_item"
app:menu="#menu/drawer"/>
</android.support.v4.widget.DrawerLayout>
And my java code is
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
///getSupportActionBar().setDisplayShowTitleEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
BnavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
SharedPreferences sP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String user = sP.getString("user", null);
sessionManager = SessionManager.getInstance(this);
if(user == null ) {
finish();
startActivity(new Intent(getApplicationContext(), Splash.class));
return;
}
BnavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
onDrawerMenuItemClicked(item);
return true;
}
});
Error report is given below
04-22 11:23:13.665 5112-5112/me.entri.entrime E/AndroidRuntime: FATAL EXCEPTION: main
Process: me.entri.entrime, PID: 5112
java.lang.RuntimeException: Unable to start activity ComponentInfo{me.entri.entrime/me.entri.entrime.activities.Home}: android.view.InflateException: Binary XML file line #52: Binary XML file line #52: Error inflating class android.support.design.widget.BottomNavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2680)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2741)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6176)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: android.view.InflateException: Binary XML file line #52: Binary XML file line #52: Error inflating class android.support.design.widget.BottomNavigationView
Caused by: android.view.InflateException: Binary XML file line #52: Error inflating class android.support.design.widget.BottomNavigationView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
at me.entri.entrime.activities.Home.onCreate(Home.java:87)
at android.app.Activity.performCreate(Activity.java:6688)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2741)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6176)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.internal.BottomNavigationItemView.initialize(android.support.v7.view.menu.MenuItemImpl, int)' on a null object reference
and my gradle dependencies are
compile 'com.android.support:support-v13:25.0.1'
compile 'com.android.support:support-v4:25.0.1'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:design:25.0.1'
compile 'com.android.support:cardview-v7:25.0.1'
Use only this dependency:
compile 'com.android.support:design:25.0.1'
Remove other two dependencies:
compile 'com.android.support:support-v13:25.0.1' compile
'com.android.support:support-v4:25.0.1'
I am trying to understand how fragments work, but i haven't had sucess so far.
This is only an example nothing usefull.
when i try to add a fragment to the layout I get always the same error.
I don't know what is missing
Fragment Class
package com.app.danieltavares.teste;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TestFragment extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.testefrag,container,false);
}
}
ActivityClass
package com.app.danieltavares.teste;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Layout Fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="diusdb"/>
</LinearLayout>
Layout that uses the fragments
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.app.danieltavares.teste.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.app.danieltavares.teste.TestFragment"
tools:layout="#layout/testefrag"></fragment>
</RelativeLayout>
Logcat
08-23 18:35:21.867 15308-15308/com.app.danieltavares.teste E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.danieltavares.teste, PID: 15308
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.danieltavares.teste/com.app.danieltavares.teste.MainActivity}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #16: Must specify unique android:id, android:tag, or have a parent with an id for com.app.danieltavares.teste.TestFragment
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2282)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:5884)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.app.danieltavares.teste.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #16: Must specify unique android:id, android:tag, or have a parent with an id for com.app.danieltavares.teste.TestFragment
Add an android:id attribute to the <fragment> element in your layout file.