BottomNavigationViewEx Null pointer exception - java

My xml file contains the code:
<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="#+id/bnve"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"
app:itemIconTint="#drawable/bottom_navigation_colors"
app:itemTextColor="#drawable/bottom_navigation_colors"
app:menu="#menu/bottom_nav_menu" />
My java file contains:
BottomNavigationViewEx bnve = (BottomNavigationViewEx)findViewById(R.id.bnve);
It runs perfectly on an android device.
But when I add the lines:
bnve.enableAnimation(false);
bnve.enableShiftingMode(false);
bnve.enableItemShiftingMode(false);
the app crashes with the error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bimsina.projectchautari/com.bimsina.projectchautari.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2583)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5765)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx.getBottomNavigationItemViews(BottomNavigationViewEx.java:569)
at com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx.enableAnimation(BottomNavigationViewEx.java:341)
at com.bimsina.projectchautari.MainActivity.onCreate(MainActivity.java:94)
at android.app.Activity.performCreate(Activity.java:6331)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
Does someone have a solution?
Edit: Heres my whole xml and java file.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="#+id/bnve"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_menu"/>
</android.support.constraint.ConstraintLayout>
The java file is:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationViewEx bnve= findViewById(R.id.bnve);
bnve.enableAnimation(false);
bnve.enableShiftingMode(false);
bnve.enableItemShiftingMode(false);
}
}

This error comes from using different version of support library that is not compatible with the library.
hence for old version support lib 25 or 26 use
com.github.ittianyu:BottomNavigationViewEx:1.2.4
and for new com.android.support:design:28.0.0 use
com.github.ittianyu:BottomNavigationViewEx:2.0.2
This will solve the problem

I found a work around for the problem.I just wanted to remove the animation. Instead of using BottomNavigationViewEx,I used the regular BottomNavigationView and added the following line which removed the animation.
app:labelVisibilityMode="unlabeled"
It will remove the label and also disable the animation.

Related

unable to setText on TextView. Giving NullPointerException

I am setting text on textview but it's giving me NullPointerException
Here's my XML file.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activitys.ScoreActivity">
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--Score Card-->
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="16dp"
app:cardCornerRadius="15dp"
app:cardElevation="6dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="-10dp"
android:text="Your Score"
android:textColor="#color/black"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/scoreQTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:layout_marginBottom="10dp"
android:text="100/200"/>
</androidx.cardview.widget.CardView>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
This is my activity where I am setting text on TextView
public class ScoreActivity extends AppCompatActivity {
private TextView scoreQTv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score);
scoreQTv = findViewById(R.id.scoreQTv);
//and finded all widgets using findviewByID ...
scoreQTv.setText("My Score 18");
}
I am unable to set text on my TextViews. How can I set text, and why am I getting this error?
When I check my layout file, I got 2 files with the same name. I think that's why this happens. I have 2 files like this:
activity_score.xml
activity_score.xml v21 written under the name.
This is my LogCat:
021-09-11 13:33:18.729 3897-3897/com.vishal.kaitka.phodo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vishal.kaitka.phodo, PID: 3897
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vishal.kaitka.phodo/com.vishal.kaitka.phodo.Activitys.ScoreActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
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:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.vishal.kaitka.phodo.Activitys.ScoreActivity.onCreate(ScoreActivity.java:32)
at android.app.Activity.performCreate(Activity.java:7994)
at android.app.Activity.performCreate(Activity.java:7978)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
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:2066) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:223) 
at android.app.ActivityThread.main(ActivityThread.java:7656) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
In Your Question You have 2 files with same name
1.activity_score.xml
2.activity_score.xml v21
So you can delete that xml layouts and make new activity:
Create New Activity named Anything You Want
and paste all xml code to that new Activity's xml layout
and all java code to that's new Activity java file
I thing Maybe it help you!

Error inflating class android.fragment.app.FragmentContainerView, can someone tell me what I'm missing

