android.view.InflateException when switching to landscape mode - java

I am trying to make an app that involves two content mains, one being for portrait the other being for landscape, However, when I switch the orientation to landscape, the app crashes. Portrait and Landscape have different layouts, the only notable difference between the two is that landscape has one more fragment within its layout.
I want to iterate that this is my first time working with landscape mode in android studio so if I made any rookie mistakes, i'm sure you'll be forgiving.
Any input is appreciated, thanks in advance!
Settings Fragment.java
package com.example.celebquizapp;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
public class SettingsFragment extends Fragment implements View.OnClickListener {
private SharedPreferences preferences;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragment_view = inflater.inflate(R.layout.fragment_settings, container, false);
preferences = this.getActivity().getSharedPreferences("values", Context.MODE_PRIVATE);
return fragment_view;
}
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RadioGroup selectionOption = getActivity().findViewById(R.id.selection_option_setting);
setRadioButtonListener(selectionOption);
}
public void onClick(View v) {
RadioButton button = (RadioButton) v;
boolean checked = button.isChecked();
if (checked) {
int chosenVal = Integer.parseInt(String.valueOf(button.getText()));
Log.d("RADIOS", "Current value chosen: " + chosenVal);
preferences.edit().putInt("numofopt", chosenVal).apply();
}
}
public void setRadioButtonListener(RadioGroup radioGroup) {
for (int i = 0; i < radioGroup.getChildCount(); i++) {
radioGroup.getChildAt(i).setOnClickListener(this);
}
}
#Override
public void onStart() {
super.onStart();
// Not required to save data in preferences
// So delete on start up
preferences.edit().clear().apply();
}
}
Settings Fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".SettingsFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/selectionOptionLabel"
android:textColor="#color/colorBlack"
android:textSize="20sp"
android:layout_marginTop="0dp"
android:padding="10dp"/>
<RadioGroup
android:id="#+id/selection_option_setting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textSize="20sp"
android:text="#string/_2"
android:layout_marginLeft="10dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="#string/_3"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="#string/_4"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="#string/_5"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="#string/_6"/>
</RadioGroup>
</LinearLayout>
Error message
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.celebquizapp, PID: 11657
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.celebquizapp/com.example.celebquizapp.MainActivity}: android.view.InflateException: Binary XML file line #22 in com.example.celebquizapp:layout/activity_main: Binary XML file line #26 in com.example.celebquizapp:layout/content_main: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5279)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5187)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
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 #22 in com.example.celebquizapp:layout/activity_main: Binary XML file line #26 in com.example.celebquizapp:layout/content_main: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #26 in com.example.celebquizapp:layout/content_main: Error inflating class fragment
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getChildCount()' on a null object reference
at com.example.celebquizapp.SettingsFragment.setRadioButtonListener(SettingsFragment.java:50)
at com.example.celebquizapp.SettingsFragment.onViewCreated(SettingsFragment.java:32)
at androidx.fragment.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManagerImpl.java:1144)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:851)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1133)
at androidx.fragment.app.FragmentManagerImpl.addFragment(FragmentManagerImpl.java:1393)
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManagerImpl.java:3205)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:134)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:336)
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.parseInclude(LayoutInflater.java:1263)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1119)
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:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.celebquizapp.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:7802)
E/AndroidRuntime: 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.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5279)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5187)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
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)

RadioGroup selectionOption = getActivity().findViewById(R.id.selection_option_setting);
It should be
RadioGroup selectionOption = view.findViewById(R.id.selection_option_setting);

Related

Getting Null Object reference on Edittext

