How to use interface between two activities [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am passing my data from one activity to another activity via intent using a bundle.now this bundle is received by another activity here i am gonna display the values i passed.now when the a button is clicked it is suppose to fire a interface which has a function .There it is showing a null point error.
MainActivity.class
public class MainActivity extends AppCompatActivity implements WILO.Communicator {
int Tap=0,loss=9;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CountDowntimer();
}
public void CountDowntimer()
{
new CountDownTimer(3000, 1000) {
#Override
public void onTick(long millisUntilFinished)
{
Tap+=1;
loss-=2;
}
#Override
public void onFinish()
{
Bundle arg=new Bundle();
arg.putInt("Tap",Tap);
arg.putInt("Loss",loss);
Intent i=new Intent(getBaseContext(),WILO.class);
i.putExtras(arg);
startActivity(i);
}
}.start();
}
#Override
public void Restart()
{
CountDowntimer();
}
}
WILO.class
public class WILO extends Activity {
Communicator communicator;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wilo);
Bundle arg=getIntent().getExtras();
Button Restart;
TextView Tap,Loss;
Restart= (Button) findViewById(R.id.Restart);
Tap= (TextView) findViewById(R.id.Tap);
Loss= (TextView) findViewById(R.id.Loss);
Loss.setText(String.valueOf(arg.getInt("Loss")));
Tap.setText(String.valueOf(arg.getInt("Tap")));
Restart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
communicator.Restart();
finish();
}
});
}
interface Communicator
{
public void Restart();
}
}
Error
FATAL EXCEPTION: main
Process: com.matrix.storm.question, PID: 27805
java.lang.NullPointerException
at com.matrix.storm.question.WILO$1.onClick(WILO.java:34)
at android.view.View.performClick(View.java:4452)
at android.view.View$PerformClick.run(View.java:18451)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5421)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:979)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
at dalvik.system.NativeStart.main(Native Method)

How to use interface between two activities
You can not connect two activities using interfaces. If you want to send receive data between two activities, you can use the help of intent arguments and ActivityResult method.
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}

Related

Passing ArrayList from SecondActivity to MainActivity Android

my problem is simple, I'm trying to pass an arraylist from my second activity to my mainActivity.
However when my onRestart() in my main activity is called when the user presses back from secondActivity the arraylist seems to be null.
IN MAIN ACTIVITY
#Override public void onRestart() {
super.onRestart();
DefaultLocations = (ArrayList<String>) getIntent().getSerializableExtra("updated");
///default locations is null
}
SECOND ACTIVITY
#Override public void onPause() {
super.onPause();
Intent intent = new Intent(ManageLocations.this, MainActivity.class);
intent.putExtra("updated",locationsavailable)///size is 2 currently;
this.setIntent(intent);
}
I've looked around as I know there are many examples but none helped me pass an arraylist BACK to my mainActivity.
All help is appreciated!
In second activity:
ArrayList < String > locationsavailable = new ArrayList < String > ();
locationsavailable.add("location1");
locationsavailable.add("location2");
Intent sentIntent = new Intent(SecondActivity.this, FirstActivity.class);
sentIntent.putExtra("string-array", locationsavailable);
startActivity(sentIntent);
In first activity:
Intent intent = getIntent();
String [] DefaultLocations = intent.getStringArrayExtra("string-array");
Edited:
In your case, I suggest use startActivityForResult to start second activity
and override onActivityResult in first activity.
The best solution for your situation would be to start the Second Activity using the startActivityForResult method. Here is an example of what you should do:
MainActivity
public class MainActivity extends AppCompatActivity {
private static final int GET_LOCATIONS_REQUEST_CODE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = findViewById(R.id.mylayout);
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, GET_LOCATIONS_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_LOCATIONS_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
ArrayList<String> locations = data.getStringArrayListExtra("result");
for(String location : locations) {
System.out.println(String.format("Location from SecondActivity %1$s", location));
}
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
}
SecondActivity
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> locationsavailable = new ArrayList<>();
locationsavailable.add("location1");
locationsavailable.add("location1");
Intent returnIntent = new Intent();
returnIntent.putExtra("result", locationsavailable);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
}
If you don't want to start it as an Activity for result, you should override your #OnStart method in the MainActivity.
you just have to write simple code of some lines to solve your problem.
step 1:
while sending intent from main activity to second activity use startActivityForResult().
//Main Activity
private static final int GET_LOCATION_LIST=1; //you can give any value to variable
Intent intent = new Intent(context,SecondActivity.class);
startActivityForResult(intent,GET_LOCATION_LIST);
step 2: now in Second Activity you have to send the location list back in Main Activity.
//Second Activity
Intent intent = new Intent();
intent.putExtra("updated", locationsavailable);
setResult(Activity.RESULT_OK, intent);
SubmissionMailerClientContactList.this.finish();
step 3: now again in main activity you have to receive that intent using onActivityResult()
//Main Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
DefaultLocations=(ArrayList<String>)getIntent().getSerializableExtra("updated");
}

