Android Studio - Dynamic Fragment crashing App - android

I am trying to link a Fragment, RecycleView and CardView using a variation of the tutorial found on:
http://www.treyrobinson.net/blog/android-l-tutorials-part-3-recyclerview-and-cardview/
Unfortunately my app crashes with the following error message:
"java.lang.IllegalStateException: RecyclerView has no LayoutManager"
I am fairly new with Android development. I tried to find similar issues on Stackoverflow but am unable to find the issue. Any help would be greatly appreciated!
Layout of the main activity: "activity_library.xml":
<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=".LibraryActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/library_activity_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LibraryActivity"
/>
</RelativeLayout>
The main activity class "LibraryActivity.java":
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class LibraryActivity extends AppCompatActivity{
private static final String TAG = LibraryActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); /**Create the activity and populate the savedInstanceState if the activity existed but has been destroyed (otherwise savedInstanceState will return 'null'*/
setContentView(R.layout.activity_library); /** Call to the XML layout library which display the activity */
if (savedInstanceState != null) {
// Restore value of members from saved state
} else {
// Probably initialize members with default values for a new instance
}
try {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BooksFragment bookFragment = new BooksFragment();
fragmentTransaction.add(R.id.library_activity_recycler_view,bookFragment);
/*The line below is the one generating the error - java.lang.IllegalStateException: RecyclerView has no LayoutManager*/
fragmentTransaction.commit();
}
catch(Exception e){
Log.d(TAG,"[ERROR] " + e.getMessage());
}
}
}
The layout of the fragment "fragment_books.xml":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.RecyclerView
android:id="#+id/Book_Recycler_View"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
And the fragment class:
import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BooksFragment extends Fragment {
public BooksFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_library, container, false);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
RecyclerView bookRecycler = (RecyclerView) v.findViewById(R.id.library_activity_recycler_view);
bookRecycler.setLayoutManager(layoutManager);
BookRecyclerViewAdapter adapter = new BookRecyclerViewAdapter();
bookRecycler.setAdapter(adapter);
/*In the line below should we return the view 'v' or the RecyclerView 'bookRecycler'??*/
return bookRecycler;
}
}
As I said I am fairly new to Android and therefore apologies if my code contains quite a few errors. Thanks for your help!
For reference the complete error log:
04-18 14:04:52.323 11225-11225/com.wldtaster.tellmeastory E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wldtaster.tellmeastory, PID: 11225
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wldtaster.tellmeastory/com.wldtaster.tellmeastory.LibraryActivity}: java.lang.IllegalStateException: RecyclerView has no LayoutManager
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager
at android.support.v7.widget.RecyclerView.generateLayoutParams(RecyclerView.java:3393)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.wldtaster.tellmeastory.BooksFragment.onCreateView(BooksFragment.java:23)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:834)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.Activity.performStart(Activity.java:6005)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

You must return the view ie. v in the onCreateView method of the Fragment inherited class.

Related

ViewPager and Android Wear = crash

