When I try running this code it says "Error:not an enclosing class:MainActivity" and "Error:Missing method body or declare abstract." And there's probably a really simple answer for this but I am new to Android and Java programming, so sorry for my noob-ishness.
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent myIntent = new Intent (MainActivity.this, MainActivity2.class);
startActivityForResult(myIntent, 0);
Button btn = (Button) findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v);
});
}
//Activities
public void onStart() {
super.onStart();
Log.i("TaskActivity", "MainActivity Started");
}
public void onResume() {
super.onResume();
Log.i("TaskActivity", "MainActivity Resumed");
}
public void onPause() {
super.onPause();
Log.i("TaskActivity", "MainActivity Paused");
}
public void onStop() {
super.onStop();
Log.i("TaskActivity", "MainActivity Stopped");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.i("TaskActivity", "MainActivity Destroyed");
}
}
The first argument of your Intent() should be the current (enclosing) class. The second argument is the Activity to load. Check the Developer Docs here
If you want to launch MainActivity from MainActivity2, reverse the arguments so your Intent looks like:
Intent myIntent = new Intent(MainActivity2.this, MainActivity.class);
startActivity(myIntent);
The error relating to your method likely comes from using the incorrect syntax when assigning your onClickListener. If you are trying to start MainActivity from a button press, you will need to re-write this section:
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
startActivity(intent);
}
});
You need to implement the onClick Method
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
}
});
Related
Im new to android programming and I am trying to build an app with imageviews that open up a new activity for each image view clicked with an image view of their own. so far i have made my main activity and put 4 image views on it all below each other with constraints. all works fine in the sense of when i open the app, the image views are laid out how they should be and i can click on imageview1, then 2, then 3, then 4 and then any of them. but if i open the app and try to click on imageview 2 first or imageview 3 for example, or imageview 4 then it doesn't work. I have to start at 1 and then go on to 2 and then 3 and 4 and then finally i can navigate freely. How can I fix this? I have posted my main activity java file below before. Any help would be appreciated.
package com.ntq.ntqapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
public class MainActivity1 extends AppCompatActivity {
private ImageView ts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
ts = (ImageView)findViewById(R.id.ts1);
ts.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent myIntent = new Intent(MainActivity1.this, imageView1.class);
startActivity(myIntent);
ts = (ImageView)findViewById(R.id.ts2);
ts.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity1.this, imageView2.class);
startActivity(myIntent);
ts = (ImageView)findViewById(R.id.ts3);
ts.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity1.this, imageView3.class);
startActivity(myIntent);
ts = (ImageView) findViewById(R.id.ts4);
ts.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), imageView4.class);
startActivity(myIntent);
}
}
);
}}
);
}});}});}}
You are using the same variable name ts for all the views.
Change to ts1, ts2, ts3, ts4.
Also there are some issues with closing parentheses.
You need to close the parentheses for the onClick method, for the inner classes and for the outer method each time.
ts1 = (ImageView) findViewById(R.id.ts1);
ts.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity1.this, imageView1.class);
startActivity(myIntent);
} //you are missing this
}); //and this
// Notice how I change the variable name to ts2 here...
ts2 = (ImageView)findViewById(R.id.ts2);
//rinse and repeat...
You have two problems. First problem is your end brackets are in the wrong location. The second problem is you are using the same variable. Here is the code corrected:
package com.ntq.ntqapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
public class MainActivity1 extends AppCompatActivity {
private ImageView ts1;
private ImageView ts2;
private ImageView ts3;
private ImageView ts4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
ts1 = (ImageView) findViewById(R.id.ts1);
ts1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity1.this,
imageView1.class);
startActivity(myIntent);
}
});
ts2 = (ImageView) findViewById(R.id.ts2);
ts2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity1.this,
imageView2.class);
startActivity(myIntent);
}
});
ts3 = (ImageView) findViewById(R.id.ts3);
ts3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity1.this,
imageView3.class);
startActivity(myIntent);
}
});
ts4 = (ImageView) findViewById(R.id.ts4);
ts4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),
imageView4.class);
startActivity(myIntent);
}
});
}
You are using the same variable for all the views: private ImageView ts
Use 4 different variables (ts1, ts2...) that will fix your sequence issue !!
The Home Activity where the login button is
package com.example.james.assignment1_18094969;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//findview for the login button
findViewById(R.id.button_login).setOnClickListener(new login());
}
onClickListener for the login button to be clicked and take the user to the login page.
class login implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(Home.this, login.class);
startActivity(intent);
}
}
}
The login screen:
package com.example.james.assignment1_18094969;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
}
Try This and please Confirm Login Activity is declared in the manifest
package com.example.james.assignment1_18094969;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;
public class Home extends AppCompatActivity implements View.OnClickListener {
Button Login;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Login=findViewById(R.id.button_login);
login.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(Home.this, Login.class);
startActivity(intent);
}
}
this will be more correct approach to put click Listener's instead of defining inner class for click Listener. Or you can use functions instead.
public class Home extends AppCompatActivity {
Button loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
loginButton = (Button) findViewById(R.id.button_login);
loginButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent(Home.this, Login.class);
startActivity(intent);
}
});
}
Acitivity names should be used correctly.
Do this!
findViewById(R.id.button_login).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Home.this, Login.class);
startActivity(intent);
}
}
I'm trying to switch actvities. Each activity only has one image view so I didn't find it necessary to show xml. It's a relatively simple program. Based on the IF-STATEMENT the program should switch to its appropriate view. All the images are 72dpi jpegs.
package app.com.example.android.chancetheoracle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "Think Of Your Question, Then Tap Anywhere.",Toast.LENGTH_LONG).show();
}
public void onClick(View view){
Random answer = new Random();
int ask = answer.nextInt(6) + 1;
if (ask == 1){
Intent intent = new Intent(this, redThree.class);
startActivity(intent);
Toast.makeText(this, "2/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
}
else if (ask == 2){
Intent intent = new Intent(this, greenThree.class);
startActivity(intent);
Toast.makeText(this, "3/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
}
else if (ask == 3){
Intent intent = new Intent(this, greenFour.class);
startActivity(intent);
Toast.makeText(this, "4/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
}
else if (ask == 4){
Intent intent = new Intent(this, redFour.class);
startActivity(intent);
Toast.makeText(this, "1/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
}
else if (ask == 5){
Intent intent = new Intent(this, redFive.class);
startActivity(intent);
Toast.makeText(this, "0/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
}
else if (ask == 6){
Intent intent = new Intent(this, greenFive.class);
startActivity(intent);
Toast.makeText(this, "5/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Think Of Your Question, Then Tap Anywhere.",Toast.LENGTH_LONG).show();
}
}
public static class greenThree extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_green_three);
}
}
public static class redThree extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_red_three);
}
}
public static class greenFour extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_green_four);
}
}
public static class redFour extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_red_four);
}
}
public static class greenFive extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_green_five);
}
}
public static class redFive extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_red_five);
}
}
}
You haven't assigned the onClick listener to anything. It's not going to fire on it's own.
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
For the memory issue, refer to this thread:
Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM
https://developer.android.com/reference/android/widget/Button.html
I am a newbie in android app development. For now my challenge is to add two buttons in my UI one for playing an mp3 file and the other for stopping it. I am able to do the first successfully and as I try to do the stop one, I see no result. My code is as following:
package ir.polyglotcenter.childrenmostfrequentwords;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button myButtonOne = (Button) findViewById(R.id.btn_audio);
myButtonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer media = MediaPlayer.create(Main.this, R.raw.audio);
media.start();
}
});
//this is to stop the audio flow
Button myButtonTwo = (Button) findViewById(R.id.btn_stop);
myButtonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer media = MediaPlayer.create(Main.this, R.raw.audio);
media.stop();
}
});
}
}
Try this code:
public class Main extends Activity {
protected MediaPlayer mMediaPlayer;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);
Button myButtonOne = (Button) findViewById(R.id.btn_audio);
myButtonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mMediaPlayer.prepare();
mMediaPlayer.start();
}
});
//this is to stop the audio flow
Button myButtonTwo = (Button) findViewById(R.id.btn_stop);
myButtonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(mMediaPlayer.isPlaying())
mMediaPlayer.stop();
}
});
}
}
Play and Stop all song for all rows.
The Point is implement MediaPlayer in the loop of if or any loop you have:
if(playandstop==true){
mp=MediaPlayer.create(MainActivity.this, resourceIIIDD);
mp.start();
playandstop=false;
}else{
mp.stop();
playandstop=true;
}
Question 1: I have two activities. I was wondering how to optimize it. I can either create two activities with multiple listeners. Or create multiple Java files for each button(onclick listener)
Question 2: I have tried to create multiple listeners in one Java, but can only get one button to work. What is the syntax for multiple listeners in one Java file? Here is my *updated code: now the issue is no matter what button is clicked on it leads to the same page.
package install.fineline;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class Activity1 extends Activity2 {
Button Button1;
Button Button2;
Button Button3;
Button Button4;
Button Button5;
Button Button6;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fineline);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
Button1 = (Button) findViewById(R.id.autobody);
Button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
}
});
Button2 = (Button) findViewById(R.id.glass);
Button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
}
});
Button3 = (Button) findViewById(R.id.wheels);
Button3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
}
});
Button4 = (Button) findViewById(R.id.speedy);
Button4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
}
});
Button5 = (Button) findViewById(R.id.sevan);
Button5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
}
});
Button6 = (Button) findViewById(R.id.towing);
Button6.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
}
});
}
}
activity2.java
package install.fineline;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Activity2 extends Activity {
Button Button1;
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autobody);
}
Button Button2;
public void onCreate2(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.glass);
}
Button Button3;
public void onCreate3(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wheels);
}
Button button4;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.speedy);
}
Button Button5;
public void onCreate5(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sevan);
}
Button Button6;
public void onCreate6(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.towing);
}
}
From the code you posted, all your buttons are leading to the same page because you have the same code in each listener:
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
Where Activity1.class is the class object of the activity you wish to start. If you wanted to start Activity2 from that button, you need to put Activity2.class as the second parameter to Intent.
In your Activity2 example, note that only onCreate() is going to get called, because that method overrides the onCreate() method in the Activity base class. onCreate1, onCreate2, etc. will never be called unless you call them explicitly, because they do not override anything in the base class.
As per your original question, it is fine to create the OnClickListeners inline as you're doing in Activity1, or you could create them as class variables for better organization, and to keep the amount of inline code to a minimum. For example:
OnClickListener button1Listener = new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity1.class);
startActivity(intent);
}
});
and then you can set your click listener as:
Button1.setOnClickListener(button1Listener);
I would consider creating a new .java file for every OnClickListener overkill.