Reputation: 676
I am trying to create a sign in sign up page. I have 3 pages ( activities). All I do is click a sign up or sign in button on main activity and it takes me to other pages.
Here is the Java code:
package com.appt.shreyabisht.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
Button btn_sign;
Button btn_sign_up;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_sign = (Button) findViewById(R.id.btn_sign);
btn_sign.setOnClickListener(this);
btn_sign_up = (Button) findViewById(R.id.btn_sign_up);
btn_sign_up.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void btclick() {
startActivity(new Intent("com.appt.shreyabisht.second"));
}
private void btsignclick() {
startActivity(new Intent("com.appt.shreyabisht.third"));
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_sign:
btclick();
break;
case R.id.btn_sign_up:
btsignclick();
break;
}
}
}
I am not sure how multiple buttons work and if I should create a separate public void onClick(View view)
for another button. Please suggest.
When I run the app, the error I get is :
FATAL EXCEPTION: main
Process: com.appt.shreyabisht.test, PID: 832
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appt.shreyabisht.test/com.appt.shreyabisht.test.MainActivity}
Caused by:java.lang.NullPointerException
at com.appt.shreyabisht.test.MainActivity.onCreate(MainActivity.java:25)
(There are more lines but I think this is the one causing the crash)
Here is the activity_main.xml:
<LinearLayout 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" android:padding="@dimen/activity_horizontal_margin"
android:gravity="center|bottom"
tools:context=".MainActivity"
android:background="@drawable/backw"
android:weightSum="1">
<Button
android:layout_width="89dp"
android:layout_height="wrap_content"
android:text="Sign in"
android:textColor="#ffffff"
android:id="@+id/btn_sign"
android:textStyle="bold"
android:layout_gravity="bottom"
android:onClick="buttonOnClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:gravity="center|center_vertical"
android:textStyle="italic"
android:text="@string/App_name"
android:id="@+id/textView"
android:layout_gravity="center_vertical"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sign_up_btn"
android:id="@+id/sign_up"
android:textColor="#ffffff"
android:layout_gravity="bottom" />
Here is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.appt.shreyabisht.test.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.appt.shreyabisht.test.second"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.appt.shreyabisht.second" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.appt.shreyabisht.test.third"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.appt.shreyabisht.third" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Upvotes: 0
Views: 793
Reputation: 5220
the problem is here :
btn_sign_up = (Button) findViewById(R.id.btn_sign_up);
you are trying to find a view by id = btn_sign_up
but you are defining it by another id here :
android:id="@+id/sign_up"
Upvotes: 1