activity传值空指针异常

qq_21435025 2016-09-03 08:58:08
package com.example.android01work;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
private EditText loginphone;
private EditText loginpassword ;
private Button reg;
static final int REQUEST_CODE = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loginphone = (EditText)findViewById(R.id.Login_Phone);
loginpassword = (EditText)findViewById(R.id.Login_Password);
setContentView(R.layout.activity_main);
reg = (Button) findViewById(R.id.reg);
}
public void register(View view) {
Intent intent = new Intent(MainActivity.this, Register.class);
//startActivity(intent);
startActivityForResult(intent, 100);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// TODO Auto-generated method stub
if (100 == requestCode && 201 == resultCode) {
// 接收第二个界面返回的值
// 参数为两边约定好的暗号
String str1 = data.getStringExtra("phone1");
Log.i("tag", str1);
loginphone.setText(str1);
loginpassword.setText(data.getStringExtra("password1"));
}

}
@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}



package com.example.android01work;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import cn.smssdk.SMSSDK;

public class Register extends Activity {
private ImageButton back;
private Button btn;
private EditText phone;
private EditText password;
private String APPKEY = "cbf1779753ce";
private String appsecret = "a10b46a5abbd7daada912f6859eecff6";
static final int RESULT_CODE = 201;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
// 初始化sdk
SMSSDK.initSDK(this, APPKEY, appsecret);
// 配置信息
phone = (EditText) findViewById(R.id.phonenumber);
password = (EditText)findViewById(R.id.password);
back = (ImageButton) findViewById(R.id.back);

btn = (Button) findViewById(R.id.identify_code);

btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
btn.setTextColor(Color.RED);
// TODO Auto-generated method stub
String string = phone.getText().toString();
SMSSDK.getVerificationCode("86",string);



}
});

}

public void reback(View view) {

Intent intent = new Intent();
intent.putExtra("phone1", phone.getText().toString()+"");
intent.putExtra("password1", password.getText().toString()+"");
setResult(201,intent);
finish();


}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.register, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
...全文
281 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
十三邵 2016-09-05
  • 打赏
  • 举报
回复
setContentView()要在findViewById之前调用。
睡精灵 2016-09-05
  • 打赏
  • 举报
回复
setContentView()要咋findViewById之前调用。
qq_26763799 2016-09-05
  • 打赏
  • 举报
回复
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); loginphone = (EditText)findViewById(R.id.Login_Phone); loginpassword = (EditText)findViewById(R.id.Login_Password); setContentView(R.layout.activity_main); reg = (Button) findViewById(R.id.reg); } 改为: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loginphone = (EditText)findViewById(R.id.Login_Phone); loginpassword = (EditText)findViewById(R.id.Login_Password); reg = (Button) findViewById(R.id.reg); } 试试
一不小心_ 2016-09-04
  • 打赏
  • 举报
回复
既然已经知道是空指针了,那你就想办法让它不空啊。初始化值,或者进行非空判断

80,475

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