我可以那样做吗?
public void onClick(View v) {
switch (v.getId()){
case R.id.btn:
edit11=edit1.getText().toString();
edit22=edit2.getText().toString();
Intent i=new Intent(this,selection.class);
i.putExtra("code_value",edit11);
Intent k=new Intent(this,myservice.class);
k.putExtra("time_value",edit22);
this.startService(k);
Intent l=new Intent(this,MainActivity.class);
startActivity(l);
break;
}
}
从上面的活动中,它有 2 个编辑文本字段和一个按钮,当按下按钮时我想要将“code_value”传递给选择活动,将 time_value 传递给 myservice 活动并启动主要活动。
这样做会在选择活动 oncreate 中给我一个空指针异常。我必须提到,如果我只使用 Intent i 并在其中传递 2 个值然后 startactivity(i),它工作正常。
- - - - - - - - - - -更新 - - - - - - - - - - - - - - --------------
选拔类:
public class selection extends FragmentActivity implements OnMyLocationChangeListener{
GoogleMap googleMap;
double latitude=0.0;
double longitude=0.0;
String code_value="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selection);
code_value=getIntent().getExtras().getString("code_value");
...//code for google map
...
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.rb_normal){
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
....
else if(checkedId == R.id.save_location){
new Thread(new Runnable() {
public void run() {
postData(Double.toString(latitude),Double.toString(longitude),code_value);
}
}).start();
.....
public void postData(String lat, String lo ,String code) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpGet htget = new HttpGet("www.test.com"+lat+"/"+lo+"/"+code);
try {
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(htget);
String resp = response.getStatusLine().toString();
Toast.makeText(this, resp, 5000).show();
} catch (ClientProtocolException e) {
Toast.makeText(this, "Error", 5000).show();
} catch (IOException e) {
Toast.makeText(this, "Error", 5000).show();
}
日志猫:
W/dalvikvm(2459): threadid=1: thread exiting with uncaught exception (group=0x2aacc560)
03-28 10:34:35.478: E/AndroidRuntime(2459): Uncaught handler: thread main exiting due to uncaught exception
03-28 10:34:35.498: W/System.err(2459): java.io.IOException: Permission denied
03-28 10:34:35.508: E/AndroidRuntime(2459): FATAL EXCEPTION: main
03-28 10:34:35.508: E/AndroidRuntime(2459): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.datagps/com.example.datagps.selection}: java.lang.NullPointerException
03-28 10:34:35.508: E/AndroidRuntime(2459): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
03-28 10:34:35.508: E/AndroidRuntime(2459): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-28 10:34:35.508: E/AndroidRuntime(2459): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-28 10:34:35.508: E/AndroidRuntime(2459): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-28 10:34:35.508: E/AndroidRuntime(2459): at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 10:34:35.508: E/AndroidRuntime(2459): at android.os.Looper.loop(Looper.java:130)
03-28 10:34:35.508: E/AndroidRuntime(2459): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-28 10:34:35.508: E/AndroidRuntime(2459): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 10:34:35.508: E/AndroidRuntime(2459): at java.lang.reflect.Method.invoke(Method.java:507)
03-28 10:34:35.508: E/AndroidRuntime(2459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
03-28 10:34:35.508: E/AndroidRuntime(2459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
03-28 10:34:35.508: E/AndroidRuntime(2459): at dalvik.system.NativeStart.main(Native Method)
03-28 10:34:35.508: E/AndroidRuntime(2459): Caused by: java.lang.NullPointerException
03-28 10:34:35.508: E/AndroidRuntime(2459): at com.example.datagps.selection.<init>(selection.java:87)
03-28 10:34:35.508: E/AndroidRuntime(2459): at java.lang.Class.newInstanceImpl(Native Method)
03-28 10:34:35.508: E/AndroidRuntime(2459): at java.lang.Class.newInstance(Class.java:1409)
03-28 10:34:35.508: E/AndroidRuntime(2459): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-28 10:34:35.508: E/AndroidRuntime(2459): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
我忘了在清单中显示“”。之后,当我启动我的应用程序时,它会崩溃并给出“IntentService 中的空指针异常”。