I know that this question looks similar to others on SO but i have tried all the suggestions in the other similar questions and haven't had any success.
I'm getting this error:
2020-08-03 20:41:21.372 8916-8916/com.example.spacing E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.spacing, PID: 8916
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.spacing/com.example.spacing.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
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.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:159)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.content.Context.obtainStyledAttributes(Context.java:675)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
at com.example.spacing.MainActivity.<init>(MainActivity.java:15)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
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) 
2020-08-03 20:41:21.411 8916-8916/com.example.spacing I/Process: Sending signal. PID: 8916 SIG: 9
on this code:
package com.example.spacing;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public int firstValue;
EditText first;
String firstString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first = (EditText)findViewById(R.id.firstNum);
firstString = first.getText().toString();
Button Go = (Button) findViewById(R.id.goButton);
Go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
firstValue = Integer.parseInt(firstString);
System.out.print(firstValue);
}
});
}
}
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<Button
android:id="#+id/goButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.898" />
<TextView
android:id="#+id/FirstAttribute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/spacedMaterial"
app:layout_constraintBottom_toTopOf="#+id/goButton"
app:layout_constraintEnd_toStartOf="#+id/firstNum"
app:layout_constraintHorizontal_bias="0.457"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.043" />
<EditText
android:id="#+id/firstNum"
android:layout_width="165dp"
android:layout_height="40dp"
android:ems="10"
android:hint="#string/inMM"
android:importantForAutofill="no"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/FirstAttribute"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.934"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/FirstAttribute"
app:layout_constraintVertical_bias="0.476" />
<TextView
android:id="#+id/secondAttribute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/amount"
app:layout_constraintBottom_toTopOf="#+id/goButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.073"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.131" />
<EditText
android:id="#+id/secondNumber"
android:layout_width="165dp"
android:layout_height="40dp"
android:ems="10"
android:hint="#string/number"
android:importantForAutofill="no"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/secondAttribute"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.934"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/secondAttribute" />
<TextView
android:id="#+id/thirdAttribute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/areaWidth"
app:layout_constraintBottom_toTopOf="#+id/goButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.068"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.218" />
<EditText
android:id="#+id/thirdNumber"
android:layout_height="40dp"
android:layout_width="165dp"
android:ems="10"
android:hint="#string/inMM"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/thirdAttribute"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.934"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/thirdAttribute"
app:layout_constraintVertical_bias="0.476"
android:importantForAutofill="no" />
</androidx.constraintlayout.widget.ConstraintLayout>
Suggestions i have tried:
I have tried changing the init for the edit text to before and after on-create.
I have made sure that all ids are correct and are referencing the right thing.
And about 5 other things that i cant recall but none of them worked when i tried to build.
Any help is appreciated.
Problem is:
you are trying to initialize the "firstString" variable OUTSIDE of the click listener and as soon as the Activity is created, which means the variable will always be a null object because you did not actually write something in the EditText.
Simply initialize the String inside the ClickListener!
` package com.example.spacing;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public int firstValue;
EditText first;
String firstString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first = (EditText)findViewById(R.id.firstNum);
Button Go = (Button) findViewById(R.id.goButton);
Go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
firstString = first.getText().toString();
firstValue = Integer.parseInt(firstString);
System.out.print(firstValue);
}
});
}
}`

Inflating a fragment in main activity brings an error

I want to be able to swap between two fragments in my main activity class. But when inflating the fragment my application crashes. Below is my code:
This is the XML code where my fragment will be placed
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_dialog"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="56dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<fragment
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"/>
</LinearLayout>
This is the code for my Fragment
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.user.swim.AsyncTasks.GeoCodingTask;
public class SearchLocation extends Fragment {
private EditText destination;
private Button search;
private RecyclerView recyclerView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_search_locatiom, null);
search = view.findViewById(R.id.search_destination);
destination = view.findViewById(R.id.destination);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
searchLocation(view);
return view;
}
Below is the code in my main activity to inflate the layout
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(findViewById(R.id.fragment_place)!=null){
if (savedInstanceState != null) {
return;
}
SearchLocation fragment = new SearchLocation();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_place, fragment).commit();
}
I get the following errors in my logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.swim, PID: 20462
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.swim/com.example.user.swim.MainActivity}: android.view.InflateException: Binary XML file line #29: Binary XML file line #14: Error inflating class fragment
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:6540)
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 #29: Binary XML file line #14: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
Caused by: java.lang.NullPointerException
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.Fragment.instantiate(Fragment.java:617)
at android.app.FragmentContainer.instantiate(FragmentContainer.java:49)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3598)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:6182)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:338)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:783)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:827)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:995)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:862)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:827)
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 androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.user.swim.MainActivity.onCreate(MainActivity.java:87)
at android.app.Activity.performCreate(Activity.java:6980)
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:6540)
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 is the XML for fragment_search_locatiom
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:layout_margin="10dp"
android:text="#string/where_would_you_like_to_go"
android:textAlignment="center" />
<EditText
android:id="#+id/destination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="10dp"
android:drawablePadding="5dp"
android:drawableStart="#drawable/ic_search_black_24dp"
android:drawableLeft="#drawable/ic_search_black_24dp"
android:textSize="18sp"
android:inputType="text"
android:background="#drawable/rectangle"
android:hint="Search for destination"
android:maxLines="1"
android:imeOptions="actionDone"
android:textAlignment="textStart" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/search_destination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/search_destination" />
</LinearLayout>
I don't want to add the name/class attribute in the XML for the fragment because when you look at the official documentation adding those attributes won't make it possible to change fragments at runtime.
Also i've tried changing the MainActivity class to extend ActivityFragment but after more research i've discovered that ActivityFragment is a subclass of AppCompatActivity.
i have copied your code and try to use it,when i use fragment in the SearchLocation it makes error so i changed it to Framlayout.
Can't realy tell what is wrong in your code but this worked after i made changes
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_place) != null) {
if (savedInstanceState != null) {
return;
}
SearchLocation fragment = new SearchLocation();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_place, fragment).commit();
}
}
SearchLocation java
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class SearchLocation extends Fragment {
private EditText mSearch;
private Button mDestination;
private RecyclerView mRecyclerView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search_location, null);
mSearch = view.findViewById(R.id.search_destination);
mDestination = view.findViewById(R.id.destination);
mSearch.setText("Hello world");
RecyclerView mRecyclerView = view.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
/*searchLocation(view);*/
return view;
}
search_location xml
<?xml version="1.0" encoding="utf-8"?>
<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=".SearchLocation">
<EditText
android:id="#+id/search_destination"
android:layout_width="match_parent"
android:layout_height="0dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/destination"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/search_destination" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/destination" />
</androidx.constraintlayout.widget.ConstraintLayout>
You can try with Framelayout instead of fragment like the following
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" />

