I am having problem with implementing barcodefragmentlibv2 into my project. As a name says and from previous experience I know that I can add it into fragment. However, I am still getting NullPointerException error when I am trying to get this fragment to work with my app.
This is a part of a code responsible for barcode fragment:
import android.support.v4.app.Fragment;
//...
public class CameraFragment extends Fragment implements IScanResultHandler {
BarcodeFragment fragment;
public static CameraFragment newInstance() {
CameraFragment fragment = new CameraFragment();
return fragment;
}
public CameraFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.include_qrfragment, container, false);
fragment = (BarcodeFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.sample);
fragment.setScanResultHandler(this);
return view;
}
//...
}
Error appears in line:
fragment.setScanResultHandler(this);
I've also tried replacing
getActivity().getSupportFragmentManager().findFragmentById(R.id.sample);
to
getFragmentManager().findFragmentById(R.id.sample);
but the result is this same.
Below are layout files:
fragment_camera
<FrameLayout 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=".CameraFragment">
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/include_qrfragment" />
<TextView
android:id="#+id/status_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_margin="15dp"
android:background="#99FFFFFF"
android:gravity="center"
android:padding="10dp"
android:text="#string/msg_default_status"
android:textAppearance="?android:attr/textAppearanceMedium" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
include_qrfragment
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sample"
android:name="com.abhi.barcode.frag.libv2.BarcodeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
At the beginning I've mentioned that from my own experience that this is possible. The truth is that I've built app utilizing this feature some time ago using eclipse and now I am using Android Studio - maybe there is a problem?
EDIT 1:
This is the whole error's stack trace:
06-22 23:05:56.804 30578-30578/myapp.com.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at myapp.com.test.CameraFragment.onCreateView(CameraFragment.java:34)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
The problem seems to lie in this line:
fragment = (BarcodeFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.sample);
Your fragment is not yet appended to the activity as getActivity() is null at this point.
Perhaps you should move that particular code to onActivityCreated
You should only inflate the layout at the CreateView, like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.your_layout, null);
}
The rest of your code, you should put it in the onActivityCreated as #waqaslam said.
You are getting a null pointer because there is no layout inflated yet.
I have read your responses, and now I am assuming you have tried or moved code of getActivity().getSupportFragmentManager().findFragmentById into onActivityCreated.
The only suspect I see without loading up your project is the GUI ID itself. You named it sample or R.id.sample, which is a suspect in some projects. It is possible that you have another UI element having the same name/ID. If so, that UI cannot be converted to a fragment. My suggestion is to change the name of the ID (with unique name), and use it with findFragmentById().
Related
I am trying to create an Activity that extends AppCompatActivity and has two fragments inside of it (one below another - just by using a LinearLayout). I would like the first fragment to extend the PreferenceFragmentCompat class from the support-v7 library.
I followed Google's short example regarding PreferenceFragmentCompat as shown at https://developer.android.com/reference/android/support/v7/preference/PreferenceFragmentCompat.html.
Here is my current code:
GroupDetailsActivity.java
public class GroupDetailsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_details);
GroupDetailsPrefFragment prefFragment = GroupDetailsPrefFragment.newInstance();
GroupDetailsMembersFragment membersFragment = GroupDetailsMembersFragment.newInstance();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.add(R.id.flPrefFragment, prefFragment, GroupDetailsPrefFragment.TAG)
.add(R.id.flMembersFragment, membersFragment, GroupDetailsMembersFragment.TAG)
.commit();
}
}
GroupDetailsPrefFragment .java - The problematic fragment
public class GroupDetailsPrefFragment extends PreferenceFragmentCompat {
public static final String TAG = "GroupDetailsPrefFragment";
#Override
public void onCreatePreferences(Bundle bundle, String s) {
setPreferencesFromResource(R.xml.group_details_preferences, s);
}
public static GroupDetailsPrefFragment newInstance() {
GroupDetailsPrefFragment fragment = new GroupDetailsPrefFragment();
return fragment;
}
}
GroupDetailsMembersFragment.java - Completely empty for now..
public class GroupDetailsMembersFragment extends Fragment {
public static final String TAG = "GroupDetailsMembersFragment";
public static GroupDetailsMembersFragment newInstance() {
GroupDetailsMembersFragment fragment = new GroupDetailsMembersFragment();
return fragment;
}
}
activity_group_details.xml - Activity's layout
<?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">
<FrameLayout
android:id="#+id/flPrefFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/flMembersFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
group_details_preferences.xml - The preference XML file
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:title="#string/remove_me"
android:key="#string/pref_key_settings_remove_me"/>
<Preference
android:title="#string/delete_group"
android:key="#string/pref_key_settings_delete_group"/>
</PreferenceScreen>
Trying to compile and run the code above lead me into a few errors, the first one was regarding a preference theme that was not set. I have quickly scanned the internet and found you need to add the following line into your Activity's theme : <item name="preferenceTheme">#style/PreferenceThemeOverlay.v14.Material</item>
To do so, I was needed to add the support-v14 library to gradle!
Trying to run the code again, lead me to another error, which is the reason I am posting this, and so far I didn't find any way to solve this issue. Here is the crash log :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cochat.android/com.cochat.android.ui.groups.details.GroupDetailsActivity}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list_container' that is not a ViewGroup class
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list_container' that is not a ViewGroup class
at android.support.v7.preference.PreferenceFragmentCompat.onCreateView(PreferenceFragmentCompat.java:269)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2184)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)
at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:181)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1189)
at android.app.Activity.performStart(Activity.java:5441)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Been stuck on this for a while now, tried looking up different posts on stackoverflow or other websites, seen some solutions, but for some reason none of them managed to solve my problem.
Edit:
My gradle file contains the following:
compileSdkVersion 25
buildToolsVersion '25.0.2'
...
compile 'com.android.support:preference-v7:25.1.0'
compile 'com.android.support:preference-v14:25.1.0'
Update Jan 02 '17
I've been looking into the source code of PreferenceFragmentCompat and seen it is trying to load the following layout: R.layout.preference_list_fragment.
In the onCreateView() method of the class, it is inflating the layout, and trying to look for the id android.R.id.list_container.
The problem is that there is no such id within the layout.
Here is a code snippet from the PreferenceFragmentCompat:
final View view = themedInflater.inflate(mLayoutResId, container, false);
final View rawListContainer = view.findViewById(AndroidResources.ANDROID_R_LIST_CONTAINER);
if (!(rawListContainer instanceof ViewGroup)) {
throw new RuntimeException("Content has view with id attribute "
+ "'android.R.id.list_container' that is not a ViewGroup class");
}
While
private int mLayoutResId = R.layout.preference_list_fragment;
Still looking for a solution, thanks!
I got past this using the following bugfix:
https://github.com/Gericop/Android-Support-Preference-V7-Fix
Simple 3 step process is to update the app's build.gradle,
Remove:
compile 'com.android.support:preference-v7:25.0.1'
compile 'com.android.support:preference-v14:25.0.1'
Add
compile 'com.takisoft.fix:preference-v7:25.0.1.0'
Then update your app's theme for preferences to use PreferenceFixTheme
(You're already using PreferenceFragmentCompat so good to go).
UPDATE:
On further investigation, with api 25.1.0, AppCompat worked fine once I realized the very strict restrictions on the style for Preferences.
I found this resource very helpful in getting everything set up nicely:
According to this article:
Note that Google changed the needed id from R.id.list_container (in
revision 23.4.0) to android.R.id.list_container (in revision 24.0.0).
Android Studio says the new id requires API 24, but it also works on
older APIs.
ViewGroup in your layout file (activity_group_details.xml) should have id "#android:id/list_container”. to make it work. It can look like this:
<FrameLayout
android:id="#android:id/list_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/flMembersFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
And you have to use this id for your transaction too, note that the other fragment of yours can use any Id for container, only pref is 'special'
So I have found a work around for this, might not be the best, but it is the only thing I have managed to get working! It seems like a problem/bug with the support library..
I have copied the original PreferenceFragmentCompat to a local class, and made minor changed to it. I have replace the following line
private int mLayoutResId = android.support.v7.preference.R.layout.preference_list_fragment;
with
private int mLayoutResId = R.layout.preference_fragment_compat_container;
which is a layout I have made, that it very simple and only contains a container for the list. Here is the layout code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
Doing the above will allow you to use PreferenceFragmentCompat with the specific gradle settings above (see original post) without any problems.
The down side is that upgrading the support libraries will not upgrade your PreferenceFragmentCompat since it is copied of course. You will need to keep track of the support libraries, and when ever the problem is fixed, you may delete the copied class and use the original one.
If you have any other solutions or ideas please share!
I think you just need to use the support PreferenceScreen and Preferences:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.preference.Preference
android:title="#string/remove_me"
android:key="#string/pref_key_settings_remove_me"/>
<android.support.v7.preference.Preference
android:title="#string/delete_group"
android:key="#string/pref_key_settings_delete_group"/>
</android.support.v7.preference.PreferenceScreen>
Checked out my answer here https://stackoverflow.com/a/53560798/1247248
you need a layout resource with that id
Adding this line to your AppTheme in the styles.xml file should do the trick:
<item name="preferenceTheme">#style/PreferenceThemeOverlay</item>
FIXED
I outsourced the Bluetooth scanning to a separate thread which was a failure. :D Having fixed this the code now works. Thanks for all support!
ORIGINAL QUESTION
I'm having troubles with exchanging dynamically added fragments (I use the support library and my minimal API version is 8 (Android 2.2)). In my XML which can be seen below file I have a FrameLayout which contains the fragment.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".BluetoothConnectionManager">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frmlFragmentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/tbBottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#color/hafnertec"
android:layout_alignParentBottom="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/lblMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lblMode"
android:layout_centerInParent="true"/>
<ImageButton android:id="#+id/btnRefresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_refresh"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_alignRight="#id/lblMode"
android:clickable="false"
android:longClickable="false"
android:onClick="updateData"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</RelativeLayout>
For exchanging the fragments i use a SupportFragmentManager. Furthermore, I also instantiate the two fragments in the onCreate() method. In addition, the fragment for discovering Bluetooth devices is added to the frame layout which works fine.
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
// fetch a FragmentManager used for exchanging the fragments
this.fmFragmentExchanger = this.getSupportFragmentManager();
this.btDiscoveryFragment = new BluetoothDiscoveryFragment();
this.btConnectorFragment = new BluetoothConnectorFragment();
if (this.findViewById(R.id.frmlFragmentContainer) != null)
{
this.fmFragmentExchanger.beginTransaction().add(R.id.frmlFragmentContainer, this.btDiscoveryFragment).disallowAddToBackStack().commit();
}
...
}
Moreover, I've taken care that the fragments extend the Fragment class provided by the support library:
import android.support.v4.app.Fragment;
public class BluetoothDiscoveryFragment extends Fragment implements ...
{
// see: https://developer.android.com/training/basics/fragments/creating.html
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Nullable Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_connection_bluetooth_device, container, false);
}
Here is my code for exchanging the fragments:
this.fmFragmentExchanger.beginTransaction().replace(R.id.frmlFragmentContainer, this.btConnectorFragment).disallowAddToBackStack().commit();
The class for discovering Bluetooth devices makes use of a BroadcastReceiver which is "connected" with the activity. On stopping the discovery process or when it has finished I unregister this BroadcastReceiver.
However, on exchanging the fragments nothing happens and after some time I get an error caused by SIGABRT:
12-31 15:09:51.621 9790-9795/com.hafnertec.afdbluetooth I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
12-31 15:09:53.945 9790-9790/com.hafnertec.afdbluetooth A/libc﹕ Fatal signal 6 (SIGABRT) at 0x000001bd (code=0), thread 9790 (ec.afdbluetooth)
Here you can see the file /data/anr/traces.txt: trace file
I'm running the tests on a Samsung Galaxy S I9000 with Android 4.4.4 (CM SNAPSHOT M12). During the activity's onCreate() method I add the btDiscoveryFragment instance which works fine:
this.fmFragmentExchanger.beginTransaction().add(R.id.frmlFragmentContainer, this.btDiscoveryFragment).disallowAddToBackStack().commit();
Furthermore, adding the instance btConnectorFragment using .add() works. However, it logically causes the btConnectorFragment to be overlayed over the btDiscoveryFragment.
I think you have a typo, causing btDiscoveryFragment to be null:
this.fmFragmentExchanger = this.getSupportFragmentManager();
//add this line
this.btDiscoveryFragment = new BluetoothDiscoveryFragment();
//remove this duplicated line
//this.btConnectorFragment = new BluetoothConnectorFragment();
this.btConnectorFragment = new BluetoothConnectorFragment();
onCreateView() - error when inflating view. Binary XML file line #15: Error inflating class fragment
Hi guys,
has any got a clue how to debug such errors?
I found several posts from people having same error but I just can't find any of solutions working.
Basically I have a project which is quite complex, if FragmentActivity with Viewpager and Fragments as pages which one of them has got map fragment which is support map fragment but extended by map extensions project.
What I now wanted to do was to get rid or sherlock fragment and replace it by actionbar compat project.
How ever app is crashing immediately after starting when inflating map fragment.
I have no idea why and just do not have a clue ho wto debug and find out what's wrong. I need some general help how to find out problem. The code of the app is soo big to paste it here and also commenting out any little thing in the app breaks the app in several places so finding what the problem is kind of impossible.
Any clues how to get to root of such error?
Thanks a lot.
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mapParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill_horizontal" >
<!-- <fragment -->
<!-- android:id="#+id/map" -->
<!-- android:layout_width="match_parent" -->
<!-- android:layout_height="match_parent" -->
<!-- class="pl.mg6.android.maps.extensions.SupportMapFragment" /> -->
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
and onCreateview()
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("map_page", "onCreateView() called");
// thi is funky workarount for map fragment as it it not properly restored itself
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.map_page, container, false);
} catch (InflateException e) {
Log.w("map_page", "onCreateView() - error when inflating view. "+e.getMessage());
/* map is already there, just return view as it is */
}
return view;
}
if you catch exceptions print full error stack in logcat not only error message ;o) and then add this to your manifest
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
I am using the library WizardDroid which uses android.support.v4.app.Fragment internally.
In my application I use normal Fragments though (android.app.Fragment).
I also use Google Maps v2, which I retrieve in a fragment that inherits from a wizardroid class (which itself inherits from android.support.v4.app.Fragment).
public class CreateEventStepMap extends WizardStep
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.create_event_map, container, false);
final Event event = ((EventInfo)getActivity()).getEvent();
final GoogleMap googleMap = ((com.google.android.gms.maps.SupportMapFragment)getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
return v;
}
}
So as you can see, I cast to com.google.android.gms.maps.SupportMapFragment() because findFragmentById(R.id.map) returns a support map fragment. I cannot do otherwise because I would get a compilation error.
This is the XML that the map fragment is inside:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
Everywhere else in my application I use normal fragments!
When I run the application I get this exception:
11-08 20:47:19.178: E/AndroidRuntime(10347): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
Is this because I am mixing Fragments and support map fragments in the same application or maybe I am doing something else stupid?
If the answer is the first, would a solution be to change the code of the wizardroid library (I don't need support for versions < 4) ?
Some users are getting this error.
I use a viewgroup to continuously display a banner add.
I then use child views for the program and switch back and forth between views as the user clicks a button.
I can cause a crash by switching back and forth between child views 6 to 10 times.
Here is the layout for the viewgroup:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.company.programname"
xmlns:app="http://schemas.android.com/apk/res/com.adwhirl"
android:orientation="vertical"
android:background="#color/darkslategrey"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.adwhirl.AdWhirlLayout
android:id="#+id/adwhirl_layout"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="52dp"
/>
</LinearLayout>
Here is the code creating the ViewGroup:
setContentView(R.layout.viewgrouplayout);
llLinLay=(LinearLayout)findViewById(R.id.LinearLayout01);
liInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Here is the main child layout with irrelevent pieces cut out:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/MainLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/MainShowAllDesiredButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/AnotherButton"
android:layout_toRightOf="#id/AButton"
android:textSize="15sp"
android:text="Show All Desired"
android:onClick="MainDesired"
/>
<ListView
android:id="#+id/ItemTypeList"
android:background="#color/aqua"
android:cacheColorHint="#color/aqua"
android:fastScrollEnabled="true"
android:layout_width="fill_parent"
android:layout_height="160dip"
android:layout_below="#id/SomeItem"
/>
</RelativeLayout>
Here is an example of the code creating a view:
vAllDesired=liInflater.inflate(R.layout.alldesiredlayout,null);
vAllDesired.setId(7);
llLinLay.addView(vAllDesired);
final ListView lvAllDesired = (ListView)findViewById(R.id.AllDesiredList);
laAllDesiredAdapter = new AllDesiredListAdapter(this, alAllDesired);
lvAllCoinsDesired.setAdapter(laAllDesiredAdapter);
ViewGroup.LayoutParams AllDesiredParams = lvAllCoinsDesired.getLayoutParams();
AllDesiredParams.height = AllCoinsDesiredHeight;
lvAllCoinsDesired.setLayoutParams(AllDesiredParams);
FillAllDesiredArray();
Here is an example of the code used to switch between views:
bReturnToMainScreen = (Button) findViewById(R.id.DesiredReturnToMainScreenButton);
bReturnToMainScreen.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View V) {
llLinLay.removeView(vAllDesired);
MainWindow();
}
});
Here is the trace from Android Developer website. The line number in the program is not always the same.
java.lang.NullPointerException
at android.webkit.WebView.requestFocus(WebView.java:6113)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
at android.view.ViewGroup.requestFocus(ViewGroup.java:1032)
at android.view.View.requestFocus(View.java:3559)
at android.view.ViewRoot.clearChildFocus(ViewRoot.java:1586)
at android.view.ViewGroup.clearChildFocus(ViewGroup.java:508)
at android.view.ViewGroup.clearChildFocus(ViewGroup.java:508)
at android.view.ViewGroup.clearChildFocus(ViewGroup.java:508)
at android.view.ViewGroup.clearChildFocus(ViewGroup.java:508)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:2207)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:2181)
at android.view.ViewGroup.removeView(ViewGroup.java:2129)
at com.jimbobga.mycoinsus.ProgramName$8.onClick(ProgramName.java:488)
at android.view.View.performClick(View.java:2411)
at android.view.View$PerformClick.run(View.java:8819)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Just curious, when you're making your calls to findViewById, are they in the OnCreate method or directly where you're inflating the row in the adapter? As it relates to your example here, the findViewById method is only guaranteed to work in the OnCreate method or where the particular row is inflated, so its possible that calling it elsewhere could cause unexpected behavior.
Look at this line:
at com.jimbobga.mycoinsus.ProgramName$8.onClick(ProgramName.java:488)
If this is happening after orientation has changed and views have not been assigned then you have a problem.
I'm experiencing the same issue.
Take a look at this response: Issues With Adwhirl(Admob+Inmobi+..)
Eric from AdMob staff says that it's a problem with the Android framework.
Did you try to surround your OnClick() code with a: try/catch Throwable Exception?