Android: java.lang.IllegalStateException: Already attached

I'm developing an Android app to store personal info in SQLite database, withdraw them out and display the list in Activity.Actually,the moment users have put data into database and returned to Activity,the page must be refreshed to upgrade the list with something new data.I used the statementonCreate(null) to make it,but an exception came into being:java.lang.IllegalStateException: Already attached.
Here is the code :
/* MainActivity */
public class MainActivity
extends AppCompatActivity
implements View.OnClickListener{
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0x1&&resultCode==RESULT_OK){
onCreate(null); // Upgrade the list
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addItem=(Button)findViewById(R.id.addItem);
addItem.setOnClickListener(this);
.....
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.addItem:
Intent intent=new Intent(this,AddItemActivity.class);
startActivityForResult(intent,0x1);
break;
default:
break;
}
}
}
/*AddItemActivity*/
public class AddItemActivity
extends AppCompatActivity
implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
Button submit=(Button)findViewById(R.id.submit);//get submit button
submit.setOnClickListener(this);
....
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.submit:
EditText editName=(EditText)findViewById(R.id.editName),
editAge=(EditText)findViewById(R.id.editAge);
insert(new Person(
editName.getText().toString(),
Integer.parseInt(editAge.getText().toString())));
//return
setResult(RESULT_OK,null);
finish();
break;
default:
break;
}
}
}
stackTrace is this:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.sqlitetest/com.example.sqlitetest.MainActivity}: java.lang.IllegalStateException: Already attached
at android.app.ActivityThread.deliverResults(ActivityThread.java:4053)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalStateException: Already attached
at android.support.v4.app.FragmentManagerImpl.attachController(FragmentManager.java:2871)
at android.support.v4.app.FragmentController.attachHost(FragmentController.java:104)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:317)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
at com.example.sqlitetest.MainActivity.onCreate(MainActivity.java:29)
at com.example.sqlitetest.MainActivity.onActivityResult(MainActivity.java:23)
at android.app.Activity.dispatchActivityResult(Activity.java:6915)

Error starting Activity from a Class on Android

I have the following class SubMenuToolBar which I usually instantiate from an activity (MainActivity) that passes itself (this) in the constructor upon instantiation:
public class SubMenuToolBar extends Activity {
private android.support.v7.widget.Toolbar mToolbar;
private Activity mActivity;
RelativeLayout mLayout;
public SubToolbar(android.support.v7.widget.Toolbar toolbar, Activity activity) {
mActivity = activity;
mToolbar = toolbar;
mLayout = (RelativeLayout) mToolbar.findViewById(R.id.layout_toolbar);
mLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(mActivity, UserMessagesActivity.class);
startActivity(intent);
}
});
}
MainActivity.cs
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new SubMenuToolBar ((android.support.v7.widget.Toolbar) findViewById(R.id.sub_toolbar), this);
}
Everytime I click on the element registered with the click event handler (in the activity) I get the popular error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
11-20 08:31:33.422 23802-23802/com.xxx.apps.xxx E/AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3918)
11-20 08:31:33.422 23802-23802/com.xxx .apps.xxx E/AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3877)
11-20 08:31:33.422 23802-23802/com.xxx.apps.xxx E/AndroidRuntime: at android.app.Activity.startActivity(Activity.java:4200)
11-20 08:31:33.422 23802-23802/com.xxx.apps.xxx E/AndroidRuntime: at android.app.Activity.startActivity(Activity.java:4168)
11-20 08:31:33.422 23802-23802/com.xxx.apps.xxx E/AndroidRuntime: at com.xxx.apps.xxx.SubMenuToolBar$1.onClick(SubToolbar.java:28)
I have tried using the Activity in the Intent, the Context of the activity, getApplicationContext, and getBaseContext. All throws the same error on line:
Intent intent = new Intent(mActivity, MessagesActivity.class);
I have checked SO and various web resource where most of them point to the activity need to be passed to the class so it's Context is used in the Intent, I have done that as you can see above and made sure all details of the calling Activity is passed and used, the problem is still there.
Any idea what I'm doing wrong here?
try replace this:
Intent intent = new Intent(mActivity, UserMessagesActivity.class);
startActivity(intent);
to this:
Intent intent = new Intent(mActivity, UserMessagesActivity.class);
mActivity.startActivity(intent);
You are a little confused. Activity must not be instantiated with new. You have to start it using Intent. There is two activities but I think you only need one.
public class SubMenuToolBar extends Activity {
RelativeLayout mLayout;
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.your_activity_layout);
android.support.v7.widget.Toolbar mToolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.sub_toolbar);
mLayout = (RelativeLayout)mToolbar.findViewById(R.id.layout_toolbar);
mLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(mActivity, UserMessagesActivity.class);
startActivity(intent);
});
}
And use SubMenuToolBar as your main activity.