I'm working on getting the Google Place API to appear within a CardView.
The particular code I'm looking at is from GitHub, seems to be working for everyone else but I don't understand why it's throwing me an error.
This is the error I am getting:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rizwanqureshi.kevstaxiscanterbury, PID: 6818
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rizwanqureshi.kevstaxiscanterbury/com.rizwanqureshi.kevstaxiscanterbury.CustomerMapsActivity}: android.view.InflateException: Binary XML file line #50 in com.rizwanqureshi.kevstaxiscanterbury:layout/activity_customer_maps: Binary XML file line #50 in com.rizwanqureshi.kevstaxiscanterbury:layout/activity_customer_maps: Error inflating class android.fragment.app.FragmentContainerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: android.view.InflateException: Binary XML file line #50 in com.rizwanqureshi.kevstaxiscanterbury:layout/activity_customer_maps: Binary XML file line #50 in com.rizwanqureshi.kevstaxiscanterbury:layout/activity_customer_maps: Error inflating class android.fragment.app.FragmentContainerView
Caused by: android.view.InflateException: Binary XML file line #50 in com.rizwanqureshi.kevstaxiscanterbury:layout/activity_customer_maps: Error inflating class android.fragment.app.FragmentContainerView
Caused by: java.lang.ClassNotFoundException: android.fragment.app.FragmentContainerView
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:454)
at android.view.LayoutInflater.createView(LayoutInflater.java:815)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
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.rInflate(LayoutInflater.java:1126)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1126)
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 com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:438)
at android.app.Activity.setContentView(Activity.java:3324)
at com.rizwanqureshi.kevstaxiscanterbury.CustomerMapsActivity.onCreate(CustomerMapsActivity.java:116)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.fragment.app.FragmentContainerView" on path: DexPathList[[zip file "/data/app/com.rizwanqureshi.kevstaxiscanterbury-Nkg-1mQCfbE8I1MoJKHQuA==/base.apk"],nativeLibraryDirectories=[/data/app/com.rizwanqureshi.kevstaxiscanterbury-Nkg-1mQCfbE8I1MoJKHQuA==/lib/x86, /system/lib, /system/product/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 32 more
The main takeaway is that there is an Error inflating class android.fragment.app.FragmentContainerView and that the issue is at line 50 of the XML.
This is the XML:
<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=".CustomerLogin" >
<androidx.fragment.app.FragmentContainerView
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttons">
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/logout"
android:text="logout"
tools:ignore="ButtonStyle" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/history"
android:text="history"
tools:ignore="ButtonStyle" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/settings"
android:text="Settings"
tools:ignore="ButtonStyle" />
</LinearLayout>
<androidx.cardview.widget.CardView
android:layout_below="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp">
//***LINE 50 WHERE THE ERROR IS***
<android.fragment.app.FragmentContainerView
android:id="#+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="ExtraText">
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
</android.fragment.app.FragmentContainerView>
</androidx.cardview.widget.CardView>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/driverInfo"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:background="#android:color/white"
android:visibility="gone">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="40sp">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:background="#android:color/white"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</LinearLayout>
</FrameLayout>
So I guess there is something wrong with the FragmentContainerView? But i'm not getting any errors in the code and nothing is coming up on Google.
I feel like maybe something is outdated rather than outright incorrect but I haven't a clue.
Thanks for any input.
just try to define your startDestination in your navigation xml !
You should implement new androidx.fragment dependencies to use FragmentContainerView. It is already added since version 1.2.0. You can check here
implementation "androidx.fragment:fragment:1.2.5"
In my case, I had a Model which was used as arguments but I had not implemented Serializable and Parcelable implementaion.
This problem was created for me as well. The reason was two mistakes.
1.I did not add startDestination in nav graph .
2.I did not add #AndroidEntryPoint annotation in MainActivitey .
This error will only occur after you have added ProGuard & R8 to your project. To fix it, add the following two lines in proguard-rule.pro:
-keepnames class androidx.navigation.fragment.NavHostFragment
-keep class * extends androidx.fragment.app.Fragment{}
For me it was a typo within one of the fragments the navGraph, in the class path of the android:name attribute.. Adding a wrong path for the fragment class doesn't provide a compile time error, instead it raises android.view.InflateException...Error inflating class android.fragment.app.FragmentContainerView
So, make sure to have a correct packaging path for your fragments in android:name
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="#+id/nav_graph.xml"
app:startDestination="#id/mainFragment">
<fragment
android:id="#+id/mainFragment"
android:name="com.example.android....MainFragment" // <<<< This must be a correct path
android:label="main_fragment"
</fragment>
</navigation>
If it is Due to Proguard Then add few lines
In proguard-rules.pro file
....
# for FragmentContainerView error
-keepnames class androidx.navigation.fragment.NavHostFragment
-keep class * extends androidx.fragment.app.Fragment{}
Your activity must be a child of FragmentActivity to use androidx.fragment.app.FragmentContainerView either you extend the FragmentActivity or the AppCompatActivity which is a child FragmentActivity class. It is recommended to extend the AppCompatActivity.
see: https://www.tutorialguruji.com/android/trying-to-use-navhostfragment-and-im-getting-an-error-error-inflating-class-androidx-fragment-app-fragmentcontainerview/
Probably after Refactor->Remove unused resources you can lost id from your androidx.fragment.app.FragmentContainerView - bring it back
In my case, despite the same error, reason was different.
However this info about adding some rules to proguard-rules.pro pointed me into the right direction. This, and some other clues in error log about problems with argument in navigation XML file.
Eventually I found that it was problem with using my own classes in argument. And I don't mean the class of argument, but the fact that this class also used some of my other classes. And it was enough to add rules for each of these other classes:
-keepnames class <MyClass>
...
If you have done all the solutions and still the problem stands maybe you should check if your fragment constructor is empty.
In my case I looked deeper inside my stacktrace and found this line: Caused by: java.lang.IllegalStateException: FragmentContainerView must have an android:id to add Fragment MyFragment. Giving it an id and specifying tools:layout fixed problem