My Android app crashes while trying to start a new activity using Intents [duplicate]

This question already has answers here:
How to fix Error inflating class null
(2 answers)
Closed 4 years ago.
Am new to Android programming and I decided to build a quiz app. I built the layout and wanted to set up a button to start a new activity which begins the quiz. I also set up the method in the MainActivity correctly but when I run the app and click on the button, the app crashes and leaves a ton of error messages on logcat.
Any help will be appreciated. Below are the codes.
XML Code:
<Button
android:id="#+id/button_view"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_below="#id/name_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/button_round"
android:fontFamily="sans-serif-light"
android:inputType="textCapWords"
android:text="Go!"
android:onClick="startQuestionOne"
android:textSize="24dp" />
MainActivity
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void startQuestionOne(View view) {
Intent myIntent = new Intent(LoginActivity.this, QuestionOneActivity.class);
startActivity(myIntent);
}
}
Logcat Message
06-25 18:32:34.259 13705-13705/com.example.android.udacityquizapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.udacityquizapp, PID: 13705
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.udacityquizapp/com.example.android.udacityquizapp.QuestionOneActivity}: android.view.InflateException: Binary XML file line #23: Error inflating class null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2344)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class null
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.android.udacityquizapp.QuestionOneActivity.onCreate(QuestionOneActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5279)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396) 
at android.app.ActivityThread.access$800(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5328) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:99)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1035)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1092)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
at com.example.android.udacityquizapp.QuestionOneActivity.onCreate(QuestionOneActivity.java:11) 
at android.app.Activity.performCreate(Activity.java:5279) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396) 
at android.app.ActivityThread.access$800(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5328) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
at dalvik.system.NativeStart.main(Native Method) 
This is the full XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#drawable/gradient"
tools:context=".LoginActivity">
<ImageView
android:id="#+id/udacity_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="#drawable/udacity" />
<TextView
android:id="#+id/textview_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/udacity_logo"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:fontFamily="sans-serif-ligh"
android:text="Udacity Quiz App"
android:textColor="#faebd7"
android:textSize="36dp"
android:textStyle="bold" />
<EditText
android:id="#+id/name_view"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_below="#id/textview_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/edit_round"
android:drawableLeft="#drawable/ic_action_user"
android:ems="10"
android:fontFamily="sans-serif-light"
android:hint="Name"
android:inputType="textCapWords"
android:textColor="#000000"
android:textColorHint="#000000"
android:textSize="16dp" />
<Button
android:id="#+id/button_view"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_below="#id/name_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/button_round"
android:fontFamily="sans-serif-light"
android:inputType="textCapWords"
android:text="Go!"
android:onClick="startQuestionOne"
android:textSize="24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:fontFamily="sans-serif-light"
android:text="powered by: #William"
android:textColor="#faebd7" />
</RelativeLayout>
Above is the XML for LoginActivity which contains a button. When the button is clicked, its supposed to start a new activity called QuestionOneActivity, but instead, the app crashes.
LoginActivity
package com.example.android.udacityquizapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void startQuestionOne(View view) {
Intent myIntent = new Intent(LoginActivity.this,
QuestionOneActivity.class);
startActivity(myIntent);
}
}
firstly bind Button in onCreate() as
Button b = (Button) findViewById(R.id.button_view);
then check if your activity QuestionOneActivity is declared in manifest file
after that check QuestionOneActivity activity xml file for error if have any...
Thanks clay_to_n, based on your suggestions, i found out that i used a lower case 'v' for my View tag. When i adjusted it, the app runs as expected.

