Second button not working - java

I have 2 buttons, the first one works fine but not the second one(button5). It crashes the app upon clicking.
The error encountered is:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
I defined the buttons as such in the XML file:
<Button
android:id="#+id/button"
android:layout_width="182dp"
android:layout_height="49dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="135dp"
android:background="#drawable/pay"
android:fontFamily="#font/roboto"
android:text="Login"/>
<Button
android:id="#+id/button5"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="-136dp"
android:fontFamily="#font/roboto"
android:text="#string/create_new_account"
android:textSize="12sp" />
and called it:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
Button button5 = (Button) findViewById(R.id.button5);
}
public void onClick(View v) {
if (v.getId() == R.id.button) {
Intent intent = new Intent(MainActivity.this, detailspage.class);
startActivity(intent);
} else if (v.getId() == R.id.button5) {
Intent intent = new Intent(MainActivity.this, Registration.class);
startActivity(intent);
}
}
My manifest file is:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".detailspage"/>
<activity android:name=".Registration"/>
<activity android:name=".confirmBicycle"></activity>
</application>
I have went through many similar questions and tried different ways of writing the code but to no avail. Thanks for any advice!

button.setOnClickListener(this);
button5.setOnClickListener(this);
You are implementing the interface in your activity but not passing your activity instance to the listener.

i suggest you to try:
Button button5 = (Button) findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your code
}
});

I copied your layout and pasted in my layout. Also, I created an activity wherein again I've pasted your code. In my case, it is working fine:
public class Activity1 extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
Button button = (Button) findViewById(R.id.button);
Button button5 = (Button) findViewById(R.id.button5);
// this is the only part which I could not find in your code.
button.setOnClickListener(this);
button5.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if(view.getId() == R.id.button){
Intent intent = new Intent(Activity1.this, SplashActivity.class);
startActivity(intent);
}
else if(view.getId() == R.id.button5){
Intent intent = new Intent(Activity1.this, ActivityLifeCycle.class);
startActivity(intent);
}
}
}

Related

errors in main_activity.java file