getLayoutParams() from LinearLayout return null

I want to implement a BottomSheetBehavoir component to an activity screen, but when I want to initialize the component via BottomSheetBehavoir.from(view) the getLayoutParams() method throw a null pointer exception.
This is my init code in onCreate():
LinearLayout llBottomSheet = findViewById(R.id.bottom_sheet_live_tracking_list_container);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(llBottomSheet);
And this is my xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/bottom_sheet_live_tracking_list_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
app:behavior_hideable="false"
app:behavior_peekHeight="40dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="#drawable/rounded_corners_gray">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/show_more"
android:textColor="#color/white"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
android:id="#+id/activity_live_tracking_show_more_tv" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:src="#drawable/ic_filter_list"
android:visibility="visible" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/activity_live_tracking_position_list_rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I include this layout in my root xml layout via the include tag.
Error code:
2020-02-21 12:34:05.097 8419-8419/de.app.haveltec.ilockit E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.app.haveltec.ilockit, PID: 8419
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.app.haveltec.ilockit/de.app.haveltec.ilockit.screens.livetracking.LiveTrackingActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
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:1839)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at com.google.android.material.bottomsheet.BottomSheetBehavior.from(BottomSheetBehavior.java:981)
at de.app.haveltec.ilockit.screens.livetracking.LiveTrackingActivity.onCreate(LiveTrackingActivity.java:147)
at android.app.Activity.performCreate(Activity.java:7232)
at android.app.Activity.performCreate(Activity.java:7221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2964)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119) 
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:1839) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:201) 
at android.app.ActivityThread.main(ActivityThread.java:6861) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 
As mentioned in the documentation BottomSheetBehaviour is deprecated. If you still want to use it, make sure it is child of a CoordinatorLayout as that is a prerequisite to use it according to the doc.
Would rather have added this answer as a comment, but not enough reputation...
So after I rest some days and go back to the problem I found the solution. For my LinearLayout, that throws the getLayoutParams() null pointer exception, I use the id from the bottom sheet xml that you can see in my post android:id="#+id/bottom_sheet_live_tracking_list_container". In my main layout there the parent is a CoordinatorLayout is include my bottom sheet layout like this:
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="match_parent"
android:animateLayoutChanges="true">
... other stuff ....
<include
android:id="#+id/activity_live_tracking_bottom_sheet"
layout="#layout/bottom_sheet_live_tracking_list" />
Know I try the id from the include tag (android:id="#+id/activity_live_tracking_bottom_sheet") and it works. I can't give you an explanation why the other id that marked the LinearLayout directly returning null for getLayoutParams() method.

