android studio buttons not working - java

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.

Related

When I click on the TextView the application closes

In my application I am trying to click on the text view and redirect it to another location. But after opening the application, when I click on the text view, my application closes. Where am I making the mistake? Can you help me?
MainActivity;
txt_kayitsayfasinagit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,KaydolActivity.class));
}
});
TextView;
<TextView
android:id="#+id/txt_kayitsayfasinagit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hesabin_yok_mu_kaydol"
android:textColor="#B2B2B2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btn_giris"
app:layout_constraintVertical_bias="0.10" />
Manifest;
<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/Theme.Picker">
<activity android:name=".KaydolActivity" />
<activity android:name=".AnaSayfaActivity" />
<meta-data
android:name="com.google.android.actions"
android:resource="#xml/background_splash" />
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
<activity android:name=".SplashActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Logcat;
enter image description here
enter image description here
enter image description here
Initialize the EditText first, like this:
EditText text;
text = findViewById(R.id.txt_kayitsayfasinagit);
then add listener for the text, like this:
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,KaydolActivity.class));
}
});
I'm not seeing any mistakes in your code. Is the MainActivity code properly inside the onCreate() or onStart() method?
A good tip is to look at the logcat to see the error message.

I arrive on a white screen when I try to switch activities on my android app

I just want to switch activity with a simple button
Button test = findViewById(R.id.please);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("MainActivity","I pass there ");
Intent intent = new Intent(MainActivity.this, TestActivity.class);
startActivity(intent);
Log.d("MainActivity","I pass there too");
}
});
I don't have an error, I just arrive on a white screen and not on my new activity. Logs appear in my console. I also have a log when I create my second activity but this one doesn't appear.
for more code,
This is my manifest :
<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=".HomeActivity">
</activity>
<activity android:name=".ScheduleActivity"></activity>
<activity android:name=".MapActivity"></activity>
<activity android:name=".TestActivity"
android:theme="#style/YourTheme">
</activity>
</application>
TestActivity :
public class TestActivity extends AppCompatActivity {
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_test);
Log.d("TestActivity","I arrived in Test Activity !!");
}
}
the button in activity_main :
</android.support.design.widget.AppBarLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="wtf"
android:background="#color/colorAccent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/please"
/>
Activity_test :
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_map_black_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello there! :D"/>
You are Overriding wrong onCreate method in TestActivity. You have to override this method:
protected void onCreate(#Nullable Bundle savedInstanceState) {}

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

error emulate player developed in android

I am a beginner in programming for Android, and am making a music and video player, which already programmed much of the program, but when the emulator executes I get the following error:
Unfortunately, FlagPlayer has stopped
I have done many researches on the subject but I can not find anything that solves my problem. I hope you can help me.
here is my main class:
package tcc.etec.FlagPlayer;
import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btMusica;
private Button btArtista;
private Button btAlbum;
private Button btVideo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
acaobutton();
}
public void acaobutton(){
btMusica = (Button)findViewById(R.id.btmusica);
btMusica.setOnClickListener((OnClickListener) this);
btArtista = (Button)findViewById(R.id.btartista);
btArtista.setOnClickListener((OnClickListener) this);
btAlbum = (Button)findViewById(R.id.btalbuns);
btAlbum.setOnClickListener((OnClickListener) this);
btVideo= (Button)findViewById(R.id.btvideo);
btVideo.setOnClickListener((OnClickListener) this);
}
public void onClick(View v){
if (v.getId()==R.id.btmusica){
Intent intent = new Intent(MainActivity.this, ListaMusicaActivity.class);
}else if (v.getId()==R.id.btartista){
Intent intent = new Intent(MainActivity.this, ListaArtistaActivity.class);
}else if (v.getId()==R.id.btalbuns){
Intent intent = new Intent(MainActivity.this, ListaAlbumActivity.class);
}else if (v.getId()==R.id.btvideo){
Intent intent = new Intent(MainActivity.this, ListaVideoActivity.class);
}
}
XML:
<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=".MainActivity" >
<Button
android:id="#+id/btmusica"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="19dp"
android:text="Musicas" />
<Button
android:id="#+id/btartista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btmusica"
android:layout_alignBottom="#+id/btmusica"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:text="Artistas" />
<Button
android:id="#+id/btalbuns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="66dp"
android:text="Albuns" />
<Button
android:id="#+id/btvideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btalbuns"
android:layout_alignBottom="#+id/btalbuns"
android:layout_alignRight="#+id/btartista"
android:text="Videos" />
and AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tcc.etec.FlagPlayer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".tcc.etec.FlagPlayer.activity_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=".tcc.etec.FlagPlayer.ListaMusicaActivity" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".tcc.etec.FlagPlayer.MusicPlayerActivity" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Try to replacing your:
public void acaobutton(){
btMusica = (Button)findViewById(R.id.btmusica);
btMusica.setOnClickListener((OnClickListener) this);
btArtista = (Button)findViewById(R.id.btartista);
btArtista.setOnClickListener((OnClickListener) this);
btAlbum = (Button)findViewById(R.id.btalbuns);
btAlbum.setOnClickListener((OnClickListener) this);
btVideo= (Button)findViewById(R.id.btvideo);
btVideo.setOnClickListener((OnClickListener) this);
}
To:
public void acaobutton(){
btMusica = (Button)findViewById(R.id.btmusica);
btMusica.setOnClickListener((OnClickListener) getBaseContext());
btArtista = (Button)findViewById(R.id.btartista);
btArtista.setOnClickListener((OnClickListener) getBaseContext());
btAlbum = (Button)findViewById(R.id.btalbuns);
btAlbum.setOnClickListener((OnClickListener) getBaseContext());
btVideo= (Button)findViewById(R.id.btvideo);
btVideo.setOnClickListener((OnClickListener) getBaseContext());
}

Categories

Resources