我正在尝试构建一个包含两个活动的简单应用程序。第一个活动有 2Buttons
和一个EditText
字段。当按下save&move
第二个活动应该打开并显示在EditText
字段中输入的文本(在第一个活动中)。按下just move
按钮时,第二个活动应该加载(而不做任何事情)。出于某种原因,按下两个按钮时什么都没有发生。
*对不起,如果按钮的名称不是很清楚。提前致谢!
这是第一个活动的代码:
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;
}
}
}
}
这是第二个活动的代码:
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"));
}
}
编辑:
感谢您的回复。新问题,如果按下应用程序会崩溃just move
,有什么想法吗?第一个按钮save&move
工作得很好。
编辑#2,崩溃中的logcat:
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