Android: No Activity found to handle Intent (trying to add an activity to exsisting app)

Ok so I am very new to the Android programming, I am starting week 2 of this class and cannot for the life of me figure out what is going wrong. I have read/watched tons of tutorials on adding new Activities and nothing works.
Assignment: Use the Activities app and add a fourth activity
My activity is simple, 3 buttons and an image. One button makes the image visible and the other makes it invisible. Third returns back to the main.
Note: I edited the original app to have buttons on Main Activity because it had me hitting center on the d-pad which I found dumb. Another note is that Activity 2 & 3 use the same layout and do basically the same thing from what I can tell
public class MainActivity extends Activity {
String tag = "Events";
int request_Code = 1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---hides the title bar---
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Log.d(tag, "In the onCreate() event");
Button act2Butt = (Button) findViewById(R.id.act2Butt);
Button act3Butt = (Button) findViewById(R.id.act3Butt);
Button act4Butt = (Button) findViewById(R.id.act4Butt);
act2Butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivityForResult(new Intent("net.learn2develop.ACTIVITY2"), request_Code);
}
});
act3Butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivityForResult(new Intent("net.learn2develop.ACTIVITY2"), request_Code);
}
});
act4Butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivityForResult(new Intent("net.learn2develop.MYACTIVITY"), request_Code);
}
});
}
/*
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
//startActivity(new Intent("net.learn2develop.ACTIVITY2"));
//startActivity(new Intent(this, Activity2.class));
startActivityForResult(new Intent(
"net.learn2develop.ACTIVITY2"),
request_Code);
}
return false;
}
*/
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
Toast.makeText(this,data.getData().toString(),
Toast.LENGTH_LONG).show();
}
}
}
public void onStart()
{
super.onStart();
Log.d(tag, "In the onStart() event");
}
public void onRestart()
{
super.onRestart();
Log.d(tag, "In the onRestart() event");
}
public void onResume()
{
super.onResume();
Log.d(tag, "In the onResume() event");
}
public void onPause()
{
super.onPause();
Log.d(tag, "In the onPause() event");
}
public void onStop()
{
super.onStop();
Log.d(tag, "In the onStop() event");
}
public void onDestroy()
{
super.onDestroy();
Log.d(tag, "In the onDestroy() event");
}
public class MyActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity4);
Button yesButt = (Button) findViewById(R.id.yesButton);
Button noButt = (Button) findViewById(R.id.noButton);
Button finButt = (Button) findViewById(R.id.finButton);
final ImageView img1 = (ImageView) findViewById(R.id.image1);
yesButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
img1.setVisibility(View.VISIBLE);
}
});
noButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
img1.setVisibility(View.INVISIBLE);
}
});
finButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent data = new Intent();
data.setData(Uri.parse("OMG IT WORKS"));
setResult(RESULT_OK, data);
finish();
}
});
}
public class Activity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
String defaultName="";
Bundle extras = getIntent().getExtras();
if (extras!=null)
{
defaultName = extras.getString("Name");
}
//---get the EditText view---
EditText txt_username =
(EditText) findViewById(R.id.txt_username);
txt_username.setHint(defaultName);
//---get the OK button---
Button btn = (Button) findViewById(R.id.btn_OK);
//---event handler for the OK button---
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
Intent data = new Intent();
//---get the EditText view---
EditText txt_username =
(EditText) findViewById(R.id.txt_username);
//---set the data to pass back---
data.setData(Uri.parse(
txt_username.getText().toString()));
setResult(RESULT_OK, data);
//---closes the activity---
finish();
}
});
}
I have entered the code for Main Activity, My Activity (the one I made), and Activity 2. My Activity runs great and does exactly what I want it to but if I try to access it from main it dies.
928-928/net.learn2develop.Activities E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: net.learn2develop.Activities, PID: 928
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=net.learn2develop.MYACTIVITY }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1765)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
at android.app.Activity.startActivityForResult(Activity.java:3736)
at android.app.Activity.startActivityForResult(Activity.java:3697)
at net.learn2develop.Activities.MainActivity$3.onClick(MainActivity.java:48)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
This code is for my last attempt and making it work before throwing my hands up. Last thing I did was make My Activity act like the other and use startActivityForResult.
Any help would help greatly. I don't know if it matters or not but I do not have a .class for My Activity in the bin directory but there is one for all the others.
If you need any more info please just ask.
Like I said before I'm really new to the whole Android area.
Edit: Manifest
<activity android:name=".MyActivity"
android:label="My Activity">
<intent-filter>
<action android:name="net.learn2develop.MYACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
You may need to add the Activity to the manifest. If you are sure you have done this, I would recommend using an Intent slightly differently.
Try using an Intent the following way:
Intent intent = new Intent(MyActivity.this, SecondActivity.class);
startActivityForResult(intent, requestCode);
The first parameter of this Intent is the current Activity. The second parameter is theActivity you wish to navigate to. Handling an Intent this way prevents a small typo in the package name which would throw that exception. As long as the Activity you are trying to navigate to is in the manifest and you have set up your Intent like the code above, everything should work fine.
Good luck and happy coding!
you need to add your activity to your manifest
<activity
android:name=".MySecondActivity"
android:label="#string/title_second_activity">
</activity>
net.learn2develop.MYACTIVITY
Android is not finiding the above activity like other engineers said,add this activity in your manifest file so JVM can find which class you are referring to.