_
Hello members of stackoverflow!
I worked successfully with the ViewPager (VP) on a "normal" activity.
Now I tried to use almost the same way for an Android Wear app.
In my MainActivity, which contains the VP, I faced different errors like:
Error 1: (getFragmentManager() -> "Cannot be applied"
Error 2: super(manager); -> "Cannot be applied"
Error 3: public Fragment getItem(int i); -> "Incompatible return type"
I found this thread and was able to fix those errors by changing the imports + changing the build.gradle.
How to implement view pager in Android watch?
Those are now my imports in my MainActivity.java:
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.wearable.activity.WearableActivity;
import java.util.ArrayList;
import java.util.List;
This is the rest of the MainActivity:
public class MainActivity extends WearableActivity
{
ViewPager vp; // Variable for Viewpager
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Link to layout
ViewPager vp = findViewById(R.id.vp); // Link variable to ID
SetUpViewPager(vp); // Run viewpager method
}
public void SetUpViewPager(ViewPager viewpager)
{
MyViewPagerAdapter Adapter = new MyViewPagerAdapter(getFragmentManager());
Adapter.AddPageFragment(new Fragment1(), "Fragment1"); // Load page 1 inside of ViewPager
Adapter.AddPageFragment(new Fragment2(), "Fragment2"); // Load page 2 inside of ViewPager
viewpager.setAdapter(Adapter);
}
public class MyViewPagerAdapter extends android.support.v4.app.FragmentPagerAdapter
{
private List<Fragment> MyFragment = new ArrayList<>();
private List<String> MyPageTitle = new ArrayList<>();
public MyViewPagerAdapter(FragmentManager manager)
{
super(manager);
}
public void AddPageFragment(Fragment Frag, String Title)
{
MyFragment.add(Frag);
MyPageTitle.add(Title);
}
#Override
public Fragment getItem(int i)
{
return MyFragment.get(i);
}
#Nullable
#Override
public CharSequence getPageTitle(int position)
{
return MyPageTitle.get(position);
}
#Override
public int getCount()
{
return 2; // 2 pages total
}
}
}
Strange is that is says ViewPager vp; -> Field 'vp' is never used!
Fragment1: (Fragment2 is the same except for 2/two)
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment
{
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View fragmentOne = inflater.inflate(R.layout.fragment1, container, false); // Link view and layout
return fragmentOne;
}
}
The XML files for the fragments are just constraint layouts with each 1 button.
The content_main.xml contains the VP which accesses the activity_main.xml.
I used the same way on the normal smartphone device and it worked flawlessly.
The problem is that everytime I start the app on the Wear Emulator, it immediately crashes.
This is what Logcat says:
10-23 14:40:32.909 2203-2203/com.example.user.wearvp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.wearvp, PID: 2203
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.wearvp/com.example.user.wearvp.MainActivity}: java.lang.NullPointerException:
Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
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:5422)
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.NullPointerException:
Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at com.example.user.wearvp.MainActivity.SetUpViewPager(MainActivity.java:35)
at com.example.user.wearvp.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6251)
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:5422) 
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) 
Any help will be appreciated!
P.S:
If I forgot to mention anything important, please, just ask and I will give you the needed information!
Best regards
The stack trace indicates that there is no view with the id vp defined in your activity_main.xml layout file. I'm not completely sure what you mean with:
The content_main.xml contains the VP which accesses the activity_main.xml.
If vp is defined in the content_main.xml layout, then that might be the one you should use with setContentView() (instead of the current activity_main.xml).
Strange is that is says ViewPager vp; -> Field 'vp' is never used!
The local variable vp (defined inside the onCreate method) is shadowing the instance variable vp. If you need it to have object-level scope just remove ViewPager when you assign a value to it.

#android:id/list cannot resolve symbol

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
/**
* Created by name on 3/13/17.
* Used for the specials tab to allow a list of days to choose what special one would like to view
*/
public class SpecialsScroll extends ListFragment implements AdapterView.OnItemClickListener{
public SpecialsScroll(){
//Empty Constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreate(savedInstanceState);
return inflater.inflate(R.layout.fragment_specials_scroll, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
setListAdapter(ArrayAdapter.createFromResource(getActivity(), R.array.days, android.R.layout.simple_list_item_1)); //Loads array into the ListFragment
getListView().setOnItemClickListener(this); //activates the listener for this Fragment class
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){ // For checking when a user taps on an option
SpecialsPage specialsPage = new SpecialsPage();
Bundle foodArgs = new Bundle();
switch (position){ // checks what option is chosen and sends respective keys as parameters
case 0:
int[] keysMonday = {R.string.boom_boom_enchiladas_key, R.string.tacos_mexican_key};
foodArgs.putIntArray("keys", keysMonday);
break;
case 1:
int[] keysTuesday = {R.string.shrimp_avocado_tacos_key, R.string.green_chile_chicken_enchiladas_key};
foodArgs.putIntArray("keys", keysTuesday);
break;
}
specialsPage.setArguments(foodArgs);
getActivity().getFragmentManager().beginTransaction().replace(R.id.container, specialsPage).addToBackStack(null).commit();
}
}
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity{
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if(getFragmentManager().findFragmentByTag("CURRENT") == null){
getFragmentManager().beginTransaction().add(R.id.container, new SpecialsScroll(), "CURRENT").commit();
}
switch (item.getItemId()) {
case R.id.special:
getFragmentManager().beginTransaction().replace(R.id.container, new SpecialsScroll(), "CURRENT").commit();
return true;
case R.id.locations:
getFragmentManager().beginTransaction().replace(R.id.container, new LocationsScroll(), "CURRENT").commit();
return true;
case R.id.order:
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
getFragmentManager().beginTransaction().add(R.id.container, new SpecialsScroll(), "CURRENT").commit();
}
}
So, I am trying to create a ListView and everything in the code works, but suddenly I am unable to get the xml to recognize the android:id/list attribute for some reason, I don't know why. When I click on an item in the listview in my app it still does what it should, I just really want to get rid of the error.
<?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"
android:background="#color/white">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<TextView
android:id="#+id/text_specialsscroll"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
04-26 14:06:30.299 2576-2576
/com.namename.www.name
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.namename.www.name, PID: 2576
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.namename.www.name/com.namename.www.name.MainActivity}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
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.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
at android.app.ListFragment.ensureList(ListFragment.java:402)
at android.app.ListFragment.onViewCreated(ListFragment.java:203)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1010)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
at android.app.BackStackRecord.run(BackStackRecord.java:816)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1578)
at android.app.FragmentController.execPendingActions(FragmentController.java:371)
at android.app.Activity.performStart(Activity.java:6695)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
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)
Change your id to this:
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>

