我有三个名为MainActivity,ShopActivity和BillActivity的活动。 MainActivity包含单选按钮的代码和用于转到下一个活动的Button,即ShopActivity。 但是当我运行应用程序时,MainActivity会打开,当我在选择单选按钮后单击按钮时,需要打开ShopActivity,但它会意外关闭应用程序。怎么办?
MainActivity
package com.example.shopcart;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends Activity {
Button btnshop;
RadioGroup rg;
RadioButton rb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnshop = (Button)findViewById(R.id.button1);
rg = (RadioGroup)findViewById(R.id.rgoffer);
btnshop.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int selectedid = rg.getCheckedRadioButtonId();
rb = (RadioButton)findViewById(selectedid);
Intent intent = new Intent(getApplicationContext(), ShopActivity.class);
//intent.putExtra("key", rb.getText().toString());
startActivity(intent);
}
});
}
@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;
}
}
ShopActivity 它包含复选框和一个用于进行第三项活动的按钮,即BillActivity。
package com.example.shopcart;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
public class ShopActivity extends Activity {
long amt;
CheckBox chs4, chs3, chz10, chone,chapple;
Button btnsub;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
str = getIntent().getExtras().getString("key");
btnsub.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(chs4.isChecked()){
amt = amt+38000;
}
else if(chs3.isChecked()){
amt = amt+29000;
}
else if(chz10.isChecked()){
amt = amt+40500;
}
else if(chone.isChecked()){
amt = amt+42500;
}
else if(chapple.isChecked()){
amt = amt+39000;
}
Intent intent1 = new Intent(ShopActivity.this,BillActivity.class);
intent1.putExtra("key1", str);
intent1.putExtra("key2", amt);
startActivity(intent1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.shop, menu);
return true;
}
}
BillActivity 这里所有数据都是从intent中检索出来并显示在TextViews上。
package com.example.shopcart;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class BillActivity extends Activity {
TextView t1,t2,t3,t4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bill);
t2.setText("Your Discount Value : "+getIntent().getExtras().getString("key1"));
t4.setText("You Have to pay : "+getIntent().getExtras().getString("key2"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bill, menu);
return true;
}
}
这是logcat输出:
06-17 19:11:27.942: D/AndroidRuntime(332): Shutting down VM
06-17 19:11:27.942: W/dalvikvm(332): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-17 19:11:27.982: E/AndroidRuntime(332): FATAL EXCEPTION: main
06-17 19:11:27.982: E/AndroidRuntime(332): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shopcart/com.example.shopcart.ShopActivity}: java.lang.NullPointerException
06-17 19:11:27.982: E/AndroidRuntime(332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-17 19:11:27.982: E/AndroidRuntime(332): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-17 19:11:27.982: E/AndroidRuntime(332): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-17 19:11:27.982: E/AndroidRuntime(332): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-17 19:11:27.982: E/AndroidRuntime(332): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 19:11:27.982: E/AndroidRuntime(332): at android.os.Looper.loop(Looper.java:123)
06-17 19:11:27.982: E/AndroidRuntime(332): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-17 19:11:27.982: E/AndroidRuntime(332): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 19:11:27.982: E/AndroidRuntime(332): at java.lang.reflect.Method.invoke(Method.java:507)
06-17 19:11:27.982: E/AndroidRuntime(332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-17 19:11:27.982: E/AndroidRuntime(332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-17 19:11:27.982: E/AndroidRuntime(332): at dalvik.system.NativeStart.main(Native Method)
06-17 19:11:27.982: E/AndroidRuntime(332): Caused by: java.lang.NullPointerException
06-17 19:11:27.982: E/AndroidRuntime(332): at com.example.shopcart.ShopActivity.onCreate(ShopActivity.java:23)
06-17 19:11:27.982: E/AndroidRuntime(332): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-17 19:11:27.982: E/AndroidRuntime(332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-17 19:11:27.982: E/AndroidRuntime(332): ... 11 more
答案 0 :(得分:3)
ShopActivity
中的您正在访问btnsub
而未初始化
btnsub = (Button)findViewById(..);
答案 1 :(得分:0)
您尚未初始化btnSub,您只声明了变量。
在设置on click侦听器之前,您需要执行
btnSub = (Button)findViewById(R.id.btnSub); //Replace R.id.btnSub for the actual ID within your layouts XML file
希望这有帮助