Converting String to int from intent

I have 2 activities, and I'm passing edit number editText data to 2nd activity with intent. But in second activity I can't convert data from String to int with any functions. Here is my code:
This is main function:
public class MainActivity extends ActionBarActivity {
public EditText mAge;
public Button mCalculate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAge = (EditText) findViewById(R.id.numberImputEditText);
mCalculate = (Button) findViewById(R.id.calculateButton);
mCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String age = mAge.getText().toString();
Intent mIntent = new Intent(getApplicationContext(),FinalActivity.class);
mIntent.putExtra("age", age);
startActivity(mIntent);
}
});
}
}
and this is 2nd activity where is data passed:
public class FinalActivity extends Activity {
public Button mCalculateAgain;
public TextView mMinAge;
public TextView mMaxAge;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
Intent mIntent = getIntent();
String age = mIntent.getStringExtra("age");
mCalculateAgain = (Button) findViewById(R.id.calculateAgainButton);
mMinAge = (TextView) findViewById(R.id.minAgeTextView);
mMaxAge = (TextView) findViewById(R.id.maxAgeTextView);
Integer i = Integer.parseInt(age);
mMinAge.setText((i/2)+7);
mMaxAge.setText((i-7)*2);
Toast.makeText(getApplicationContext(), age, Toast.LENGTH_LONG).show();
mCalculateAgain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(mIntent);
}
});
}
}
here's the log:
`java.lang.RuntimeException: Unable to start activity ComponentInfo{com.robigroza.halfyourage/com.robigroza.halfyourage.FinalActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x12
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
at android.app.ActivityThread.access$700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x12
at android.content.res.Resources.getText(Resources.java:1058)
at android.widget.TextView.setText(TextView.java:3857)
at com.robigroza.halfyourage.FinalActivity.onCreate(FinalActivity.java:35)
at android.app.Activity.performCreate(Activity.java:5203)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
There's a few types of TextView.setText(...). One takes a CharSequence, which is probably what you expected to use, and another commonly used variant takes an int. The int form expects the parameter to be a resource ID.
mMinAge.setText((i/2)+7);
mMaxAge.setText((i-7)*2);
Given that (i/2)+7 isn't likely to resolve to a string resource, you could do:
mMinAge.setText("" + (i/2)+7);
mMaxAge.setText("" + (i-7)*2);
or better:
mMinAge.setText(String.valueOf((i/2)+7));
mMaxAge.setText(String.valueOf((i-7)*2));
Use this code in MainActivity.java
int age = Integer.parseInt(mage.getText().toString());
Intent i = new Intent(MainActivity.this,FinalActivity.class);
i.putExtra("age", age);
startActivity(i);
Code for FinalActivity .java
Bundle extras = getIntent().getExtras();
int age = extras.getInt("age");
In this way you can send integer value to other activity.
EnJoY coding :)

Categories