Attempt to invoke virtual method Android Studio RecyclerView android.support.v7 [duplicate]

This question already has answers here:
Null pointer Exception - findViewById()
(12 answers)
Closed 5 years ago.
Hi i'm having a problem with my app, its crashing when ever i press button called "Riders". The error that i'm getting is:
12-13 10:40:03.467 16463-16463/bskerritt.student.ncirl.ie.saddleup E/AndroidRuntime: FATAL EXCEPTION: main
Process: bskerritt.student.ncirl.ie.saddleup, PID: 16463
java.lang.RuntimeException: Unable to start activity ComponentInfo{bskerritt.student.ncirl.ie.saddleup/bskerritt.student.ncirl.ie.saddleup.RiderActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at bskerritt.student.ncirl.ie.saddleup.RiderActivity.onCreate(RiderActivity.java:54)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
I'v been trying to solve this for ages now. The error is referencing the error to the page called RiderActivity:
package bskerritt.student.ncirl.ie.saddleup;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
public class RiderActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Contact> list = new ArrayList<Contact>();
int[] image_id = { R.drawable.lilyheadshot, R.drawable.aaronheadshot, R.drawable.willowheadshot,
R.drawable.kidheadshot1, R.drawable.kidheadshot2, R.drawable.kidheadshot3, R.drawable.kidheadshot4,
R.drawable.kidheadshot5 };
String[] name, mobile, address;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rider);
name = getResources().getStringArray(R.array.rider_name);
mobile = getResources().getStringArray(R.array.rider_mobile);
address = getResources().getStringArray(R.array.rider_address);
int count = 0;
for(String Name: name) {
Contact contact = new Contact(image_id[count], Name, mobile[count], address[count]);
count++;
list.add(contact);
}
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter = new ContactAdapter(list);
recyclerView.setAdapter (adapter);
}
}
I'm using android studio, any help will be appreciated. I was trying few solutions that are on stackoverflow but non worked. Please help. Thanks !
This is the code for my activity_rider:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bskerritt.student.ncirl.ie.saddleup.RiderActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/RecyclerView">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
>
<ImageView
android:id="#+id/rider_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/lilyheadshot" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/rider_image"
android:text="Lily Skerritt"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:layout_alignParentTop="true"
android:id="#+id/rider_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rider_name"
android:layout_toRightOf="#id/rider_image"
android:text="0872287760"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:id="#+id/rider_mobile"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rider_mobile"
android:layout_toRightOf="#id/rider_image"
android:text="Laytown, Meath"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:id="#+id/rider_address"
/>
</RelativeLayout>
</android.support.v7.widget.RecyclerView>
<Button
android:id="#+id/button5"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:text="#string/addRiderBtn"
android:textColor="#color/suPrimary"
android:textSize="18sp"
android:textStyle="bold" />
Also regarding the other code provided with context, i'm getting this error: http://prntscr.com/hmw2yi
Okay so the first error has been fixed, changing the R.id.RecyclerView did it but for some reason the error now occurs on different line:
12-13 11:10:11.590 2486-2486/bskerritt.student.ncirl.ie.saddleup E/AndroidRuntime: FATAL EXCEPTION: main
Process: bskerritt.student.ncirl.ie.saddleup, PID: 2486
java.lang.RuntimeException: Unable to start activity ComponentInfo{bskerritt.student.ncirl.ie.saddleup/bskerritt.student.ncirl.ie.saddleup.RiderActivity}: android.view.InflateException: Binary XML file line #15: RecyclerView has no LayoutManager
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.view.InflateException: Binary XML file line #15: RecyclerView has no LayoutManager
Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager
at android.support.v7.widget.RecyclerView.generateLayoutParams(RecyclerView.java:3996)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:860)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
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:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at bskerritt.student.ncirl.ie.saddleup.RiderActivity.onCreate(RiderActivity.java:36)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
12-13 11:11:00.875 2486-2494/bskerritt.student.ncirl.ie.saddleup W/art: Suspending all threads took: 6.024ms
Use this
recyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
Instead of this
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
EDIT
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bskerritt.student.ncirl.ie.saddleup.RiderActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/RecyclerView"/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
>
<ImageView
android:id="#+id/rider_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/lilyheadshot" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/rider_image"
android:text="Lily Skerritt"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:layout_alignParentTop="true"
android:id="#+id/rider_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rider_name"
android:layout_toRightOf="#id/rider_image"
android:text="0872287760"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:id="#+id/rider_mobile"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rider_mobile"
android:layout_toRightOf="#id/rider_image"
android:text="Laytown, Meath"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:id="#+id/rider_address"
/>
</RelativeLayout>
<Button
android:id="#+id/button5"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:text="#string/addRiderBtn"
android:textColor="#color/suPrimary"
android:textSize="18sp"
android:textStyle="bold" />
Please specify you are using same id in .xml file of recycler view
Second thing-:
package bskerritt.student.ncirl.ie.saddleup;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
public class RiderActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
private Context context=this;
ArrayList<Contact> list = new ArrayList<Contact>();
int[] image_id = {
R.drawable.lilyheadshot,
R.drawable.aaronheadshot,
R.drawable.willowheadshot,
R.drawable.kidheadshot1,
R.drawable.kidheadshot2,
R.drawable.kidheadshot3,
R.drawable.kidheadshot4,
R.drawable.kidheadshot5
};
String[] name, mobile, address;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rider);
name = getResources().getStringArray(R.array.rider_name);
mobile = getResources().getStringArray(R.array.rider_mobile);
address = getResources().getStringArray(R.array.rider_address);
int count = 0;
for(String Name: name){
Contact contact = new Contact(image_id[count], Name, mobile[count], address[count]);
count++;
list.add(contact);
}
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter = new ContactAdapter(list);
recyclerView.setAdapter (adapter);
}}'
Use context in case of this as I have changed the code.

