Android开发关于Activity用bundle传值问题

freedragonle freedragonle 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
第一个Activity:
package com.Adr.work3;

import android.app.Activity;
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;

public class Adr3Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.OK);
button.setOnClickListener(myname);
}

private OnClickListener myname = new OnClickListener() {

@Override
public void onClick(View v) {
EditText inname = (EditText) findViewById(R.id.name);

Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("NAME", inname.getText().toString());
intent.putExtras(bundle);
intent.setClass(Adr3Activity.this, Welcome.class);
startActivity(intent);
}

};
}

第二个Activity

package com.Adr.work3;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Welcome extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
TextView receivename=(TextView) findViewById(R.id.welcome_name);
Bundle bundle=this.getIntent().getExtras();
String name=bundle.getString("NAME");
receivename.setText(name);
}
}


错误报告:
02-11 05:17:46.404: E/AndroidRuntime(563): FATAL EXCEPTION: main
02-11 05:17:46.404: E/AndroidRuntime(563): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Adr.work3/com.Adr.work3.Welcome}: java.lang.NullPointerException
02-11 05:17:46.404: E/AndroidRuntime(563): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
02-11 05:17:46.404: E/AndroidRuntime(563): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-11 05:17:46.404: E/AndroidRuntime(563): at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-11 05:17:46.404: E/AndroidRuntime(563): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-11 05:17:46.404: E/AndroidRuntime(563): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 05:17:46.404: E/AndroidRuntime(563): at android.os.Looper.loop(Looper.java:137)
02-11 05:17:46.404: E/AndroidRuntime(563): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-11 05:17:46.404: E/AndroidRuntime(563): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 05:17:46.404: E/AndroidRuntime(563): at java.lang.reflect.Method.invoke(Method.java:511)
02-11 05:17:46.404: E/AndroidRuntime(563): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-11 05:17:46.404: E/AndroidRuntime(563): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-11 05:17:46.404: E/AndroidRuntime(563): at dalvik.system.NativeStart.main(Native Method)
02-11 05:17:46.404: E/AndroidRuntime(563): Caused by: java.lang.NullPointerException
02-11 05:17:46.404: E/AndroidRuntime(563): at com.Adr.work3.Welcome.onCreate(Welcome.java:16)
02-11 05:17:46.404: E/AndroidRuntime(563): at android.app.Activity.performCreate(Activity.java:4465)
02-11 05:17:46.404: E/AndroidRuntime(563): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-11 05:17:46.404: E/AndroidRuntime(563): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-11 05:17:46.404: E/AndroidRuntime(563): ... 11 more

感觉是第二个Activity的receivename.setText(name)一直报错...这句注销掉可以运行...
...全文
评论 5
收藏 已收藏
分享
返回顶部
freedragonle
Android 80,489 成员 91,375 内容