I have an issue with intents. I use it in main activity and call another activity but on click its call again main activity. Here is my code
Main activity.java
package com.umair.facebook;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
ImageView img1;
TextView txt, txt3;
Button like, share, update;
EditText et1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img1 = (ImageView) findViewById(R.id.img);
txt = (TextView) findViewById(R.id.name);
txt3 = (TextView) findViewById(R.id.status);
like = (Button) findViewById(R.id.Like);
share = (Button) findViewById(R.id.Share);
update = (Button) findViewById(R.id.Comment);
et1 = (EditText) findViewById(R.id.editText1);
like.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
// TODO Auto-generated method stub
like.setText("Liked");
}
});
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
share.setText("Shared");
}
});
update.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub startGame();
}
});
}
private void startGame() {
// TODO Auto-generated method stub
Intent lanuchGame = new Intent(MainActivity.this, Result.class);
startActivity(lanuchGame);
}
}
Result.java (Second Activity)
package com.umair.facebook;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class Result extends MainActivity {
ImageView img;
TextView txt1, txt2;
Button btn1, btn2,btn3;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.result_1);
super.onCreate(savedInstanceState);
img =(ImageView)findViewById(R.id.img1);
txt1= (TextView)findViewById(R.id.nam);
txt2= (TextView)findViewById(R.id.stats);
btn1=(Button)findViewById(R.id.Liked);
btn2=(Button)findViewById(R.id.Shared);
et=(EditText)findViewById(R.id.editText); } }
Call super.onCreate(savedInstanceState) before setContentView() in your second Activity.
Related
I've started coding in android. While running my app, I'm getting message "Unfortunately, myapp is stopped".
I've attached the code below.
Can you tell me what's wrong with this code?
The code is given below :
package in.developer.rjsharma.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText fno = (EditText) findViewById(R.id.fno);
String no1=fno.getText().toString();
int first=Integer.parseInt(no1);
EditText sno = (EditText) findViewById(R.id.sno);
String no2=fno.getText().toString();
int second=Integer.parseInt(no2);
final int r1=first+second;
final int r2=first-second;
final int r3=first*second;
final int r4=first/second;
Button add = (Button) findViewById(R.id.add);
Button subtract = (Button) findViewById(R.id.subtract);
Button multiply = (Button) findViewById(R.id.multiply);
Button divide = (Button) findViewById(R.id.divide);
final TextView solution = (TextView) findViewById(R.id.solution);
add.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(r1);
}
});
subtract.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(r2);
}
});
multiply.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(r3);
}
});
divide.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(r4);
}
});
}
}
You are not allowed to set Integer in Textview. You have to convert Integer to String
package in.developer.rjsharma.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText fno = (EditText) findViewById(R.id.fno);
String no1=fno.getText().toString();
int first=Integer.parseInt(no1);
EditText sno = (EditText) findViewById(R.id.sno);
String no2=fno.getText().toString();
int second=Integer.parseInt(no2);
final int r1=first+second;
final int r2=first-second;
final int r3=first*second;
final int r4=first/second;
Button add = (Button) findViewById(R.id.add);
Button subtract = (Button) findViewById(R.id.subtract);
Button multiply = (Button) findViewById(R.id.multiply);
Button divide = (Button) findViewById(R.id.divide);
final TextView solution = (TextView) findViewById(R.id.solution);
add.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(String.valueOf(r1));
}
});
subtract.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(String.valueOf(r2));
}
});
multiply.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(String.valueOf(r3));
}
});
divide.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
solution.setText(String.valueOf(r4));
}
});
}
}
You are making some mistakes in the code. You are reading the values of EditText fno in the onCreate(), it has just been created, their value would be an empty String. If you parse that to a value, I don't know if it will be 0 or null.
However, I would try it this way (not tested - likely to be errors with solution or other variables):
public class MainActivity extends AppCompatActivity {
private EditText fno, sno;
private TextView solution;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText fno = (EditText) findViewById(R.id.fno);
EditText sno = (EditText) findViewById(R.id.sno);
Button add = (Button) findViewById(R.id.add);
Button subtract = (Button) findViewById(R.id.subtract);
Button multiply = (Button) findViewById(R.id.multiply);
Button divide = (Button) findViewById(R.id.divide);
TextView solution = (TextView) findViewById(R.id.solution);
add.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
solution.setText(readIntegerFromEditText(fno)+readIntegerFromEditText(sno));
}
});
subtract.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
solution.setText(readIntegerFromEditText(fno)-readIntegerFromEditText(sno));
}
});
multiply.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
solution.setText(readIntegerFromEditText(fno)*readIntegerFromEditText(sno));
}
});
divide.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
solution.setText(readIntegerFromEditText(fno)/readIntegerFromEditText(sno));
}
});
}
private int readIntegerFromEditText (EditText editText){
int no = Integer.parseInt(editText.getText());
return no;
}
}
There are a lot of things to be done yet, like checking for 0 division. You will find out on the way, if you use the LogCat and Debugging messages.
Android app, i wrote to test image button doesn't work. I have created a image button and implement an event listener for that button. What's wrong with this source code?
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
public class ImageButtonTestApp extends Activity {
ImageButton imageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void eventListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(ImageButtonTestApp.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
}
}
u have written setOnClickListener in different method but u didnot call that method any where call that method in oncreate.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
eventListenerOnButton();
}
try this one,
Try it
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(ImageButtonTestApp.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
I have a page that has two buttons to "two" other pages, the thing is, button 1 and button 2 lead to a second page, thought button 1 should lead to the first page and button 2 should lead to the second page, here is the java file, I don't know how to put it!
ActiveMain.java
package com.d.di;
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 MainActivity extends Activity {
Button button1;
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button1 = (Button) findViewById(R.id.abus);
button2 = (Button) findViewById(R.id.weoff);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageTwo.class);
// you made a mistake here you called the PageOne again here while you should call the second.
startActivity(intent);
}
});
}
}
PageOne.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageOne extends Activity {
Button button1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abus);
}
}
PageTwo
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageTwo extends Activity {
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weoff);
}
}
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageTWO.class); // you made a
mistake here you called the PageOne again here while you should call the second.
startActivity(intent);
}
});
I'm attempting to multithread my code, and there are no errors apart from the one which reads in the title. I'm quite new to coding, so now that I have to supposedly turn my code inside out to allow the app to run smoothly, I'm unsure how to tackle the problem.
My Java code:
package com.bipbapapps.leagueclickerapp;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainClass extends Activity implements OnClickListener {
public float goldCount;
Button minionClick;
Button storeClick;
Button storeDismiss;
TextView textGoldCount;
ImageView spinningBars;
String textTotal;
private SharedPreferences prefs;
PopupWindow pw;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full-screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.mainlayout);
// Initialize variables from popup window
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.storemenu,
null, false));
storeDismiss = (Button) pw.getContentView().findViewById(
R.id.menudismissid);
prefs = getSharedPreferences("LeagueClicker", Context.MODE_PRIVATE);
goldCount = prefs.getFloat("goldCount", 0.0f);
// Linking the variables
minionClick = (Button) findViewById(R.id.minioncentreid);
storeClick = (Button) findViewById(R.id.storeimageid);
textGoldCount = (TextView) findViewById(R.id.textviewtop);
spinningBars = (ImageView) findViewById(R.id.spinningbarsid);
// String which will display at the top of the app
textTotal = goldCount + " Gold";
// Setting TextView to the String
textGoldCount.setText(textTotal);
textGoldCount.setGravity(Gravity.CENTER);
Typeface tf = Typeface.createFromAsset(getAssets(), "mechanical.ttf");
textGoldCount.setTypeface(tf);
textGoldCount.setTextSize(35);
// Setting onClickListeners
minionClick.setOnClickListener(this);
storeClick.setOnClickListener(this);
storeDismiss.setOnClickListener(this);
}
#Override
public void onPause() {
super.onPause();
prefs.edit().putFloat("goldCount", goldCount).commit();
}
#Override
public void onResume() {
super.onResume();
goldCount = prefs.getFloat("goldCount", 0.0f);
t.start();
}
#Override
public void onStop() {
super.onStop();
prefs.edit().putFloat("goldCount", goldCount).commit();
Log.d(prefs.getFloat("goldCount", 0.0f) + "derprolw", "ejwfjbrea");
}
public void barsAnimation() {
Animation rotation = AnimationUtils.loadAnimation(this,
R.anim.spinningbarsanimation);
rotation.setRepeatCount(Animation.INFINITE);
spinningBars.startAnimation(rotation);
}
Thread t = new Thread(){
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.minioncentreid:
goldCount += 1.0;
prefs.edit().putFloat("goldCount", goldCount).commit();
textTotal = goldCount + " Gold";
textGoldCount.setText(textTotal);
textGoldCount.setGravity(Gravity.CENTER);
break;
case R.id.storeimageid:
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupview = inflater.inflate(R.layout.storemenu, null);
final PopupWindow pw = new PopupWindow(popupview, 300, 450);
pw.showAtLocation(v, Gravity.CENTER, 0, 0);
storeDismiss = (Button) pw.getContentView().findViewById(
R.id.menudismissid);
storeDismiss.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
pw.dismiss();
}
});
pw.showAsDropDown(storeClick, 0, 0);
break;
}
};
};
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
The thread is at "Thread t = new Thread(){". Does anyone know what I can do to solve this problem?
this is the context of the Thread, while you are actually looking to use .getSystemService of the Activity.
Use instead:
MainClass.this.getSystemService
The first button works fine, but when I click to the second, nothing happens. I'm not getting any errors, I can't see what's wrong.
Sounds.java
package com.andrew.finnandjake;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Sounds extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
}
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}
}
You can't randomly create a method like you're doing with onCreate1, it's never executed by you or by your Activity.
This is all you need:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(this);
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}
You aren't assigning the onclick listener inside the actual OnCreate. This should work:
package com.andrew.finnandjake;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Sounds extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}