-1

我有三个名为 MainActivity、ShopActivity 和 BillActivity 的活动。MainActivity 包含单选按钮的代码和一个用于转到下一个活动(即 ShopActivity)的按钮。但是当我运行应用程序时,MainActivity 会打开,当我在选择单选按钮后单击该按钮时,需要打开 ShopActivity,但它却意外关闭了应用程序。该怎么办?

主要活动

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
4

2 回答 2

3

ShopActivitybtnsub没有初始化它的情况下访问

btnsub = (Button)findViewById(..);
于 2013-06-14T10:50:52.500 回答
0

你还没有初始化 btnSub,你只声明了变量。

在设置点击监听器之前,您需要做

btnSub = (Button)findViewById(R.id.btnSub); //Replace R.id.btnSub for the actual ID within your layouts XML file

希望这可以帮助

于 2013-06-14T10:53:31.963 回答