cannot find symbol class intent, cannot find...Activity2.class, cannot find...Activity-main
public class MainActivity extends AppCompatActivity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.MyButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
NewActivity2.class);
startActivity(myIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
all the imports precede the above code like import widget button, etc.
Open your Manifest and check if there are entries like these:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NewActivity2" />
Overriding method should call super, so it should be:
super.onCreate(savedInstanceState);
and make sure that your NewActivity2 is registered in manifest.

android studio buttons not working

it's my first time posting a question and i already tried more than 5 solutions being told in this site, but none of them works for me, the problem is the buttons are clickable, but not going anywhere i wanted them to, but the main page login button that leads to this page works with the same exact code, below is the menu code
public class AdminMenu extends Activity{
Button b1, b2, b3, b4, b5;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.admin_menu);
b1 = (Button)findViewById(R.id.staff);
b2 = (Button)findViewById(R.id.stock);
b3 = (Button)findViewById(R.id.incoming);
b4 = (Button)findViewById(R.id.outgoing);
b5 = (Button)findViewById(R.id.logout1);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(AdminMenu.this, StaffSelection.class);
startActivity(intent);
finish();
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(AdminMenu.this, StockSelection.class);
startActivity(intent);
finish();
}
});
b3.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(AdminMenu.this, IncomingSelection.class);
startActivity(intent);
finish();
}
});
b4.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(AdminMenu.this, OutgoingSelection.class);
startActivity(intent);
finish();
}
});
b5.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Toast.makeText(getApplicationContext(), "Logging Out...",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(AdminMenu.this, Login.class);
startActivity(intent);
finish();
}
});
}
}
then here is the xml for the app interface
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#drawable/background">
<Button
android:layout_width="330dp"
android:layout_height="70dp"
android:text="STAFF"
android:id="#+id/staff"
android:layout_marginTop="23dp"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp"
android:background="#52cc85"
android:clickable="true"
android:contextClickable="true" />
<Button
android:layout_width="330dp"
android:layout_height="70dp"
android:text="STOCK"
android:id="#+id/stock"
android:layout_below="#+id/staff"
android:layout_alignLeft="#+id/staff"
android:layout_alignStart="#+id/staff"
android:layout_marginTop="20dp"
android:layout_alignRight="#+id/staff"
android:layout_alignEnd="#+id/staff"
android:textSize="25dp"
android:background="#52cccc"/>
<Button
android:layout_width="330dp"
android:layout_height="70dp"
android:text="INCOMING SHIPMENT"
android:id="#+id/incoming"
android:layout_below="#+id/stock"
android:layout_alignLeft="#+id/stock"
android:layout_alignStart="#+id/stock"
android:layout_marginTop="20dp"
android:layout_alignRight="#+id/stock"
android:layout_alignEnd="#+id/stock"
android:textSize="25dp"
android:background="#52cc85"/>
<Button
android:layout_width="330dp"
android:layout_height="70dp"
android:text="OUTGOING SHIPMENT"
android:id="#+id/outgoing"
android:layout_below="#+id/incoming"
android:layout_alignLeft="#+id/incoming"
android:layout_alignStart="#+id/incoming"
android:layout_marginTop="20dp"
android:layout_alignRight="#+id/incoming"
android:layout_alignEnd="#+id/incoming"
android:textSize="25dp"
android:background="#52cccc"/>
<Button
android:layout_width="150dp"
android:layout_height="70dp"
android:text="LOGOUT"
android:id="#+id/logout1"
android:layout_marginTop="20dp"
android:layout_below="#+id/outgoing"
android:layout_alignRight="#+id/outgoing"
android:layout_alignEnd="#+id/outgoing" />
</RelativeLayout>
and here is the manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.reversesky.mwms">
<application android:allowBackup="true"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".User.Login">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".User.AdminMenu">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".User.StaffMenu">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".User.StaffSelection">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".IS.IncomingSelection">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".OS.OutgoingSelection">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".Stock.StockSelection">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".User.CreateStaff">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".IS.CreateIncoming">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".Stock.CreateStock">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".OS.CreateOutgoing">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
here is the UI1
Here are some Alternative solutions ,
ViewAnimator : This is useful for showing a quick animation, if you want to change the view multiple times in quick succession.
Fragments : Instead of re-drawing the entire view, you can switch out fragments. Each fragment is a kind of mini activity, and overall this will contain the code much better.
Start New Activity : Pass information to an activity to help it set up. The first activity passes information to a common second activity, which knows how to set itself up based off of the information it receives from the first activity.
I tried with your code, it works fine for me
Replace your code like this,
AdminMenu.class
public class AdminMenu extends Activity {
Button b1, b2, b3, b4, b5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admin_menu);
b1 = (Button) findViewById(R.id.staff);
b2 = (Button) findViewById(R.id.stock);
b3 = (Button) findViewById(R.id.incoming);
b4 = (Button) findViewById(R.id.outgoing);
b5 = (Button) findViewById(R.id.logout1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Redirecting...", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(AdminMenu.this, StaffSelection.class);
startActivity(intent);
}
});
}
}
StaffSelection.class
public class StaffSelection extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".AdminMenu">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StaffSelection"
android:label="#string/app_name"
/>
</application>
According to you add your class and other details.I have done only for Staff button click.
I've cleaned up Your solution a little bit.
Entire AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".AdminMenuActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".CreateIncomingActivity"/>
<activity android:name=".CreateOutgoingActivity"/>
<activity android:name=".CreateStaffActivity"/>
<activity android:name=".CreateStockActivity"/>
<activity android:name=".IncomingSelectionActivity"/>
<activity android:name=".LoginActivity"/>
<activity android:name=".OutgoingSelectionActivity"/>
<activity android:name=".StaffMenuActivity"/>
<activity android:name=".StaffSelectionActivity"/>
<activity android:name=".StockSelectionActivity"/>
</application>
Please keep in mind You should align activities names as you wish.
In my recreation, every single activity (except AdminMenuActivity to demonstrate usage) is as silly as:
package com.stackoverflow.tommus.buttonsnotworking;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class CreateIncomingActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_incoming);
}
}
Please keep in mind you should use proper layout id in all activities.
Again, in my recreation every single layout (except activity_admin_menu to demonstrate usage) is as silly as:
Now is the important part.
My activity_menu_admin is the same as yours:
<Button
android:id="#+id/staff"
android:layout_width="330dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:layout_marginTop="23dp"
android:background="#52cc85"
android:clickable="true"
android:text="STAFF"
android:textSize="25dp"
/>
<Button
android:id="#+id/stock"
android:layout_width="330dp"
android:layout_height="70dp"
android:layout_alignEnd="#+id/staff"
android:layout_alignLeft="#+id/staff"
android:layout_alignRight="#+id/staff"
android:layout_alignStart="#+id/staff"
android:layout_below="#+id/staff"
android:layout_marginTop="20dp"
android:background="#52cccc"
android:text="STOCK"
android:textSize="25dp"
/>
<Button
android:id="#+id/incoming"
android:layout_width="330dp"
android:layout_height="70dp"
android:layout_alignEnd="#+id/stock"
android:layout_alignLeft="#+id/stock"
android:layout_alignRight="#+id/stock"
android:layout_alignStart="#+id/stock"
android:layout_below="#+id/stock"
android:layout_marginTop="20dp"
android:background="#52cc85"
android:text="INCOMING SHIPMENT"
android:textSize="25dp"
/>
<Button
android:id="#+id/outgoing"
android:layout_width="330dp"
android:layout_height="70dp"
android:layout_alignEnd="#+id/incoming"
android:layout_alignLeft="#+id/incoming"
android:layout_alignRight="#+id/incoming"
android:layout_alignStart="#+id/incoming"
android:layout_below="#+id/incoming"
android:layout_marginTop="20dp"
android:background="#52cccc"
android:text="OUTGOING SHIPMENT"
android:textSize="25dp"
/>
<Button
android:id="#+id/logout1"
android:layout_width="150dp"
android:layout_height="70dp"
android:layout_alignEnd="#+id/outgoing"
android:layout_alignRight="#+id/outgoing"
android:layout_below="#+id/outgoing"
android:layout_marginTop="20dp"
android:text="LOGOUT"
/>
AdminMenuActivity has some changes:
package com.stackoverflow.tommus.buttonsnotworking;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class AdminMenuActivity extends AppCompatActivity {
Button b1, b2, b3, b4, b5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_menu);
b1 = (Button) findViewById(R.id.staff);
b2 = (Button) findViewById(R.id.stock);
b3 = (Button) findViewById(R.id.incoming);
b4 = (Button) findViewById(R.id.outgoing);
b5 = (Button) findViewById(R.id.logout1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Redirecting...", Toast.LENGTH_SHORT).show();
navigateToActivity(CreateStaffActivity.class);
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Redirecting...", Toast.LENGTH_SHORT).show();
navigateToActivity(CreateStockActivity.class);
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Redirecting...", Toast.LENGTH_SHORT).show();
navigateToActivity(CreateIncomingActivity.class);
}
});
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Redirecting...", Toast.LENGTH_SHORT).show();
navigateToActivity(CreateOutgoingActivity.class);
}
});
b5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Logging Out...", Toast.LENGTH_SHORT).show();
navigateToActivity(LoginActivity.class);
}
});
}
private void navigateToActivity(Class<?> activityClass) {
final Intent intent = new Intent(this, activityClass);
startActivity(intent);
}
}
I am opening new activities with a use of Intents. This way I am sure when creating a new activity I have an activity with proper layout and not messed up ids.
Can you please remove line "android:contextClickable="true" from your b1 button in your xml file and then try again.

