Android Studio Multiple onCreate methods in MainActivity.java - java

For a school project I am creating a simple app. However, I run into one problem to finalize the app.
I've been trying to make two buttons on my main layout. The buttons are supposed to open a second layout, one called barcode_scanner.xml and another called vragen.xml
However, only the first button opens the scanner. The second button does not do anything.
This is my current code from MainActivity.java
package com.kvprasad.zbarbarcodescanner;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button scannerButton;
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
}
#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);
}
public class bovenbouw extends MainActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.bovenbutton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);}});}}}
I don't see any problems in the code. What am I possibly doing wrong?
Thank you.

Do not make multiple onCreate methods for the same activity. Handle all your button clicks in the same onCreate method. What you can do is this:
package com.kvprasad.zbarbarcodescanner;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button scannerButton;
#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);
}
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next1 = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
Button next2 = (Button) findViewById(R.id.bovenbutton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);}
});
}
}

You don't need 2 onCreate() methods for two buttons. As both of them are in the same layout, you can set onClickListener on both of them in one method only.
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(this, BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
Button next1 = (Button) findViewById(R.id.bovenbutton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(this, Vragen.class);
startActivityForResult(myIntent, 1);
//Make sure the second parameter is not 0, so that you can differentiate between them in onActivityReult method.
}
});
}
}

You don't have to create another class for vragen.class after bovenbutton is pressed.
just create another button for bovenbutton in the main class oncreate method besides the scanner_button
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
Button vragen = (Button) findViewById(R.id.bovenbutton);//button for vragen
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
//here goes the onclicklistener for vragen button
vragen.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);
}
});
}

You're not supposed to register two Activities in single .java file. You must create separate Activity.

package com.example.photosapp
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

Related

Android Studio multiple onCreate methods

I've been trying to make two buttong on my main layout. The buttons are supposed to open a second layout, called barcode_scanner.xml and vragen.xml
However, only the first button opens the scanner. The second button does not do anything.
This is my current code from MainActivity.java
package com.kvprasad.zbarbarcodescanner;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button scannerButton;
//#Override
//protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// scannerButton = (Button) findViewById(R.id.scannerButton);
//scannerButton.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// Intent intent = new Intent(v.getContext(), BarcodeScanner.class);
// startActivity(intent);
// }
// });
//}
#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);
}
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
}
//bovenbouw knop
public class AutoBodyActivity extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.bovenbouw);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);
}
});
}};
}
This is the specific part with the onCreate methods:
#Override
//Barcodescanner knop
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.scannerButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
startActivityForResult(myIntent, 0);
}
});
}
//bovenbouw knop
public class AutoBodyActivity extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.bovenbouw);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Vragen.class);
startActivityForResult(myIntent, 0);
}
});
}};
}
The button with id: 'scannerButton' works fine, but the button with id: 'bovenbouw'
I don't see any mistakes in the code. What is possibly wrong?
You should declare both buttons in the MainActivity.onCreate(..) method
Button b1 = (Button) findViewById(R.id.b1);
Button b2 = (Button) findViewById(R.id.b2);
and then register your onClick listeners to open a new Activity.
For your second activity you'd have an own class SecondActivity with its own onCreate method to take care of stuff.
To open the second Activity you just open a new Intent like so
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent)
hope this helps :)

Cannot Resolve Symbol "OnClickListener"

I am getting the error Cannot Resolve Symbol "OnClickListener" and "Cannot Resolve Symbol "V""
I am starting to get into Java so I am not very good at this and not so familiar with the language
My code:
package com.emiliogaines.counter.counter;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button mButton = (Button) findViewById(R.id.More);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.Antal);
mTextView.setText("Some Text");
}
});
#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);
}
}
I'm not sure why you did that but please move the initializing and listeners to onCreate():
Simply cut these lines of code:
Button mButton = (Button) findViewById(R.id.More);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.Antal);
mTextView.setText("Some Text");
}
});
and paste them in onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButton = (Button) findViewById(R.id.More);
final TextView mTextView = (TextView)findViewById(R.id.Antal);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mTextView.setText("Some Text");
}
});
}
And then go read the basis of coding and it's standards.
U must store this into onCreate method :
#Override
protected void onCreate(Bundle savedInstanceState) {
Button mButton = (Button) findViewById(R.id.More);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final TextView mTextView = (TextView)findViewById(R.id.Antal);
mTextView.setText("Some Text");
}
});
}

Android SDK public void gets an error

I have a problem by following a tutorial for making an app. But now I get an error by adding an respond
for a button. It gives an error at public void sendMessage, but I cant see what is wrong.
package com.example.myfirstapp;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
/** Called when the user clicks the Send button */
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
}
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
those lines
/** Called when the user clicks the Send button */
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
}
go into your class, after
public class MainActivity extends ActionBarActivity {
Your function is outside the activity.
Move
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
}
inside Activity like this:
public class MainActivity extends ActionBarActivity {
//other code i.e. onCreate etc
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
}
}

Android - How to re-start MainActivity after clicking a button in another class?

