Issue
I have a button in my launcher activity which is menu.java and when I click said button I want it to load the other activity MainActivity.java and run the AsyncTask in said actvity without having to click another button.
This is my menu.java activity:
package davidgb.baseballspain;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
public class Menu extends ActionBarActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Button beis = (Button) findViewById(R.id.beis);
beis.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.beis:
Intent intent1 = new Intent(this, MainActivity.class);
startActivity(intent1);
Intent myIntent = new Intent(this, MainActivity.doit.class);
startActivity(myIntent);
//new MainActivity.doit().execute();
break;
default:
break;
}
}
}
The thing is when i click the button in the menu.java it loads the MainActivity but it doesn't run the AsyncTask directly, it just crashes. Im new to coding, any advice will be highly appreciate.
Edit: Fixed this by creating a new class and implementing the AsyncTask there.
Solution
Why you are calling the below
Intent myIntent = new Intent(this, MainActivity.doit.class);
startActivity(myIntent);
break;
It considers that MainActivity.doit.class
is also an Activity. While your Activity is MainActivity
Just put the below lines in your button click
Intent intent1 = new Intent(this, MainActivity.class);
startActivity(intent1);
And call your AsyncTask
inside the MainActivity
onCreate()
method like
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new doitTask().execute();
}
Answered By - Abid Khan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.