Launch another activity rather than mainactivity after splash screen

could anyone tell me how to make my android application perform this order?
1) Splash Screen (SplashActivity) .. which i have done as launcher
2) Intro Slider (WelcomeActivity) .. which i don't know how to make it appear after splash.
3) Main Activity .. I want it to appear after the welcome or i'm gonna launch it from clicking the "GOT IT" button.
Thanks in advance.
If i'm correct,what you are asking for is how to work with intents and handlers. First off, your splashActivity.java should look like this;
public class SplashActivity extends Activity{
//timer in miliseconds, 1000ms = 1s//
private static int SPLASH_TIME_OUT = 2000;
//create first screen showed when app is launched//
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
//showing splashscreen with a timer //
#Override
public void run() {
//this is executed once the timer is over//
Intent i = new Intent(SplashActivity.this, WelcomeActivity.class);
startActivity(i);
finish();
}
},SPLASH_TIME_OUT);
}
}
then declare your menu activity and splash activity in AndroidManifest.xml
for example;
<activity
android:name=".SplashActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".WelcomeActivity"
android:screenOrientation="sensor" />
<activity
android:name=".MainActivity"
android:screenOrientation="sensor" />
Then for how to open you main activity after your welcome activity, just copy and paste the code for SplashActivity.java into your WelcomeActivity,making necessary changes,
but for how to open using button,
see example code below
first off your button show be designed already in your activity_welcome.xml
e.g.
<?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_welcome"
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="com.domainname.yourappname.WelcomeActivity"
android:background="#drawable/splash"
<Button
android:text="#string/got it"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button"
style="#style/Widget.AppCompat.Button.Borderless"
android:textAlignment="center"
android:textSize="30sp"
android:layout_marginTop="41dp"
android:textColorHighlight="#android:color/transparent"
android:textColorHint="#android:color/transparent"
android:layout_below="#+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
In your WelcomeActivity.java
public class WelcomeActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
finish();
Toast.makeText(context, "MainActivity Opened.", Toast.LENGTH_SHORT).show();
}
});
}
}
NB: I don't know what program you are writing or how you have designed it so far, this is just an example and your might need to make adjustments for your actual code to run properly
To answer your question of "HOW" to do that;
First, make sure all activities are declared in your Manifest like so:
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".WelcomeActivity" />
<activity android:name=".MainActivity" />
</application>
Then declare this in SplashActivity at the finish of the splash timer:
//If you're using a "Timer" to count down splash screen
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, WelcomeActivity.class);
startActivity(intent);
}
}, 2000);
In your WelcomeActivity wherever you call the end of the activity:
Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
You can find more information about starting another activity using an intent here including how to add extra data for the next activity to receive. Hope that helps.

