Being new to Android Application development i was trying to learn connecting two activities using Intent. I tried a code from a book. It keeps throwing an error saying - 'onCreate(Bundle)' is already defined in MainActivity class as well as the NewActivity class. Would be of great help if i could get a solution.
MainActivity.class
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
View view = getWindow().getDecorView().findViewById(android.R.id.content);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent startIntent = new Intent(MainActivity.this, NewActivity.class);
startActivity(startIntent);
}
});
}
NewActivity.class
public class NewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
}
}
If you want to connect those activities you have to do this :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = getWindow().getDecorView().findViewById(android.R.id.content);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent startIntent = new Intent(MainActivity.this, NewActivity.class);
startActivity(startIntent);
}
});
}
And then in yout second activity just delete the:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
}
And copy this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
}
And it will work.
Just change your NewActivity to:
public class NewActivity extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
}
}
A class can contain only one onCreate() method.First learn about Activity Life Cycle http://developer.android.com/training/basics/activity-lifecycle/starting.html
Just remove the first onCreate event on your main activity and new activity. you don't need twice
public class MainActivity extends Activity {
#Override
/*protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}*/
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
View view = getWindow().getDecorView().findViewById(android.R.id.content);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent startIntent = new Intent(MainActivity.this, NewActivity.class);
startActivity(startIntent);
}
});
}
Related
I am trying just to pass the int user input to the next class and print it, see that it works before continuing on using it or something else.
Home.java
start and exit button
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button) findViewById(R.id.b1);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToSecondActivity();
}
});
Button two = (Button) findViewById(R.id.b2);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
private void goToSecondActivity() {
Intent i = new Intent(this, SelectNumberOfPlayers.class);
startActivity(i);
}
}
SelectNumberOfPlayers.java
Taking only the numbers from the input and passing it to StartGame.class
public class SelectNumberOfPlayers extends AppCompatActivity {
EditText numberOfPlayers;
Button three;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_number_of_players);
three = (Button) findViewById(R.id.button3);
three.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String txt = numberOfPlayers.getText().toString();
Intent i = new Intent(getApplicationContext(), StartGame.class);
i.putExtra("players", txt);
startActivity(i);
}
});
}
}
StartGame.java
Receiving Int and printing to TextView
public class StartGame extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("Player number"));
}
}
Where is the error happening? I've set the input keyboard to take only numbers
1. you forgot to initialize youe editext first initialize it in your SelectNumberOfPlayers Activity
numberOfPlayers = (EditText) findViewById(R.id.numberOfPlayers);
2.
the key must be same to pass and receive data with intent
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("players"));
}
When I read Qr code, textView doesn't change.(Question 1) What is the problem ?
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
private Button buton;
private TextView textView;
private ZXingScannerView myview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buton = (Button)findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textView);
buton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myview = new ZXingScannerView(MainActivity.this);
myview.setResultHandler(MainActivity.this);
setContentView(myview);
myview.startCamera();
}
});
}
#Override
protected void onPause() {
super.onPause();
myview.stopCamera();
}
#Override
public void handleResult(Result result) {
setContentView(R.layout.activity_main);
textView.setText(result.getText().toString());
myview.stopCamera();
}
}
And when I finished reading Qr code,I want to start new activty with result.
(Question 2) How I do it ? Will this code work ?
#Override
public void handleResult(Result result) {
myview.stopCamera();
Intent intent = new Intent(getApplicationContext(),SecondActivty.class);
intent.putExtra("Result",result);
startActivity(intent);
}
If it doesn't, how should I fix it ?
Remove setContentView(R.layout.activity_main); from handleResult. You're replacing the view that you have references to with a new view.
As for communicating the Result over intent, as is, what you have will not work. Result does not inherit from Parcelable, and you can't just stick it in an intent and expect it to work. Better would be to get all relevant info from the Result and put it in the Intent as a String.
i'm getting this error
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.bassammetwally.like/com.example.bassammetwally.like.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
when i'm trying to switch activity in another method in the
mainActivity.class
The Code i'm trying to run(not going to include libraries);
public class MainActivity extends AppCompatActivity {
final Intent i = new Intent(this, profile.class);
ImageButton ButtonOne = (ImageButton) findViewById(R.id.profile);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent i = new Intent(this, profile.class);
ImageButton ButtonOne = (ImageButton) findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button();
}
});
}
public void button()
{
startActivity(i);
}
}
code before that worked
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent i = new Intent(this, profile.class);
ImageButton ButtonOne = (ImageButton)findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener(){
public void onClick( View v ){
startActivity(i);
}
});
}}
Questions:
What is the meaning of the error?
why is this error showing?
Try this code:
final Intent i ;
ImageButton ButtonOne ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i =new Intent(MainActivity.this, profile.class);
ButtonOne = (ImageButton) findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button();
}
});
}
public void button()
{
startActivity(i);
}
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton ButtonOne = (ImageButton) findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, profile.class);
startActivity(i);
}
});
}
}
Also Added Profile.java in manifest file
You're initiating variable i twice. When going to function button() you are using the one in public scope (above the onCreate method), not the one from onCreate. Your code should look like this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent i = new Intent(this, profile.class);
ImageButton ButtonOne = (ImageButton) findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button(i);
}
});
}
public void button(Intent i)
{
startActivity(i);
}
}
Hi i am new to programming. I want access arraylist outside from the onCreate but i get error.Below is code.
public class WelcomeOnFootActivity extends AppCompatActivity {
ImageButton next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
List<UserDate> data= new ArrayList();
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle();
}
});
}
private void handle(){
String info=data.getUserInfo;// here i get error
}
}
Just declare the list globally at class level. In your case before OnCreate.
List<UserDate> data= new ArrayList();
en#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle();
}
});
}
private void handle(){
String info=data.getUserInfo;
}
Hope it will help.
public class WelcomeOnFootActivity extends AppCompatActivity {
ImageButton next;
List<UserDate> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
data= new ArrayList();
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle();
}
});
}
private void handle(){
String info=data.getUserInfo;// here i get error
}
}
public class WelcomeOnFootActivity extends AppCompatActivity {
ImageButton next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
final List<UserDate> data= new ArrayList();
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle(data);
}
});
}
private void handle(List<UserDate> listUD){
String info=listUD.getUserInfo;// here i get error
}
}
In an app,there are 3 activities,A,B,C. A is to log in .B is the menu,C is to set values. The working line: A->B->C. In C, there is a button named logout. When clicking logout , I setResult to B ,finish C and start A. In B,I override onActivityResult, when the resultcode is right, I finish B. But the testing data shows when A is restarted by C,B isn't be finished. Solutions are welcomed! Here is the code:
Activity A: LoginActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
login=(Button) findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(LoginActivity.this,MenuActivity.class);
startActivity(intent);
LoginActivity.this.finish();
}
}
}
Activity B: MenuActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
setting=(Button) findViewById(R.id.login);
setting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent settingintent=new Intent(MenuActivity.this, SettingActivity.class);
startActivityForResult(settingintent,1);
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==1){
switch(resultCode){
case RESULT_CANCLED:
break;
case RESULT_OK:
MenuActivity.this.finish();
Log.i(TAG, "I'm killed"+System.currentTimeMillis());
break;
}
}
}
Activity C: Setting.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
logout=(Button) findViewById(R.id.login);
logout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent=new Intent(SettingActivity.this,LoginActivity.class);
startActivity(mIntent);
SettingActivity.this.setResult(RESULT_OK);
SettingActivity.this.finish();
}
}
}
startActivity(intent);//start the next activity
finish(); //finish the current activity
Found a descriptive discussion here
Call finish() at the end of the startActivity(<Your Intent>) in ActivityB. Then ActivityB will not be in stack when moving to ActivityC.