Error displaying Admob banner in Fragment

I'm familiar with implementing Admobs within my app, however im struggling with changing my code from Activities to Fragments.
I'm getting a NullPointerException. I believe using getView() may be causing my issue.
Admob code: Fragment
//CREATE BANNER ADD
private void createAdmobBanner()
{
// Create an ad.
adView = (com.google.android.gms.ads.AdView) getView()
.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
// .addTestDevice("B3827F13FB335337F4CA0F350B78A866")
.build();
// Load ads into Banner Ads
adView.loadAd(adRequest);
}
XML Admob Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adViewHolder"
android:layout_width="fill_parent"
android:layout_height="#dimen/ad_bottom_height"
android:layout_marginTop="1dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-xxxx/xxxx"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="#dimen/bottom_bar_height"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:text="Refresh Scores"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="11dp"
android:layout_weight="1"
/>
Note: I use ...include layout="#layout/admob_ad" /> in my fragment layout xml.
Code Error:
09-24 22:04:02.429: E/AndroidRuntime(5375): java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
09-24 22:04:02.429: E/AndroidRuntime(5375): at info.androidhive.slidingmenu.ChampionsFragment.createAdmobBanner(ChampionsFragment.java:391)
I faced Similar issue, when i checked closely in resourse file
There were two resourse file for my activity:
\res\layout\activity_main_lauch.xml
\res\layout-v21\activity_main_lauch.xml
I was modifing single file, hence it was throwing error. when i apply the change in both files it started working.

Null Pointer Exception of MapView

I've been trying to find the cause of this error:
>07-13 23:44:06.715: E/AndroidRuntime(2932): FATAL EXCEPTION: main
07-13 23:44:06.715: E/AndroidRuntime(2932): java.lang.NullPointerException
07-13 23:44:06.715: E/AndroidRuntime(2932): at android.app.Activity.findViewById(Activity.java:1825)
07-13 23:44:06.715: E/AndroidRuntime(2932): at com.example.usignasync.MainActivity.runOverlaysAgain(MainActivity.java:141)
My aim is to reload some map overlays when the location of the GPS changes, so I have the GPS set up and onLocationChanged() called runOverlaysAgain(), however I can't call a function that uses findViewById outside of a class using Activity. This is because onLocationChanged() is in a class using LocationListener and runOverlaysAgain() is in a class using MapActivity. So basically findViewById can't be run from LocationListener, how would I fix this? Below is runOverlaysAgain() and the second line is line 141 as mentioned in the error:
protected void runOverlaysAgain(){
mapView = (MapView) findViewById(R.id.mapview);
mapOverlays = mapView.getOverlays();
Log.d("A", Integer.toString(mapOverlays.size()));
mapOverlays.clear();
String latitude;
String longitude;
if(lastKnownLocation!=null){
latitude = GPSTracker.yourLocation.substring(3,GPSTracker.yourLocation.indexOf("Long"));
longitude = GPSTracker.yourLocation.substring(GPSTracker.yourLocation.indexOf("Long")+4, GPSTracker.yourLocation.length());
}
else{
latitude="92000";
longitude ="92000";
}
A lot of the re-declarations were just me trying to figure out what was going on, to no prevail. They can be removed and a similar error will be generated again. So there are two possible things I need, either how to fix this error or how to create mapOverlays outside of onCreate().
Here is the XML:
<Button
android:id="#+id/readWebpage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="readWebpage"
android:text="Load Webpage" >
</Button>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0IhkxetuKVA3ujGbdU8kltq8tCDodiOrV-92Low"/>
<TextView
android:id="#+id/TextView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Example Text" >
</TextView>
</LinearLayout>
Any help is appreciated, thank you.
first of all give this permission
then in res/layout/main.xml declare mapview in this manner
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
/>
and then call the mapview in your main activoty like this
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);

Categories