button not opening new activity

I've created 3 buttons. Each should open up different activities. I've tested it on my device and it never opens a new activity. Could it be due to the main.xml file where I've used the onClick feature for the button despite no using it within my main activity.
public class Main extends Activity implements View.OnClickListener{
private Button playButton, rulesButton, aboutButton;
#Override
protected void onCreate(Bundle savedInstanceState) {//when the app starts this method is run
super.onCreate(savedInstanceState);
// Set the layout for fragment_layout.xml
setContentView(R.layout.main_layout);
playButton = (Button) findViewById(R.id.button_play);
playButton.setOnClickListener(this);
rulesButton = (Button) findViewById(R.id.button_rules);
rulesButton.setOnClickListener(this);
aboutButton = (Button) findViewById(R.id.button_about);
aboutButton.setOnClickListener(this);
}
public void buttonPlayClick(){
startActivity(new Intent("com.example.will.sata.openGLActivity"));
}
public void buttonRulesClick(){
startActivity(new Intent("com.example.will.sata.DetailsActivity"));
}
public void buttonAboutClick(){
startActivity(new Intent(""));
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button_play:
buttonPlayClick();
break;
case R.id.button_about:
buttonAboutClick();
break;
case R.id.button_rules:
buttonRulesClick();
break;
}
}
}
AndroidManifest.xml
<activity
android:name=".Main"
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.example.will.sata.DetailsActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.example.will.sata.openGLActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
main.layout.xml
android:id="#+id/button_play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play"
android:onClick="PlayGame"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
style="?android:attr/borderlessButtonStyle"
android:focusable="true" />
I have some corrections of your code, I hope it helps:
In main_layout.xml you don't need android:onClick="playGame" because you are using View.OnClickListener in the main class.
You can also use the android:onClick="playGame", but your code in the Main.class would be like this:
public class Main extends Activity {
private Button playButton, rulesButton, aboutButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
playButton = (Button) findViewById(R.id.button_play);
rulesButton = (Button) findViewById(R.id.button_rules);
aboutButton = (Button) findViewById(R.id.button_about);
}
public void playGame(View v) {
switch (v.getId())
{
case R.id.button_play:
Intent intent = new Intent(Main.this, OpenGLActivity.class);
startActivity(intent);
break;
case R.id.button_rules:
Intent intent = new Intent(Main.this, DetailsActivity.class);
startActivity(intent);
break;
}
}
To register a new activity in AndroidManifest.xml you just need to do this
<activity android:name="com.example.will.sata.OpenGLActivity"/>
<activity android:name="com.example.will.sata.DetailsActivity"/>`
To start a new activity from Main.class you have to do this (It is really important to register the Activity first in the AndroidManifest.xml):
Intent intent = new Intent(Main.this, OpenGLActivity.class);
startActivity(intent);
Intent intent = new Intent(Main.this, DetailsActivity.class);
startActivity(intent);
Tip: be careful with the naming conventions

No Activity Found to handle Intent in the class file

I am pretty new to Android. I am learning about passing Intents within activities. Here is my code to pass Intent between 2 activties.
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.btOk);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText etName = (EditText)findViewById(R.id.etName);
String Data = etName.getText().toString();
Intent i = new Intent("com.adhish.passingintentdata.layout2");
Bundle extras = new Bundle();
extras.putString("Name", Data);
i.putExtras(extras);
startActivityForResult(i,1);
}
});
}
layout2.java
public class layout2 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout2);
String myName = null;
Bundle extras = getIntent().getExtras();
if(extras != null)
{
myName = extras.getString("Name");
}
TextView tvData = (TextView)findViewById(R.id.tvData);
tvData.setText(myName);
}
}
Manifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".layout2"
android:label="#string/title_activity_layout2"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.adhish.passingintentdata.MainActivity" />
</activity>
When i run this code and click on the OK button to pass the data, my app crashes with a fatal error.
The error is:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.adhish.passingintentdata.layout2 (has extras) }
Please give me a detailed help about working with this issue, because I am new to Android.
Thanks.
Use com.adhish.passingintentdata.MainActivity as Action string for creating Intent to launch layout2 Activity :
Intent i = new Intent("com.adhish.passingintentdata.layout2");
and in Manifast add intent-filter for layout2 Activity:
<intent-filter>
<action android:name="com.adhish.passingintentdata.layout2" />
</intent-filter>
Use this intent instead:
Intent i = new Intent(MainActivity.this, layout2.class);

Categories

Resources