android.view.InflateException: Binary XML file line #36: Error inflating class fragment

I'm very new to Android Application Development. I know my problem might be simple one. but, I'm sure it is related to layout.
Here's my issue: There's is a listView in the Fragment. When I click the button the new Fragment is added to the ScrollView in the main Layout. When I start the activity one fragment is already added to the activity. So, when I change the rotation the application does not crash.
But, new Fragment is added in the ScrollView after clicking button. But this time when I change the Rotation of the device the Application crashes giving following the error in the end. Can you help me solve this problem. Error occurs only when I change rotaion otherwise app runs fine.
Layout(XML) File activity_another_custom_list_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_another_custom_list_view"
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.practices.app.practices.AnotherCustomListViewActivity"
android:background="#drawable/border">
<Button
android:text="#string/add_fragments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:onClick="addfrag"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/button">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.practices.app.practices.AnotherCustomListViewFragment"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/fragment"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Fragment Layout another_custom_listview_fragment.xml
<?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">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fraglistview"></ListView>
</LinearLayout>
Java File AnotherCustomListViewActivity.java
package com.practices.app.practices;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.View;
public class AnotherCustomListViewActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another_custom_list_view);
}
public void addfrag(View v){
AnotherCustomListViewFragment f = new AnotherCustomListViewFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment,f);
ft.commit();
}
}
Fragment Java Class AnotherCustomListViewFragment
package com.practices.app.practices;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Adapter;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AnotherCustomListViewFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.another_custom_listview_fragment, container, false);
String[] str = {"a","b","c","d"};
ListView fraglist = (ListView) view.findViewById(R.id.fraglistview);
ListAdapter listadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, str);
fraglist.setAdapter(listadapter);
return view;
}
}
Here's the full Error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.practices.app.practices/com.practices.app.practices.AnotherCustomListViewActivity}: android.view.InflateException: Binary XML file line #36: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3994)
at android.app.ActivityThread.access$1000(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
at com.practices.app.practices.AnotherCustomListViewActivity.onCreate(AnotherCustomListViewActivity.java:15)
at android.app.Activity.performCreate(Activity.java:6024)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3994) 
at android.app.ActivityThread.access$1000(ActivityThread.java:155) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5318) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717) 
Caused by: java.lang.IllegalStateException: Fragment com.practices.app.practices.AnotherCustomListViewFragment did not create a view.
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2145)
at android.app.Activity.onCreateView(Activity.java:5373)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:36)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:77)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143) 
at com.practices.app.practices.AnotherCustomListViewActivity.onCreate(AnotherCustomListViewActivity.java:15) 
at android.app.Activity.performCreate(Activity.java:6024) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3994) 
at android.app.ActivityThread.access$1000(ActivityThread.java:155) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5318) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717) 
I added reply to the answers in comment section but no one is answering.
Well i had this kind of problem few days back but after changing the drawable in my layout it solved my problem because my drawable was too large that's why it's causing the problem.
You can check in your layout for drawable if it is large than change it with other small one.
I think this will end your problem.
In my case, updating the libraries worked for me.

ViewPager obj is null even though it initilized correctly and also present in the defined layout xml

