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
source share