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);
Related
I am making a chat app and I want to add a feature like telegram to copy links ,phone numbers and etc.. from long clicking on an auto link.I used this library to add long click listeners on a auto link.I implemented it successfully.But when I do this, i want to show a Lottie animation in the start like this but on long click of a link.
I tried many answer but I get an exception.I already made the layout for custom snakbar.It is given below
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="56dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="#dimen/_3sdp"
android:backgroundTint="#color/snakbar_background"
app:cardCornerRadius="#dimen/_4sdp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lottieSnakbar"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginStart="#dimen/_5sdp"
android:layout_marginEnd="#dimen/_10sdp"
app:lottie_rawRes="#drawable/copy"
app:lottie_autoPlay="true"
app:lottie_loop="false"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Text copied to clipboard"
android:gravity="center_vertical"
android:theme="?snackbarTextViewStyle"
tools:ignore="HardcodedText" />
</LinearLayout>
</androidx.cardview.widget.CardView>
Now how can I achieve this?
You can use an interface in the adapter. And receive and show snakbar from activity
You could use localBroadcast and broadcast a local signal from the adapter and let the other Activity(probably receiver) handle snakbar.
AdapterClass{
LocalBroadcastManager localBroadcastManager = null;
void sendB(parms){
if(localBroadcastManager != null){
localBroadcastManager.sendBroadcast(Intent("NOTIFICATION_RECEIVER"))
}
onCreateViewHolder(){
localBroadcastManager = LocalBroadcastManager.getInstance(applicationContext)
}
onBindViewHolder(){
if(someCondition){
sendB(parms);
}
}
then receive it from an activity then call snakBar
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.
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.
I try to implement Admob in the Goceng project. Since Admob changed, I can't really follow the documentation of this project.
My error is
05-04 12:51:21.959: W/Ads(9017): There was a problem getting an ad response. ErrorCode: 1
Many other people bought this project too, so I think the code is written well, I did not changed anything.
For testing I use:
AdRequest adRequest = new AdRequest.Builder().addTestDevice("XXX").build();
ads.loadAd(adRequest);
Admob id looks like
<string name="admob_id">ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx</string>
I just created an App in Admob and an advertisement block where I have this ID from.
Can anyone tell me my mistake?
Meta-data etc. in manifest is there, as I mentioned it is a finished project which works for many people
Code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
setContentView(R.layout.activity_home);
// Declare object of Utils class;
utils = new Utils(this);
// connect view objects and xml ids
adView = (AdView)this.findViewById(R.id.adView);
// add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.frame_content, fragObjList).commit();
// load ads
Ads.loadAds(adView);
}
activity_home.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray">
<FrameLayout
android:id="#+id/frame_content"
android:layout_above="#+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="#string/admob_id"
ads:adSize="SMART_BANNER"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
findViewById gives out an error, I think it's because it's returning null. My app is forced to close.
SensorManager sensorManager=null;
TextView x= null;
TextView y= null;
TextView z= null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
x= (TextView) findViewById(R.id.tv1);
y= (TextView) findViewById(R.id.tv2);
z= (TextView) findViewById(R.id.tv3);
}
XML:
<TextView
android:id="#+id/tv1"
android:layout_x="10px"
android:layout_y="550px"
android:text="#string/x"
/>
<TextView
android:id="#+id/tv2"
android:layout_x="100px"
android:layout_y="550px"
android:text="#string/y"
/>
<TextView
android:id="#+id/tv3"
android:layout_x="200px"
android:layout_y="550px"
android:text="#string/z"
/>
Can someone please tell me what I'm doing wrong?
SOLVED!
I used the XML given by Sunny at the bottom! Thank you to all!
That's simple.... tv* is not in your layout!
Please post the layout, I am 100% sure it doesn't contain:
<TextView android:id="#+id/tv1"/>
<TextView android:id="#+id/tv2"/>
<TextView android:id="#+id/tv3"/>
Please also learn how to get a logcat before going further in android development! ;-)
Window -> Show View -> Logcat
yes above code working fine and xml file should be like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView>
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
yes that problem and if with xml is not done then
You import android.R;
so that it will give error if xml you have
remove that
and import PACKAGENAME.R;
First : Verify that your TextView Are present on your layout : main.xml :
<TextView android:id="#+id/tv1" .../>
<TextView android:id="#+id/tv2" .../>
<TextView android:id="#+id/tv3" .../>
Second : reBuild your project by following this steps : Project ==> Clean ==> Select your project and click OK .
The reason why this happens is :
findViewById() will search for the TextViews in the View hierarchy established by setContentView(R.layout.main). So if the TextViews aren't in main.xml you will get a null