so I'm fairly new to Android. Lemme just explain first:
I'm trying to make a small portable music app, just for my phone and to test my skills/review what I've learned. I have the mainactivity setup to pick a song with 1 of 4 buttons, and it'll start another activity with buttons to pause, resume, and go back to the song selection screen (MainActivity). I'm trying to get the back button to both release the player and finish the activity, and I've tried many different things but nothing seems to work; the app closes due to some exception (a common one being NullPointerException).
So here's MainActivity:
package me.lemmy.portablemusic.app;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button wreckingBall, happy, lig, ligMulti;
Intent songPickedActivity = new Intent("me.lemmy.portablemusic.app.SONGPICKED");
public static MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set buttons
wreckingBall = (Button) findViewById(R.id.song_wreck);
happy = (Button) findViewById(R.id.song_happy);
lig = (Button) findViewById(R.id.song_lig);
ligMulti = (Button) findViewById(R.id.song_lig_multi);
//listeners
wreckingBall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(songPickedActivity);
player = MediaPlayer.create(MainActivity.this, R.raw.wrecking_ball);
player.start();
}
});
happy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(songPickedActivity);
player = MediaPlayer.create(MainActivity.this, R.raw.happy);
player.start();
}
});
lig.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(songPickedActivity);
player = MediaPlayer.create(MainActivity.this, R.raw.let_it_go);
player.start();
}
});
ligMulti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(songPickedActivity);
player = MediaPlayer.create(MainActivity.this, R.raw.let_it_go_multi);
player.start();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static MediaPlayer getPlayer(){
return player;
}
}
and here's my SongPicked activity:
package me.lemmy.portablemusic.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.media.MediaPlayer;
import android.widget.TextView;
/**
* Created by Lemuel on 6/20/14.
*/
public class SongPicked extends Activity {
TextView text;
Button pause, resume, back;
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_songpicked);
text = (TextView) findViewById(R.id.tvPlaying);
pause = (Button) findViewById(R.id.buttonPause);
resume = (Button) findViewById(R.id.buttonResume);
back = (Button) findViewById(R.id.buttonBack);
player = MainActivity.getPlayer();
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
player.pause();
}
});
resume.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
player.start();
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
player.release();
}
});
}
#Override
protected void onPause(){
super.onPause();
finish();
}
}
Any help is appreciated, thanks!
P.S. I know I can't use copyrighted music in my apps, this is just a test.
I suggest to first make sure the activity is in the AndroidManifest file. Second is to change your intent from this:
Intent songPickedActivity = new Intent("me.lemmy.portablemusic.app.SONGPICKED");
into:
Intent songPickedActivity = new Intent(this, SongPicked.class);
Do it as well on the songPickedActivity when going back to MainActivity. You can use putExtra to send data to next intent. For more information click this link.

On button click switch to new layout xml file, using buttons now but how do I use fragments?

I am creating a Bluetooth app that requires a log in and/or sign up. I use a button click to switch to new layout xml file, but I need to not use buttons and figure out how to use fragments! When I have a button on the second page that gets clicked it crashes the app. Help :(
MainActivity.java
package com.example.chirp;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.bluetooth.BluetoothAdapter;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter() ;
bluetooth.setName("Chirp Bluetooth");
String status; {
if (bluetooth != null)
{
if (bluetooth.isEnabled())
{
String mydeviceaddress = bluetooth.getAddress();
String mydevicename = bluetooth.getName();
// String state = bluetooth.getState();
// status = mydevicename + ":" + mydeviceaddress + ":" + state;
}
else
{
status = "Bluetooth is not Enabled.";
}
}
// Toast.makeText(this, status, Toast.LENGTH_LONG).show();
}
// Configuration config = getResources().getConfiguration();
//
// FragmentManager fragmentManager = getFragmentManager();
// FragmentTransaction fragmentTransaction =
// fragmentManager.beginTransaction();
//
Button quickfindbutton = (Button) findViewById(R.id.qfbutton);
quickfindbutton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
}
});
Button loginbutton = (Button) findViewById(R.id.button1);
loginbutton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
setContentView(R.layout.loginscreen);
};
});
Button signupbutton = (Button) findViewById(R.id.button2);
signupbutton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
setContentView(R.layout.signupscreen);
}
});
// Button login = (Button) findViewById(R.id.login);
// login.setOnClickListener(new OnClickListener(){
// public void onClick(View v){
// setContentView(R.layout.devicepage);
// }
// });
}
public void showToast(String message) {
Toast toast = Toast.makeText(getApplicationContext(), message,
Toast.LENGTH_SHORT);
toast.show();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
setContentView(R.layout.activity_main);
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// public void newMessage(View v){
// Intent intent = new Intent(this,loginscreen.class);
// startActivity(intent);
// }
}
setContentView() should be in onCreate() method within first few lines. Otherwise it will not work. If you need to change the layout after click on the button what you can do is, use SharedPreferences.
Check the saved item in shared Preferences before and then use setContentView() method in onCreate(). Inside the button onClick () method save the wanted layout in SharedPreferences. Then write the code to restart the application.
Check out this link:
http://developer.android.com/guide/components/fragments.html
And also check if your second activity is registered in the manifest.xml.

Categories

Resources