Reputation: 783
First Activity :
It crashes when the button at the first activity gets clicked
package com.example.android_hw_3;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
Button btn;
TextView tv;
Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn2 = (Button) findViewById(R.id.btn2);
btn = (Button) findViewById(R.id.btn1);
tv = (TextView)findViewById(R.id.tv1);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
show();
}
});
btn2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
move();
}
});
}
public void show()
{
tv.setText("This is a test ..Please work :$");
}
public void move()
{
Intent i = new Intent (this,Second.class);
i.putExtra("value", "this is from first :D ");
startActivityForResult(i,1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Second :
package com.example.android_hw_3;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class Second extends Activity {
TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Bundle extra = getIntent().getExtras();
String val1 = extra.getString("value");
TextView tv2 = (TextView)findViewById(R.id.tv2);
tv2.setText(val1);
}
Upvotes: 0
Views: 470
Reputation: 2897
You are calling startActivityForResult
in your move()
method. According to your situation you should use startActivity(intent)
like:
Intent intent = new Intent (MainActivity.this,Second.class);
intent.putExtra("value", "this is from first :D ");
startActivity(i);
Also check in your Manifest that you should add the following line of code under application tag
<activity
android:name="yourpackagename.Second">
</activity>
Hope this will help you
Also you should take a look at difference between startActivity()
and startActivityForResult()
here and here
Upvotes: 2
Reputation: 533
Fix your second activity class like this.
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Bundle extra = getIntent().getExtras();
String val1 = extra.getString("value");
TextView tv2 = (TextView)findViewById(R.id.tv2);
tv2.setText(val1);
}
}
Upvotes: 1
Reputation: 132972
In Second Activity you are initializing TextView before setting layout for Activity so move it after setContentView
as:
TextView tv2; //<< declare TextView here
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tv2 = (TextView)findViewById(R.id.tv2); // initialize here
// your code here...
}
Upvotes: 1