Button may cause null pointer exception (Android Studio)

new to Android Studio, and I thought everything was fine, but last night I ran into a bug that I just can't fix, despite all my best Google search efforts. The button on one of my actions "may raise java.lang.NullPointerException", except that it continues to fail each time it is clicked. It may just be as simple as ordering a line of code in the wrong place, etc., but I'm so new to Android studio. I do not know where I am wrong.

public class searchPage extends AppCompatActivity { private GoogleApiClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search_page); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); Button goBackButton = (Button) findViewById(R.id.goBackButton); goBackButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(searchPage.this, MainActivity.class)); } }); 

Here is xml

  <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/goBackButton" android:layout_marginTop="88dp" android:layout_below="@+id/spinner4" android:layout_centerHorizontal="true" /> 

And manifest file

  <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.onein.mobilefinalapp"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".searchPage" android:label="@string/title_activity_search_page" android:theme="@style/AppTheme.NoActionBar" /> <activity android:name=".MoviePage" android:label="@string/title_activity_movie_page" android:theme="@style/AppTheme.NoActionBar"></activity> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application> </manifest> 

Here is activity_search_page.xml

 <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_search_page" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout> 

If you need any other information, please let me know.

Any help is much appreciated! Thank you

EDIT - duplicate button removed and added to activity_search_page.xml

+5
source share
3 answers

A call to the setOnClickListener method can throw "java.lang.NullPointerException" less ...

This test analyzes the method’s control and data flow to report possible conditions that are always true or false, expressions whose value is statically proved to be constant, and situations that can lead to breaches of the contract with an error.

This is just a warning , because findViewById will return null if the given identifier is not found in the layout.

You can safely ignore it if you know for sure that the identifier is in the layout used, or you can ignore it with the statement (and possibly @Nullable annotations).

 View v = findViewById(R.id.someId); assert v != null; v.setOnClickListener(...); 
+4
source

First of all, your Button must be in the activity_search_page.xml file for findViewById() to work.

Secondly, I think you need the back button functionality, in this case

  • Remove Button Related Code
  • Add toolbar.setDisplayHomeAsUpEnabled(true);
  • Add below code

     toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(searchPage.this, MainActivity.class)); } }); 
  • Optional use toolbar.setNavigationIcon(R.drawable.your_own_back_button_drawable_here); to use your own drawable.

It may also help.

0
source

As correctly pointed out by @ cricket007 This is a warning that if a resource is not found, null is returned, which in fact causes the application to crash.

Some solutions to this problem:

1) Use the command

 assert object_name != null 

or just do alt+enter , it is one of the best offers of android studio.

2) Use

 if(object!=null) 

3) Use a try and catch (a way to handle Java errors).

 try{ your entire code here; } catch(Exception e){ what to do when exception happens } 
0
source

Source: https://habr.com/ru/post/1245712/


All Articles