I'm trying to launch another activity from a fragment, but I'm getting an error that sais me "the class ... cannot be referenced from static context".
I'm doing the call from a handler, inside of a PlaceholderFragment
public void wait_launch_handler(int secs){
handler.postDelayed(new Runnable() {
#Override
public void run()
{
Animation fadeout = AnimationUtils.loadAnimation(getActivity(), R.anim.fadeout);
rl_container.startAnimation(fadeout);
Intent main_menu = new Intent(SplashscreenOptimizedActivity.this, MainActivity.class);
startActivity(main_menu);
}
}, secs * 1000 );
}
The error is given by "Intent main_menu = new Intent(SplashscreenOptimizedActivity.this, MainActivity.class);"
Thank you.
inside a Fragment you have to use getActivity() in place of SplashscreenOptimizedActivity.this to retrieve the context of the Activity that hosts your Fragment
Try to use Fragment.getActivity() method instead of SplashscreenOptimizedActivity.this
Related
I'm new to android studio and i have a problem when I'm trying to jump to a new activity, so when the line is:
public class signup_activity extends AppCompatActivity {
ImageButton logupButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup_activity);
logupButton = findViewById(R.id.signuparrow);
logupButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, signup_activity.class);
startActivity(intent);
}
});
}}
I get the error:
'com.example.myapplication.MainActivity' is not an enclosing class
and i so a couple of of people advising to chane the intent to this instead of MainActivity.this
but when im changing to this i get the error:
Cannot resolve constructor 'intent'
Intent intent = new Intent(MainActivity.this, signup_activity.class);
Several things:
First, the first param of Intent() constructor is a context. Since you are on signup_activity you need to do signup_activity.this to use it as a context.
I'd assume you want to go to MainActivity, so your second param should be MainActivity.class. It seems you got the order altered there.
you are in signup_activity and when use Intent in fisrt part you should call the current contex to jump to other activity.
so you should replace
Intent intent = new Intent(signup_activity.this, MainActivity.class);
if you want to jump to signup_activity you can call intent from MainActivity.
I have an application where you can select an item from a AlertDialog spinner like, but I don't know how to make my app, to memorize the selected choice, and then behave my button to this. This is what I have now:
#Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
mp = MediaPlayer.create(getApplicationContext(), R.raw.ais);
mp.start();
Intent myIntent = new Intent(MainActivity.this, Main2Activity.class);
MainActivity.this.startActivity(myIntent);
}
}, 9000);
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
}
});
}
And somehow to this code, I want to add my last selected item button, for example:
If ( last selected item from AlertDialog was = dance)
do the code, that I wrote above.
One way to do it is with an interface. Define an interface in your Alert Dialog fragment and instantiate it like this:
public interface MyDialogListener {
//put whatever data you want to pass as a paramenter, below I have two examples
public void onDataSelectedEvent( String action);
}
MyDialogListener myListener;
The listener will have to be instantiated in the dialog by typecasting your activity to a MyDialogListener.
You could do it like this:
myListener = (MyDialogListener) MainActivity.this;
Or even better would be to use the activity parameter in the dialog's onAttach(...) method.
We are able to do this because later, we will make your activity implement the MyDialogListener interface, which will effectively make the activity a MyDialogListener.
Put this line in the method or listener, maybe an onClick() in your alert dialog. This is for a spinner:
myListener.onDataSelectedEvent(mySpinner.getSelectedItem().toString())
Implement the interface in your Activity that receives the data like this:
public class MyActivity extends Activity
implements MyAlertDialog.MyDialogListener{
Then, in the activity receiving the data, have the interface method look like this:
#Override
public void onDataSelectedEvent(String action) {
//probably better to use a switch statement
if(action == "dance") {
// dance()
} else if(action == "stand") {
.......etc
}
}
There is a good example here: http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents
I have this piece of code which was first implemented into a class that extends Activity and now that class extends Fragment and I don't know how to transform it.
This is my code:
menuButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(ActivityTypes.this, ConnectionScreen.class);
startActivity(intent);
}
});
Given your code is now inside a Fragment, you simply have to replace ActivityTypes.this with getActivity().
Clarification: The first parameter of new Intent() requires a Context. Fragment doesn't extend Context. Calling getActivity() retrieves the container activity, which does extend Context.
actually I am bit confused on some thing which is working in one scenario very well but in other scenario that is not working fine so I am here to explain my problem to identify the wrong move made by me, if any on will identify the problem please mention it. thank you.
working fine:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.distance_demo_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ListViewClass.class);
intent.putExtra(ListViewClass.EXTRAS_TARGET_ACTIVITY, Distance.class.getName());
startActivity(intent);
}
});
NOt working fine:
scan = (Button) view.findViewById(R.id.scan);
scan.setOnClickListener(startListener);
}
//Create an anonymous implementation of OnClickListener
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v) {
Toast.makeText(view.getContext(),"Scanning", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, ListViewClass.class);
intent.putExtra(ListViewClass.EXTRAS_TARGET_ACTIVITY, Distance.class.getName());
startActivity(intent);
}
}
now the problem is in this line
Intent intent = new Intent(MainActivity.this, ListViewClass.class);
when I use this line in this scenario it shows me error on this line:
Error: 'The constructor Intent(MainActivity, Class<ListViewClass>) is undefined' please mention my problem if you are well aware of it.
use a global context variable glaboaly and use it.
Context myContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myContext = MyActivity.this;
}
and use myContext insted of MainActivity.this.
Im not sure whether it solve the problem, just try it out
When your calling Intent intent = new Intent(MainActivity.this, ListViewClass.class); inside MainActivity class. It will work fine. But when your not in Context of MainActivity class. i.e move to other class. This error will occur.
If you want to rectify, You can follow #jithu method. Otherwise store the context in Application class. And use it in anywhere.
In the first case you are overriding the method onClick().
If you override the method it's like you are declaring the function in your activity class.
This is the reason that you can call MainActivity.this
In the second case you are implementing the abstract method onClick(). You can't call MainActivity.this because your "MainActivity.this" is referencing to the current context and this isn't accessible from OnClickListener.class.
I am extending a thread class and from that class I want to start an activity. How to do this?
You need to call startActivity() on the application's main thread. One way to do that is by doing the following:
Initialize a Handler and associate it with the application's main thread.
Handler handler = new Handler(Looper.getMainLooper());
Wrap the code that will start the Activity inside an anonymous Runnable class and pass it to the Handler#post(Runnable) method.
handler.post(new Runnable() {
#Override
public void run() {
Intent intent = new Intent (MyActivity.this, NextActivity.class);
startActivity(intent);
}
});
you can use Something like this.
public class MyActivity extends Activity
{
Handler hander = new Handler(){
public void handleMessage(Message m){
Intent intent = new Intent (MyActivity.this, Next.class);
startActivity(intent);
}
};
pubilc void onCreate(Bundle ic)
{
//your code setContentView() etc....
Thread toRun = new Thread()
{
public void run()
{
hander.sendMessage(1);
}
}
toRun.start();
}
}
Well to start an activity of an class, a class should extends with activity according to me.
But if you want to start activity with some threading function you can do these things.
Instead of extends Thread, use implements Runnable. After that some class that have an Activity you just call the start thread and put your logic and that start Intent.
I think this is the good solution for you.