Previously I had error - something about some loop, I've seen info that it is necessary in that case to start a new thread for button, but still nothing happens, thought logs show no errors now.
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int CAPTURE = 9003;
Button button;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.capture);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, "BUTTON clicked", Toast.LENGTH_LONG).show();
Log.d("BUTTON","CLICKED");
}
});
}
});
Intent intent = new Intent(this, CaptureActivity.class);
intent.putExtra(CaptureActivity.AutoFocus, true);
startActivityForResult(intent, CAPTURE);
}
}
runOnUiThread mean you are telling the UI thread to execute the passed Runnable as instruction so it won't create a new thread.
Solution : I assume you this code is for demo purpose for threading and UI updation so one of best alternative is to AsyncTask
and remove runOnUiThread function , no need of it
You need to remove this code or move it inside run method
Intent intent = new Intent(this, CaptureActivity.class);
intent.putExtra(CaptureActivity.AutoFocus, true);
startActivityForResult(intent, CAPTURE);
Because you are currently in CaptureActivity not on MainActivity.
OnCreate will directly take you to the CaptureActivity where you are expecting the code of MainActivity to run (probably they have same UI)
Related
I'm really stuck here. I'm trying to navigate to a second activity using a button but whenever i try to parse in the name of the class to the Intent method, Android Studio throws an error.
In the Intent method toWeightsScreen, it won't let me parse in a class.
Can anyone tell me what I'm messing up please.android studio snapshot
package leith.comstephen.facebook.httpswww.fitnessapp5;
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 MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button navToWeightsScreen = (Button)findViewById(R.id.Firstweights);
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent toWeightsScreen = new Intent(this, cut.class)
startActivity(toWeightsScreen);
}
});
}
}
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent toWeightsScreen = new Intent(getActivity(), cut.class);
startActivity(toWeightsScreen);
}
});
The this in your intent declaration refers to the button's setOnClickListener.
You should specify the actual activity. See the code below
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Changed cut.class to Cut.class to follow coding conventions
Intent toWeightsScreen = new Intent(MainActivity.this, Cut.class);
startActivity(toWeightsScreen);
}
});
what i'm trying to do is a home screen that stays for 5 seconds and goes to activity1.When i click a button in activity1 leads me to activity2.I've tried many times to click the button but no switching happens.
homescreen (5 seconds)=Main_Activity
Activity1=selectpets.java
Activity2=fishtank.java
onclick listener seems the problem i don't know what's wrong with it
Main Activity Code
package com.set.petshome;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button fButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Delay Code after 5 seconds
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.selectscreen); //where <next> is you target activity :)
}
}, 5000);
}
//Delay End
#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;
}
}
Now the Selectpets Code
package com.set.petshome;
import android.app.Activity;
import android.content.*;
import android.os.Bundle;
import android.view.*;
import android.widget.Button;
public class SelectPetsScreen extends Activity {
Button fButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectscreen);
//Button Fishtank Listener Start
fButton = (Button) findViewById(R.id.button1);
//Listening to button event
fButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), fishtank.class);
startActivity(nextScreen);
}
});
//Button Fishtank Listener End
}
}
Fishtank class code
package com.set.petshome;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class fishtank extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ftank);
}
}
by the way no errors in the application just no switching after clicking
thank you very much
Here you aren't ever switching to the next Activity, just changing the layout of the current Activity
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.selectscreen); //where <next> is you target activity :)
}
}, 5000);
instead of setContentView() you need to use an Intent
Intent i = new Intent(MainActivity.this, SelectPetsScreen.this);
startActivity(i);
Since you aren't actually going to the next Activity (java file) your onClick() isn't set.
Edit
This is what you are doing
public class MainActivity extends Activity {
Button fButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Delay Code after 5 seconds
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.selectscreen); //where <next> is you target activity :)
}
}, 5000);
}
This is what you should be doing. Notice the difference in the run() function
public class MainActivity extends Activity {
Button fButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Delay Code after 5 seconds
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(MainActivity.this, SelectPetsScreen.this);
startActivity(i);
}
}, 5000);
}
You can use finish() if you want use firts Activity only once.
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent().setClass(MainActivity.this, SelectPetsScreen .class).setData(getIntent().getData()));
finish();
}
}, 5000);
Be sure that you have 2nd Activity defined in your Manifest.xml:
<activity android:name="x.x.SelectPetsScreen"
android:theme="#style/NoTitle"
android:screenOrientation="nosensor"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
Avoid doing this.
setContentView(R.layout.activity_main);
//Delay Code after 5 seconds
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.selectscreen); //where <next> is you target activity :)
}
}, 5000);
Google gives you the Intent mechanism for switching Activities
i.e. use
startActivity(new Intent(this, yourSecondActivity.class));
instead of
setContentView(R.layout.selectscreen);
The remaining part of your code must work fine.
I was able to solve it with the help of Maxim Shoustin and everyone by:
adding second Activity to Manifest.xml which is SelectPetsScreen
Thank you so much
I am trying to access MainActivity function to my another java class. But i am not able to use these function. Please tell me what else need to be added to get it access.
My code:
Where i am trying to access my MainActivity
package com.example.musicplayer;
**import com.example.musicplayer.MainActivity;**
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class current_song extends Activity implements OnClickListener{
MainActivity ma = new MainActivity();
protected void onCreate(Bundle icicle) {
Bundle extra = getIntent().getExtras();
super.onCreate(icicle);
setContentView(R.layout.songplay_page);
if(extra != null){
String song_name = extra.getString("song_name");
TextView textchange = (TextView)findViewById(R.id.current_song_name);
textchange.setText(song_name);
textchange.setSelected(true);
}
Button btn_pause = (Button)findViewById(R.id.pause_btn);
btn_pause.setOnClickListener(this);
Button btn_next = (Button)findViewById(R.id.next_btn);
btn_next.setOnClickListener(this);
Button btn_prv = (Button)findViewById(R.id.prv_btn);
btn_prv.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "In Onclick ()", Toast.LENGTH_SHORT).show();
switch(v.getId())
{
case R.id.pause_btn:
Toast.makeText(getApplicationContext(), "pause", Toast.LENGTH_SHORT).show();
ma.pause();
break;
case R.id.next_btn:
ma.next();
break;
case R.id.prv_btn:
ma.prv();
break;
}
}
}
Make sure that MainActivity has a zero argument constructor and the access specifier for pause , next and prv function is public.
In response to "i have some methods defined by me stop(), next(), pri() i am trying to access these methods when i click on each button. If you think that "creating a separate common class for sharing all methods" can you please show me 1 example bec i don't know how to access a method from 1 activity to another. "
public class myController{
private MyActivity m;
public myController(MyActivity m){
this.m = m;
}
public void stop(){
m.stop;
}
}
In other classes you initialize in the main activity and pass it the controller object so it can call the stop method
Hello StackOverflow Users,
I am new to android and trying to develop a game in which I use a
1) Main class to redirect (like a menu.. new game, options, help, exit etc..)
2) A surfaceview class
3) A thread to handle drawing on canvas.
I have added an exit button on the main class.
However after playing the game i.e. drawing the objects and using them, when i redirect to my Main class and try to exit; the main screen disappears but the view and threads aren't destroyed.
This is the main class.
package com.tgm.welcome;
import com.tgm.R;
import com.tgm.main.GThread;
import com.tgm.main.TGMActivity;
import com.tgm.options.OptionsMain;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class Welcome_Act extends Activity {
ImageView game, exit, options;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
game = (ImageView) findViewById(R.id.newGame);
options = (ImageView) findViewById(R.id.options);
exit = (ImageView) findViewById(R.id.exit);
game.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
gotogame();
}
});
options.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
goto_opt();
}
});
exit.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
exit_game();
}
});
}
public void gotogame() {
Intent game = new Intent(Welcome_Act.this, TGMActivity.class);
startActivity(game);
}
public void goto_opt() {
Intent opt = new Intent(Welcome_Act.this, OptionsMain.class);
startActivity(opt);
}
public void exit_game() {
System.exit(0);
}
}
PLEASE HELP TO REMOVE THE GAMESCREEN FROM THE STACK THAT ANDROID MAINTAINS.
Thanks..
Using System.exit(0) is not advised in android. It doesnt guarantee finishing the Activity.
Instead of
public void exit_game() {
System.exit(0);
}
Use:
public void exit_game() {
Welcome_Act.finish();
}
Just call finish on the activity you want to remove from the stack.. it does the work you want..
I've looked all over the place and taken all of the tips given, but my app still force closes.
The app has two classes (Main and Next) and "Next" class has a get method. When I try to use the get method in the "Main" class my app force closes. This is the line of code that causes the problems:
timesHit_txtview.setText(next.getTimesHit());
Like I mentioned before, I don't get any errors in eclipse. The First error I get from log cat is "Uncaught handler: thread main exiting due to uncaught exception"
Any suggestions? Thanks in advance
EDIT:
package com.whatever.main;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Next next = new Next();
TextView timesHit_txtview = (TextView) findViewById(R.id.textView2);
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timesHit_txtview.setText(5);
Button next_btn = (Button) findViewById(R.id.button1);
next_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent nextscreen_intent = new Intent(view.getContext(), Next.class);
startActivityForResult(nextscreen_intent, 0);
}
});
}
}
You're trying to initialize a widget before the onCreate() call has been completed.
Initialize your TextView in the onCreate() method before you call setText on it, like so:
public class MainActivity extends Activity {
Next next;
TextView timesHit_txtview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
next = new Next(); // Initialize Next
timesHit_txtview = (TextView) findViewById(R.id.textView2); // Initialize Widget
timesHit_txtview.setText(5);
Button next_btn = (Button) findViewById(R.id.button1);
next_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent nextscreen_intent = new Intent(view.getContext(), Next.class);
startActivityForResult(nextscreen_intent, 0);
}
});
}
In your current code, timesHit_txtview is null when you try to set a text for it, which makes you Activity crash.