Reputation: 2062
Im trying to build a simple app that consists of two activities. The first activity has 2 Buttons
and an EditText
field. When pressing save&move
the second activity supposed to open and show the text that was entered in the EditText
field (in the first activity). When pressing just move
button, the second activity supposed to load (and not do anything). For some reason nothing happens when both buttons are pressed.
*sorry if the names of the buttons are not very clear. Thanks in advance!
Heres the code of the first activity:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener
{
private Button nextBT;
private EditText mainEditText;
private String data;
private Button nextPref;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nextBT=(Button)findViewById(R.id.nextButton);
mainEditText=(EditText)findViewById(R.id.dataEditText);
nextPref=(Button)findViewById(R.id.nextPrefBut);
}
public void onClick(View v)
{
int id = v.getId();
switch(id)
{
case R.id.nextButton:
{
data=mainEditText.getText().toString();
if(!(data.equals("")) && (data!=null) )
{
Intent intent = new Intent(MainActivity.this,Other.class);
intent.putExtra("Intent_Data", data);
startActivity(intent);
}
else
{
Toast.makeText(getApplicationContext(), "no data to pass", Toast.LENGTH_SHORT).show();
}
break;
}
case R.id.nextPrefBut:
{
startActivity(new Intent(MainActivity.this, Other.class));
break;
}
}
}
}
Heres the code of the second activity:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Other extends Activity
{
private TextView textFromMain;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
textFromMain = (TextView)findViewById(R.id.mainText);
textFromMain.setText(getIntent().getExtras().getString("Intent_Data"));
}
}
edit:
Thanks for the responses. New problem, the app crashes if just move
is pressed, any ideas? The first button save&move
works just fine.
edit #2, the logcat from the crash:
03-24 12:50:14.135: I/ActivityManager(289): START u0 {cmp=com.example.c/.Other} from pid 851
03-24 12:50:14.314: D/dalvikvm(289): GC_FOR_ALLOC freed 980K, 20% free 8310K/10384K, paused 99ms, total 113ms
03-24 12:50:14.584: D/AndroidRuntime(851): Shutting down VM
03-24 12:50:14.584: W/dalvikvm(851): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-24 12:50:14.625: E/AndroidRuntime(851): FATAL EXCEPTION: main
03-24 12:50:14.625: E/AndroidRuntime(851): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.c/com.example.c.Other}: java.lang.NullPointerException
03-24 12:50:14.625: E/AndroidRuntime(851): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-24 12:50:14.625: E/AndroidRuntime(851): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-24 12:50:14.625: E/AndroidRuntime(851): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-24 12:50:14.625: E/AndroidRuntime(851): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-24 12:50:14.625: E/AndroidRuntime(851): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 12:50:14.625: E/AndroidRuntime(851): at android.os.Looper.loop(Looper.java:137)
03-24 12:50:14.625: E/AndroidRuntime(851): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-24 12:50:14.625: E/AndroidRuntime(851): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 12:50:14.625: E/AndroidRuntime(851): at java.lang.reflect.Method.invoke(Method.java:511)
03-24 12:50:14.625: E/AndroidRuntime(851): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-24 12:50:14.625: E/AndroidRuntime(851): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-24 12:50:14.625: E/AndroidRuntime(851): at dalvik.system.NativeStart.main(Native Method)
03-24 12:50:14.625: E/AndroidRuntime(851): Caused by: java.lang.NullPointerException
03-24 12:50:14.625: E/AndroidRuntime(851): at com.example.c.Other.onCreate(Other.java:16)
03-24 12:50:14.625: E/AndroidRuntime(851): at android.app.Activity.performCreate(Activity.java:5104)
03-24 12:50:14.625: E/AndroidRuntime(851): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-24 12:50:14.625: E/AndroidRuntime(851): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-24 12:50:14.625: E/AndroidRuntime(851): ... 11 more
03-24 12:50:14.664: W/ActivityManager(289): Force finishing activity com.example.c/.Other
03-24 12:50:14.703: W/ActivityManager(289): Force finishing activity com.example.c/.MainActivity
03-24 12:50:14.974: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
03-24 12:50:15.216: W/ActivityManager(289): Activity pause timeout for ActivityRecord{4112d850 u0 com.example.c/.Other}
03-24 12:50:15.623: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
03-24 12:50:15.643: W/EGL_emulation(413): eglSurfaceAttrib not implemented
03-24 12:50:16.443: E/SurfaceFlinger(36): GL error 0x0505
03-24 12:50:16.643: E/SurfaceFlinger(36): GL error 0x0505
03-24 12:50:16.753: I/Process(851): Sending signal. PID: 851 SIG: 9
03-24 12:50:16.773: I/WindowState(289): WIN DEATH: Window{41134870 u0 com.example.c/com.example.c.MainActivity}
03-24 12:50:16.786: I/ActivityManager(289): Process com.example.c (pid 851) has died.
03-24 12:50:16.943: W/InputMethodManagerService(289): Got RemoteException sending setActive(false) notification to pid 851 uid 10052
03-24 12:50:17.156: E/SurfaceFlinger(36): GL error 0x0505
03-24 12:50:17.233: E/SurfaceFlinger(36): GL error 0x0505
03-24 12:50:17.366: E/SurfaceFlinger(36): GL error 0x0505
03-24 12:50:46.890: D/ExchangeService(658): Received deviceId from Email app: null
Upvotes: 1
Views: 2344
Reputation: 1820
Source class :
Intent myIntent = new Intent(this, NewActivity.class);
myIntent.putExtra("firstName", "Your First Name Here");
myIntent.putExtra("lastName", "Your Last Name Here");
startActivity(myIntent)
Destination Class (NewActivity class) :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Intent intent = getIntent();
String fName = intent.getStringExtra("firstName");
String lName = intent.getStringExtra("lastName");
}
Upvotes: 0
Reputation: 797
For clarity, I'll try to summarize what was discussed and concluded:
1) The Initial issue (buttons were non-reactive) was caused because they didn't register any listeners. In order for any widget (Button, textview, etc..) to react in some non-default manner it must register a listener, which will be responsible for that reaction. The solution in this case would simply be to add the following two lines of code at the bottom of OnCreate
:
nextBT.setOnClickListener(this);
nextPref.setOnClickListener(this);
2) The second issue (app crashes when user presses the just move
button) occurs in the Other
class. We see that in MainActivity
the following line is executed when a user presses the save&move
button:
intent.putExtra("Intent_Data", data);
However, no corresponding action is performed when a user presses the just move
button. The result is that sometimes the intent will have a value attached to the Intent_Data
key, and sometimes it wont, depending on which button is pressed. Since no check is performed in Other
, to see if Intent_Data
has a value (i.e.getIntent().getExtras().getString("Intent_Data") != null
), you'll get a NullPointerException every time you press the just move
button. There are a number of simple solutions to this issue. For example, you could add the mentioned check. The best solution, however, would be to keep the intent consistent and simply rewrite the onClick
method as follows:
public void onClick(View v)
{
String data = mainEditText.getText().toString();
if(v.getId() == R.id.nextButton && data.equals("")) {
Toast.makeText(getApplicationContext(), "no data to pass", Toast.LENGTH_SHORT).show();
}
else {
Intent intent = new Intent(MainActivity.this,Other.class);
intent.putExtra("Intent_Data", data);
startActivity(intent);
}
}
Also, Consider using the function: getStringExtra("Intent_Data")
instead of using: getExtras().getString("Intent_Data")
. It does the same thing, but it's shorter and more efficient.
Upvotes: 1
Reputation: 82543
For one, you haven't set on click listeners for your buttons. Add:
nextBT.setOnClickListener(this);
nextPref.setOnClickListener(this);
For two, your switch-case syntax is wrong. case blocks don't need brackets. Try:
switch(id)
{
case R.id.nextButton:
data=mainEditText.getText().toString();
if(!(data.equals("")) && (data!=null) )
{
Intent intent = new Intent(MainActivity.this,Other.class);
intent.putExtra("Intent_Data", data);
startActivity(intent);
}
else
{
Toast.makeText(getApplicationContext(), "no data to pass", Toast.LENGTH_SHORT).show();
}
break;
case R.id.nextPrefBut:
startActivity(new Intent(MainActivity.this, Other.class));
break;
}
Upvotes: 1
Reputation: 20155
You haven't set onClickListener for your buttons.
i.e
Add this in MainActivity
onCreate method.
nextBT.setOnClickListener(this);
nextPref.setOnClickListener(this);
Upvotes: 2