我正在 Android 中开发一个提问器。单击按钮时,复选框值应传递给下一个活动。但是一半的代码运行良好,而另一半则不行。下面的代码返回一个 nullPOinter 异常。
Manufac_RG.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
int selectedID_manfac=Manufac_RG.getCheckedRadioButtonId();
manuf_RB=(RadioButton)findViewById(selectedID_manfac);
manufac_DB=manuf_RB.getText().toString();
Intent i=new Intent(MainActivity.this,ViewData.class);
i.putExtra("manifac",manufac_DB);
}
});
}
但是这段代码运行良好
else if(selectedItem.equalsIgnoreCase("Trading"))
{
trading.setVisibility(View.VISIBLE);
Trading_RG.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
if(Minerals.isChecked())
{
Sand.setVisibility(View.VISIBLE);
Gems.setVisibility(View.VISIBLE);
Others_minaral.setVisibility(View.VISIBLE);
}
else
{
Sand.setVisibility(View.GONE);
Gems.setVisibility(View.GONE);
Others_minaral.setVisibility(View.GONE);
int selectedID_income1_trading=Trading_RG.getCheckedRadioButtonId();
incomeSource1_trading=(RadioButton)findViewById(selectedID_income1_trading);
is1_trading_DB=incomeSource1_trading.getText().toString();
Intent i=new Intent(MainActivity.this,ViewData.class);
i.putExtra("trading1",is1_trading_DB);
}
}
});
manufac.setVisibility(View.GONE);
agri.setVisibility(View.GONE);
}
这是从第二个活动中捕获的。但它返回一个 NullPointer 异常。任何人都可以帮我解决这个问题。先谢谢了。
这是我的日志猫
java.lang.NullPointerException 10-17 09:54:41.250: E/AndroidRuntime(31698): at com.isuru.socialdatagathering.ViewData.onCreate(ViewData.java:97) 10-17 09:54:41.250: E/AndroidRuntime (31698): 在 android.app.Activity.performCreate(Activity.java:5372) 10-17 09:54:41.250: E/AndroidRuntime(31698): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) 10-17 09:54:41.250: E/AndroidRuntime(31698): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257) 10-17 09:54:41.250: E/AndroidRuntime(31698): ... 11 更多 10-17 09:54:50.060: I/Process(31698): 发送信号。PID:31698 SIG:9 在 android.app.Activity.performCreate(Activity.java:5372) 10-17 09:54:41.250: E/AndroidRuntime(31698): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) 10-17 09 :54:41.250: E/AndroidRuntime(31698): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257) 10-17 09:54:41.250: E/AndroidRuntime(31698): ... 11 更多 10- 17 09:54:50.060:I/Process(31698):发送信号。PID:31698 SIG:9 在 android.app.Activity.performCreate(Activity.java:5372) 10-17 09:54:41.250: E/AndroidRuntime(31698): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) 10-17 09 :54:41.250: E/AndroidRuntime(31698): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257) 10-17 09:54:41.250: E/AndroidRuntime(31698): ... 11 更多 10- 17 09:54:50.060:I/Process(31698):发送信号。PID:31698 SIG:9 发送信号。PID:31698 SIG:9 发送信号。PID:31698 SIG:9
public class ViewData extends Activity implements OnClickListener{
TextView roofTV;
TextView floorTV;
TextView wallsTV;
TextView toiletsTV;
TextView roomsTV;
TextView electricityTV;
TextView waterTV;
TextView statusTV;
TextView ownershipTV;
TextView loantakenforTV;
TextView haveBankAccTV;
TextView loansofOtherbanksTV;
TextView currentEmpTV;
TextView newEmpTV;
TextView incomeSource1;
Button Edit,Submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewdata);
roofTV=(TextView)findViewById(R.id.textView_roofV);
floorTV=(TextView)findViewById(R.id.textView_floorV);
wallsTV=(TextView)findViewById(R.id.textView_wallsV);
toiletsTV=(TextView)findViewById(R.id.textView_toiletsV);
roomsTV=(TextView)findViewById(R.id.textView_RoomsV);
electricityTV=(TextView)findViewById(R.id.textView_electricityV);
waterTV=(TextView)findViewById(R.id.textView_waterV);
statusTV=(TextView)findViewById(R.id.textView_statusV);
ownershipTV=(TextView)findViewById(R.id.textView_ownershipV);
loantakenforTV=(TextView)findViewById(R.id.textView_loantakenforV);
haveBankAccTV=(TextView)findViewById(R.id.textView_havebankaccV);
loansofOtherbanksTV=(TextView)findViewById(R.id.textView_loansofotherbanksV);
currentEmpTV=(TextView)findViewById(R.id.textView_currentEmpV);
newEmpTV=(TextView)findViewById(R.id.textView_newEmpV);
incomeSource1=(TextView)findViewById(R.id.textView_is1);
Edit=(Button)findViewById(R.id.button_edit);
Submit=(Button)findViewById(R.id.button_submit);
Edit.setOnClickListener(this);
Submit.setOnClickListener(this);
Intent intent = getIntent();
String roof = intent.getExtras().getString("roof");
String floor = intent.getExtras().getString("floor");
String walls = intent.getExtras().getString("walls");
String toilets= intent.getExtras().getString("toilets");
String rooms = intent.getExtras().getString("rooms");
String electricity = intent.getExtras().getString("electricity");
String water = intent.getExtras().getString("water");
String status= intent.getExtras().getString("status");
String ownership = intent.getExtras().getString("ownership");
String loantakenFor = intent.getExtras().getString("loantakenfor");
String haveBankAcc = intent.getExtras().getString("havebankacc");
String loansOfOtherBnks = intent.getExtras().getString("loansofotherbank");
String currentEmp= intent.getExtras().getString("currentemp");
String newEmp = intent.getExtras().getString("newemp");
String agri1 = intent.getExtras().getString("agri1");
String trading1 = intent.getExtras().getString("trading1");
String manufac = intent.getExtras().getString("manifac");
String services = intent.getExtras().getString("services");
String employeed = intent.getExtras().getString("employeed");
//for db
String branch = intent.getExtras().getString("branch");
String name = intent.getExtras().getString("name");
String nic = intent.getExtras().getString("nic");
roofTV.setText(roof);
floorTV.setText(floor);
wallsTV.setText(walls);
toiletsTV.setText(toilets);
roomsTV.setText(rooms);
electricityTV.setText(electricity);
waterTV.setText(water);
statusTV.setText(status);
ownershipTV.setText(ownership);
loantakenforTV.setText(loantakenFor);
haveBankAccTV.setText(haveBankAcc);
loansofOtherbanksTV.setText(loansOfOtherBnks);
currentEmpTV.setText(currentEmp);
newEmpTV.setText(newEmp);
if(!(agri1.equals("")))
{
incomeSource1.setText(agri1);
}
else if(!(trading1.equals("")))
{
incomeSource1.setText(trading1);
}
else if(!(manufac.equals("")))
{
incomeSource1.setText(manufac);
}
else if(!(services.equals("")))
{
incomeSource1.setText(services);
}
else if(!(employeed.equals("")))
{
incomeSource1.setText(employeed);
}
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.button_edit)
{
onBackPressed();
}
else if(v.getId()==R.id.button_submit)
{
}
}
}