Checkbox在android中返回NullPointerException

时间:2014-10-17 04:01:06

标签: android nullpointerexception radio-button android-radiogroup

我在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异常。任何人都可以帮我这个。提前致谢。

这是我的logcat

  

10-17 09:54:41.250:E / AndroidRuntime(31698):致命异常:主要   10-17 09:54:41.250:E / AndroidRuntime(31698):   java.lang.RuntimeException:无法启动活动   ComponentInfo {com.isuru.socialdatagathering / com.isuru.socialdatagathering.ViewData}:   java.lang.NullPointerException 10-17 09:54:41.250:   E / AndroidRuntime(31698):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)   10-17 09:54:41.250:E / AndroidRuntime(31698):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)   10-17 09:54:41.250:E / AndroidRuntime(31698):at   android.app.ActivityThread.access $ 700(ActivityThread.java:159)10-17   09:54:41.250:E / AndroidRuntime(31698):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1316)   10-17 09:54:41.250:E / AndroidRuntime(31698):at   android.os.Handler.dispatchMessage(Handler.java:99)10-17   09:54:41.250:E / AndroidRuntime(31698):at   android.os.Looper.loop(Looper.java:137)10-17 09:54:41.250:   E / AndroidRuntime(31698):at   android.app.ActivityThread.main(ActivityThread.java:5419)10-17   09:54:41.250:E / AndroidRuntime(31698):at   java.lang.reflect.Method.invokeNative(Native Method)10-17   09:54:41.250:E / AndroidRuntime(31698):at   java.lang.reflect.Method.invoke(Method.java:525)10-17 09:54:41.250:   E / AndroidRuntime(31698):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1209)   10-17 09:54:41.250:E / AndroidRuntime(31698):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)10-17   09:54:41.250:E / AndroidRuntime(31698):at   dalvik.system.NativeStart.main(Native Method)10-17 09:54:41.250:   E / AndroidRuntime(31698):引起: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):at   android.app.Activity.performCreate(Activity.java:5372)10-17   09:54:41.250:E / AndroidRuntime(31698):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)   10-17 09:54:41.250:E / AndroidRuntime(31698):at   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

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)
        {

        }

    }
}

1 个答案:

答案 0 :(得分:0)

请检查此行似乎此行包含空值。

String manufac = intent.getExtras().getString("manifac");