Using Butterknife to Bind EditText from DialogFragment

I am attempting to get Strings from 2 Edittexts within a Dialog Fragment, but my app keeps crashing when the activity starts. It is some sort of issue with being able to bind the edittexts from the dialog. I am not sure of the correct way to bind them.
Here is the crash:
09-24 11:34:30.366 16147-16147/com.epicodus.concertaid E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.epicodus.concertaid, PID: 16147
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.epicodus.concertaid/com.epicodus.concertaid.ui.UserProfileActivity}: java.lang.RuntimeException: Unable to bind views for com.epicodus.concertaid.ui.UserProfileActivity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Unable to bind views for com.epicodus.concertaid.ui.UserProfileActivity
at butterknife.ButterKnife.bind(ButterKnife.java:322)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at com.epicodus.concertaid.ui.UserProfileActivity.onCreate(UserProfileActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.IllegalStateException: Required view 'userEmailEditText' with ID 2131558627 for field 'mUserEmailEditText' was not found. If this view is optional add '#Nullable' annotation.
at butterknife.ButterKnife$Finder.findRequiredView(ButterKnife.java:140)
at com.epicodus.concertaid.ui.UserProfileActivity$$ViewBinder.bind(UserProfileActivity$$ViewBinder.java:15)
at com.epicodus.concertaid.ui.UserProfileActivity$$ViewBinder.bind(UserProfileActivity$$ViewBinder.java:8)
at butterknife.ButterKnife.bind(ButterKnife.java:319)
at butterknife.ButterKnife.bind(ButterKnife.java:237) 
at com.epicodus.concertaid.ui.UserProfileActivity.onCreate(UserProfileActivity.java:37) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
And here is the Activity:
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.epicodus.concertaid.Constants;
import com.epicodus.concertaid.R;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import butterknife.Bind;
import butterknife.ButterKnife;
public class UserProfileActivity extends AppCompatActivity implements View.OnClickListener {
#Bind(R.id.summaryTextView) TextView mSummaryTextView;
#Bind(R.id.deleteAccountButton) Button mDeleteAccountButton;
#Bind(R.id.userEmailEditText) EditText mUserEmailEditText;
#Bind(R.id.userPasswordEditText) EditText mUserPasswordEditText;
private Firebase mFirebaseRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
ButterKnife.bind(this);
mDeleteAccountButton.setOnClickListener(this);
//GET REFERENCE TO FIREBASE
mFirebaseRef = new Firebase(Constants.FIREBASE_URL);
//SETS FONT FOR TITLE
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MUSICNET.ttf");
mSummaryTextView.setTypeface(tf);
}
#Override
public void onClick(View view) {
if(view == mDeleteAccountButton) {
createAlertDialog();
}
}
public void deleteUser(String userEmail, String userPassword) {
Firebase.ResultHandler handler = new Firebase.ResultHandler() {
#Override
public void onSuccess() {
Toast.makeText(UserProfileActivity.this, "User deleted Successflly", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FirebaseError firebaseError) {
Toast.makeText(UserProfileActivity.this, "There was an error, please try again", Toast.LENGTH_LONG).show();
}
};
mFirebaseRef.removeUser(userEmail, userPassword, handler );
}
public void createAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(UserProfileActivity.this);
LayoutInflater inflater = UserProfileActivity.this.getLayoutInflater();
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
builder.setView(inflater.inflate(R.layout.delete_user_dialog, null));
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
String userEmail = mUserEmailEditText.getText().toString();
String userPassword = mUserPasswordEditText.getText().toString();
deleteUser(userEmail, userPassword);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
This is the layout for the activity
<?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"
tools:context="com.epicodus.concertaid.ui.UserProfileActivity"
android:id="#+id/relativeLayout">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageView"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:layout_centerHorizontal="true"
android:src="#drawable/background1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account Details"
android:textColor="#0288D1"
android:id="#+id/summaryTextView"
android:layout_alignParentTop="true"
android:textSize="30sp"
android:layout_centerInParent="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Photo"
android:textColor="#0288D1"
android:background="#drawable/buttonshape"
android:id="#+id/addPhotoButton"
android:layout_below="#+id/summaryTextView"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginTop="25dp" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/userPhotoImageView"
android:layout_marginTop="25dp"
android:layout_below="#+id/addPhotoButton"
android:layout_centerHorizontal="true"
android:contentDescription="#string/current_user_s_image"
android:src="#drawable/blank_user" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Email"
android:textColor="#0288D1"
android:id="#+id/changeEmailButton"
android:background="#drawable/buttonshape"
android:layout_below="#+id/userPhotoImageView"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginTop="25dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Password"
android:textSize="25sp"
android:textColor="#0288D1"
android:layout_marginTop="25dp"
android:background="#drawable/buttonshape"
android:id="#+id/changePasswordEmailButton"
android:layout_below="#+id/changeEmailButton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Account"
android:textSize="25sp"
android:textColor="#0288D1"
android:layout_marginTop="25dp"
android:id="#+id/deleteAccountButton"
android:layout_below="#+id/changePasswordEmailButton"
android:layout_centerHorizontal="true"
android:background="#drawable/buttonshape" />
Here is the layout for the AlertDialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/userEmailEditText"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="Enter Email" />
<EditText
android:id="#+id/userPasswordEditText"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="Enter Password"/>
</LinearLayout>
Any example to show how to correctly Bind the mUserEmailEditText and mUserPasswordEditText
Thank you
When you use bind like that is supposed to be from a existing ID in your activity layout, since you dont have it in your xml it will crash.. Dont bind the EditText and just create them in the AlertDialog, and im use setParams to change their layout
#Bind(R.id.userEmailEditText) EditText mUserEmailEditText;
#Bind(R.id.userPasswordEditText) EditText mUserPasswordEditText;
You can not bind the view from the xml which is not inflated as part of Activity's view. So remove #Bind from above line as follows.
EditText mUserEmailEditText;
EditText mUserPasswordEditText;
and inflate them while creating your dialog in createAlertDialog() method. So replace following 2 lines
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
builder.setView(inflater.inflate(R.layout.delete_user_dialog, null));
with
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
mUserEmailEditText = rootView.findViewById(R.id.userEmailEditText);
mUserPasswordEditText = rootView.findViewById(R.id.userPasswordEditText);
builder.setView(rootView);

Categories

Resources