I am working on an android app in which I am using admob to display ads but after I have put banner ad from admob it fails to start.
Here is my xml code,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:auto="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_above="#+id/adView"
android:background="#color/bg"
android:scrollbars="vertical"
app:ignore="NamespaceTypo">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="#dimen/lml"
android:layout_marginEnd="#dimen/lmr"
android:layout_marginTop="#dimen/lmt"
android:layout_marginBottom="#dimen/lmb"
android:background="#color/bg"
>
<TextView
android:id="#+id/lpp1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/content"
android:textColor="#color/content"
android:lineSpacingMultiplier="1.2"
android:background="#color/bg"
android:text="#string/lpp1"
/>
</LinearLayout>
</ScrollView>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
I have added this in the manifest file
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
I have read many questions related to this on stackoverflow and searched for this problem and tried a few things but still couln't solve the problem. Can anyone point out where it the problem.
Java file details
MobileAds.initialize(this, "xxxxxxxxxx");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
Logcat
FATAL EXCEPTION: main
Process: com.example.gurjeet.application, PID: 15393
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gurjeet.application/com.example.gurjeet.application.MainActivity}: 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
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
at android.app.ActivityThread.access$1100(ActivityThread.java:223)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: 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
at com.example.gurjeet.application.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:6877)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
at android.app.ActivityThread.access$1100(ActivityThread.java:223)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
I would suggest that please checkout whether you had add the AdView to the layout of your activity.
Something like this:
layout = findViewById(R.id.root_layout);
layout.addView(mAdView);
then you could load ads with the AdView:
mAdView.loadAds(adRequest);
Related
After updating the android studio to version 3.1.2 & gradle to version 4.4, I've faced weird problem to show snackbar in an activtiy.
here is the code:
#BindView(R.id.layout_coordinator)
CoordinatorLayout coordinatorLayout;
Snackbar.make(coordinatorLayout,"hello", Snackbar.LENGTH_LONG).show();
my XMl layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.LoginActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="70dp"
android:paddingRight="70dp">
<EditText
android:id="#+id/edtMobileNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/txt_RegisterTitle"
android:layout_marginTop="19dp"
android:gravity="center"
android:hint="#string/enterYourMobilePhone"
android:inputType="number"
android:maxLength="11"
android:maxLines="1" />
<Button
android:id="#+id/btn_Confirm"
android:layout_width="130dp"
android:layout_height="40dp"
android:layout_below="#+id/edtMobileNumber"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:background="#drawable/button_background_green"
android:text="#string/confirm"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white" />
</RelativeLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
And this is the errro I've got:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.myproject/com.company.myproject.Activities.LoginActivity}: android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
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:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
at android.view.LayoutInflater.inflate(LayoutInflater.java:476)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.support.design.widget.Snackbar.make(Snackbar.java:143)
at com.company.myproject.Activities.LoginActivity.onCreate(LoginActivity.java:98)
as you can see, the error is about inflating layout but can't understand the reason:
Caused by: android.view.InflateException: can be used only with a valid ViewGroup root and attachToRoot=true
It must be mentioned that I've used every solution that are suggested :
my support design version is : implementation com.android.support:design:27.1.1'
the activtiy is extend from AppCompatActivity
The AppTheme is Theme.AppCompat.Light.NoActionBar
I've used getWindow().getDecorView().findViewById(R.id.layout_coordinator), but nothing is solved
be glad someone could help me.
Finally, I've found the solution:
There is a MERGE tag in the snackbar layout that cause exception while inflating layout. By commenting the related row, the problem is solved.
I'm using support:preference-v7:25.0.1 and in my app I'm using an PreferenceActivity to set username with an EditText. When I run my app, at runtime I get the following error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: it.company.android.aaaa, PID: 8023
java.lang.IllegalStateException: Dialog view must contain an EditText with id #android:id/edit
at android.support.v7.preference.EditTextPreferenceDialogFragmentCompat.onBindDialogView(EditTextPreferenceDialogFragmentCompat.java:67)
at android.support.v7.preference.PreferenceDialogFragmentCompat.onCreateDialog(PreferenceDialogFragmentCompat.java:148)
at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:312)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5633)
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:896)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
at dalvik.system.NativeStart.main(Native Method)
Why I get this error? How can I resolve this problem?
Thank you to all.
Sadly, this is a mistake on Google and I have filed a bug issue
https://code.google.com/p/android/issues/detail?id=231576
The hack to get around this in the mean time is to:
Create this layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:layout_marginBottom="48dp"
android:overScrollMode="ifContentScrolls">
<LinearLayout
android:id="#+id/edittext_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:orientation="vertical">
<EditText
android:id="#android:id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="4dp"
android:paddingEnd="4dp" />
</LinearLayout>
</ScrollView>
Then after you create your object (EditTextPreference editTextPreference = new EditTextPreference(context)), call setDialogLayoutResource(R.layout.THE_LAYOUT_ABOVE);
This will resolve the issue for now
I've begun learning how to program for Android using Google's tutorials, but I've had a huge problem.
I have a Button which when clicked should switch the user to another screen (Activity), but whenever I run the app, it crashes.
This is the code that should start the next Activity when the Button is clicked:
public void addListenerOnButton() {
Button button = (Button) findViewById(R.id.BeginShoppingButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent beginShopping = new Intent(getApplicationContext(), BeginShoppingScreen.class);
startActivity(beginShopping);
}
});
}
This method is called in the onCreate() method so that the listener is active.
This is the XML for the Main Activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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="eddgroceryapp.cartcourse2.Activities.MainActivity">
<Button
android:text="Begin Shopping"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/BeginShoppingButton"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:text="Browse Stores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_below="#+id/BeginShoppingButton"
android:layout_alignParentStart="true"
android:onClick="findAStore"/>
<Button
android:text="Report Locations"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:id="#+id/ReportLocationsButton"
android:layout_below="#+id/BeginShoppingButton"
android:layout_alignParentStart="true"
android:onClick="findAStore"/>
</RelativeLayout>
And this is the XML for the other Activity:
<?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">
<Button
android:text="Choose From Saved Lists"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Choose" />
<Button
android:text="Create A List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Create" />
<Button
android:text="Use Someone Else's List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button4" />
</LinearLayout>
Crash Log:
11-01 13:51:28.166 1345-1345/eddgroceryapp.cartcourse2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: eddgroceryapp.cartcourse2, PID: 1345
android.content.ActivityNotFoundException: Unable to find explicit activity class {eddgroceryapp.cartcourse2/eddgroceryapp.cartcourse2.Activities.BeginShoppingScreen}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1855)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1546)
at android.app.Activity.startActivityForResult(Activity.java:4284)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:4231)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
at android.app.Activity.startActivity(Activity.java:4568)
at android.app.Activity.startActivity(Activity.java:4536)
at eddgroceryapp.cartcourse2.Activities.MainActivity$1.onClick(MainActivity.java:58)
at android.view.View.performClick(View.java:5698)
at android.widget.TextView.performClick(TextView.java:10850)
at android.view.View$PerformClick.run(View.java:22523)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7230)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
11-01 13:51:31.951 1345-1345/eddgroceryapp.cartcourse2 I/Process: Sending signal. PID: 1345 SIG: 9
Did you add <activity> tag for BeginShoppingScreen in Manifest.xml like below:
<activity
android:name="your.package.name.BeginShoppingScreen"
android:label="#string/app_name"
/>
EDIT:
As per your logs:
android.content.ActivityNotFoundException: Unable to find explicit
activity class
{eddgroceryapp.cartcourse2/eddgroceryapp.cartcourse2.Activities.BeginShoppingScreen};
have you declared this activity in your AndroidManifest.xml?
Add below code in you Manifest file:
<activity
android:name="eddgroceryapp.cartcourse2.Activities.BeginShoppingScreen"
android:label="#string/app_name"
/>
It seems you forget to declare your activity in Android Manifest file.
So, all activities from your project must be declare in manifest file.
Add below code into your manifest file :
<activity android:name=".BeginShoppingScreen"/>
You can also set other properties like your activity orientation from here, like this :
<activity
android:name=".BeginShoppingScreen"
android:screenOrientation="portrait">
</activity>
Hello guys This my Xml code
I use Relative Layout
<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=".MapsActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
<Button
android:id="#+id/add_restaurant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="new mark" />
<Button
android:id="#+id/remove_restaurant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#id/add_restaurant"
android:text="clear mark" />
/>
<Button
android:id="#+id/check_map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#id/remove_restaurant"
android:text="just check" />
/>
</RelativeLayout>
It can show correctly just like the following image
Image
But when I opened the APP with my mobile, it just crashed
How can I solve it?
Error Message from Android Studio
10-15 20:43:30.100 20922-20922/com.ncku.addmarker E/AndroidRuntime: FATAL
EXCEPTION: main
Process:
com.ncku.addmarker, PID: 20922
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.ncku.addmarker/com.ncku.addmarker.MapsActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2509)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at com.ncku.addmarker.MapsActivity.onCreate(MapsActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6262)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
For UI, you need to change your code...
If you want to add button on map fragment, then you need to add frame layout. As frame layout give an option to add the component on to the other component as a layer.
For crash, please provide crash logs.
Cheers!!!
I think this error showing because you have to not Generate a API key on google console. If you have try to load a map then you have must to Generate a API key.Try to this link and do step by step..
https://developers.google.com/maps/documentation/android-api/
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
In chapter 3 of Big Nerd Ranch guide, they explain how to differently layout the default and landscape views. But when I create an Android Resource Directory with Landscape orientation and then copy the same .xml from the default layout into the /layout-land and make changes into the latter, the app crashes on my physical device when I rotate to landscape mode.
If I don't change anything in the copied .xml inside the /layout-land file, it runs absolutely fine when rotated.
activity.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:gravity="center"
android:orientation="vertical"
>
<TextView
android:id="#+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button"/>
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/prev_button"/>
<Button
android:id="#+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/next_button"/>
</LinearLayout>
</LinearLayout>
/layout-land/activity.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:gravity="center"
android:orientation="vertical"
>
<TextView
android:id="#+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button"/>
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/prev_button"/>
</LinearLayout>
</LinearLayout>
Logcat
FATAL EXCEPTION: main
Process: com.bignerdranch.android.geoquiz, PID: 5727
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.QuizActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4378)
at android.app.ActivityThread.access$1000(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1436)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
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:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.bignerdranch.android.geoquiz.QuizActivity.onCreate(QuizActivity.java:81)
at android.app.Activity.performCreate(Activity.java:6178)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4378)
at android.app.ActivityThread.access$1000(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1436)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
Cannot figure out what is the problem. Please help
You can have different layout files that appear different, but if you are using findViewById on views that do not exist (such as the next button), then you'll get a NullPointerException when you go to call any methods on that view (such as assign it's click listener, in the case of your error).
The simple solution is to keep all the views in both layouts with the same ids, but just rearrange them according to the orientation.