Before posting I checked many questions but they didn't helped me. Most of the answers says that the ViewPager is in different layout, so that is why it generating null pointer exception.
Below is my stacktrace
E/AndroidRuntime: FATAL EXCEPTION: main Process: pdfshare.hemanthreddy.com.pdfshare, PID: 29092
java.lang.RuntimeException: Unable to start activity ComponentInfo{pdfshare.hemanthreddy.com.pdfshare/pdfshare.hemanthreddy.com.pdfshare.activities.HomeScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at pdfshare.hemanthreddy.com.pdfshare.activities.HomeScreen.onCreate(HomeScreen.java:39)
at android.app.Activity.performCreate(Activity.java:6259)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
This is my xml file where I have declared Viewpager
activity_home_screen.xml
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
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="pdfshare.hemanthreddy.com.pdfshare.activities.HomeScreen">
<android.support.v4.view.ViewPager
android:id="#+id/viewpagerhome"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</RelativeLayout>
Activity class HomeScreen.java
package pdfshare.hemanthreddy.com.pdfshare.activities;
import android.graphics.Color;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.BottomBarBadge;
import com.roughike.bottombar.BottomBarTab;
import com.roughike.bottombar.OnMenuTabSelectedListener;
import com.roughike.bottombar.OnTabSelectedListener;
import pdfshare.hemanthreddy.com.pdfshare.R;
import pdfshare.hemanthreddy.com.pdfshare.fragments.GroupsFragment;
import pdfshare.hemanthreddy.com.pdfshare.fragments.HomeFragment;
import pdfshare.hemanthreddy.com.pdfshare.fragments.NotificationsFragment;
import pdfshare.hemanthreddy.com.pdfshare.fragments.ProfileFragment;
public class HomeScreen extends AppCompatActivity {
ViewPager pager;
BottomBar bottomBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
bottomBar = BottomBar.attach(this,savedInstanceState);
pager = (ViewPager) findViewById(R.id.viewpagerhome);
MyPagerAdapter obj = new MyPagerAdapter(getSupportFragmentManager());
//the following two if statements are used to check if objects are null
if(obj.getCount()==4)
Log.e("obj","not null");
if(pager == null)
Log.e("pager","null");
pager.setAdapter(obj);
bottomBar.setItems(new BottomBarTab(R.mipmap.ic_action_home_24,"home"),
new BottomBarTab(R.mipmap.ic_action_user_group,"groups"),
new BottomBarTab(R.mipmap.ic_action_notification,"notifications"),
new BottomBarTab(R.mipmap.ic_action_profile,"profile")
);
bottomBar.setOnItemSelectedListener(new OnTabSelectedListener() {
#Override
public void onItemSelected(int position) {
T oast.makeText(getApplicationContext(),position,Toast.LENGTH_LONG).show();
pager.setCurrentItem(position);
}
});
BottomBarBadge message = bottomBar.makeBadgeForTabAt(2,"red",10);
message.show();
}
private class MyPagerAdapter extends FragmentStatePagerAdapter
{
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
return new HomeFragment();
case 1:
return new GroupsFragment();
case 2:
return new NotificationsFragment();
case 3:
return new ProfileFragment();
default:
return new HomeFragment();
}
}
#Override
public int getCount() {
return 4;
}
}
}
E/obj: not null
E/pager: null
dont know why pager object is null.
Please help me I am trying to load 4 fragments in viewPager, thank you.
it means your variable obj of type MyPagerAdapter is null, there is an error with it's return value.
MyPagerAdapter obj = new MyPagerAdapter(getSupportFragmentManager());
That line is the problem, the problem could be in your custom adapter, or your get support fragment manager. if they were working the obj would not be null.
i bet if you comment out that line and the setadapter line it won't crash anymore, unless you have bigger problems

Unable to start activity ComponentInfo : android.view.InflateException: Binary XML file line #7: Error inflating class fragment

I'm getting the below error when trying out the Fragments in Android Studio. The below exception occurs when launching the app :
04 - 24 21: 02: 16.830 21108 - 21108 / com.example.user.samplefragments E / AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo {
com.example.user.samplefragments / com.example.user.samplefragments.MainActivity
}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line # 7: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java: 710)
at android.view.LayoutInflater.rInflate(LayoutInflater.java: 752)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 495)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 397)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java: 364)
at android.app.Activity.setContentView(Activity.java: 1930)
at com.example.user.samplefragments.MainActivity.onCreate(MainActivity.java: 19)
at android.app.Activity.performCreate(Activity.java: 5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2299) 
at android.app.ActivityThread.access$700(ActivityThread.java: 150) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1280) 
at android.os.Handler.dispatchMessage(Handler.java: 99) 
at android.os.Looper.loop(Looper.java: 137) 
at android.app.ActivityThread.main(ActivityThread.java: 5283) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java: 511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1102) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 869) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException
at com.example.user.samplefragments.MenuFragment.onCreateView(MenuFragment.java: 23)
at android.app.Fragment.performCreateView(Fragment.java: 1699)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java: 879)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java: 1053)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java: 1155)
at android.app.Activity.onCreateView(Activity.java: 4872)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java: 686)
at android.view.LayoutInflater.rInflate(LayoutInflater.java: 752) 
at android.view.LayoutInflater.inflate(LayoutInflater.java: 495) 
at android.view.LayoutInflater.inflate(LayoutInflater.java: 397) 
at android.view.LayoutInflater.inflate(LayoutInflater.java: 353) 
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java: 364) 
at android.app.Activity.setContentView(Activity.java: 1930) 
at com.example.user.samplefragments.MainActivity.onCreate(MainActivity.java: 19) 
at android.app.Activity.performCreate(Activity.java: 5283) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1097) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2209) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2299) 
at android.app.ActivityThread.access$700(ActivityThread.java: 150) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1280) 
at android.os.Handler.dispatchMessage(Handler.java: 99) 
at android.os.Looper.loop(Looper.java: 137) 
at android.app.ActivityThread.main(ActivityThread.java: 5283) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java: 511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1102) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 869) 
at dalvik.system.NativeStart.main(Native Method) 
This is my MainActvity.java :
package com.example.user.samplefragments;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity implements MenuFragment.OnItemSelectedListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onItemSelected(String link) {
DetailFragment fragment = (DetailFragment)getFragmentManager().findFragmentById(R.id.detailfragment);
fragment.setText(link);
}
}
This is my Fragment 1 :
package com.example.user.samplefragments;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by USER on 24-04-2016.
*/
public class MenuFragment extends Fragment{
private OnItemSelectedListener listener;
public MenuFragment(){}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu,container,false);
Button btn = (Button)getView().findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
updateDetail("fake");
}
});
return view;
}
public void updateDetail(String uri){
String newTime = String.valueOf(System.currentTimeMillis());
listener.onItemSelected(newTime);
}
public interface OnItemSelectedListener {
public void onItemSelected(String link);
}
}
This is my Fragment 2 :
package com.example.user.samplefragments;
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;
import android.widget.TextView;
/**
* Created by USER on 24-04-2016.
*/
public class DetailFragment extends Fragment{
public DetailFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail,container,false);
return view;
}
public void setText(String url){
TextView tv = (TextView)getView().findViewById(R.id.textView);
tv.setText(url);
}
}
This is my activity_main.xml :
Just using two fragments inside the main layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="#+id/menufragment"
class="com.example.user.samplefragments.MenuFragment" /**Declaring the class of the fragment*/
android:layout_height="match_parent"
android:layout_width="5dp">
</fragment>
<fragment
android:id="#+id/detailfragment"
class="com.example.user.samplefragments.DetailFragment"
android:layout_height="match_parent"
android:layout_width="5dp">
</fragment>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2" />
</LinearLayout>
As you can see, the error I'm getting :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.samplefragments/com.example.user.samplefragments.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment.
I know this issue occurs when the package name is wrong, but I have given the correct package name. How can I resolve this?
Solution:
You can modify your MenuFragment using follow:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu, container, false);
Button btn = (Button)view.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
updateDetail("fake");
}
});
return view;
}
Why:
Every time onCreateView is invoked, the view in the fragment is just about to create.At that time there is no view in your fragment.So getView() always returns null invoked in onCreateView. That's why you've got NullPointerException when inflating fragment in your xml.
To avoid NullPointer, try this:
public void updateDetail(String uri){
String newTime = String.valueOf(System.currentTimeMillis());
if ( listener != null ){
listener.onItemSelected(newTime);
}
}
I think This line makes the app crashing,
Button btn = (Button)getView().findViewById(R.id.button);
Replace this line with
Button btn = (Button)view.findViewById(R.id.button);
It will work if your button contains in the fragment xml (R.layout.menu).

Categories

Resources