0

我的 MainActivity 打开所有小部件都在工作,但是当我选择按钮打开我的第二个活动时,它无法打开并且应用程序崩溃。Eclipse 没有显示错误

我的主要活动课程:

public class MainActivity extends FragmentActivity  {

private int dYear;
private int dMonth;
private int dDay;
private int rYear;
private int rMonth;
private int rDay;
static final int dDATE_DIALOG_ID = 0;
static final int rDATE_DIALOG_ID = 1;
private RadioButton buttonO = null;
private RadioButton buttonR = null;
private TableRow tr;
TextView txD, txR;
private Button btnDepart,btnReturn, btnSubmit,btnClose;
private Spinner noOfPassengersSpinner,departureSpinner,returnSpinner;
private CheckBox tandCCB;
final Context context = this;
private Bundle basket;


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonO = (RadioButton) findViewById(R.id.radioO);
    buttonR = (RadioButton) findViewById(R.id.radioR);
    txD=(TextView)findViewById(R.id.tvDepartureDate);
    txR=(TextView)findViewById(R.id.tvReturnDate);
    btnDepart = (Button)findViewById(R.id.btnDepart);
    btnReturn = (Button)findViewById(R.id.btnReturn);
    tr = (TableRow)findViewById(R.id.tableRow5);
    btnSubmit = (Button)findViewById(R.id.btnSubmit);
    btnClose = (Button)findViewById(R.id.btnClose);
    noOfPassengersSpinner = (Spinner)findViewById(R.id.noOfPassengersSpinner);
    departureSpinner = (Spinner)findViewById(R.id.departureSpinner);
    returnSpinner = (Spinner)findViewById(R.id.returnSpinner);
    tandCCB = (CheckBox)findViewById(R.id.tandCCB);




    final Calendar dC = Calendar.getInstance();
    final Calendar rC = Calendar.getInstance();

    dYear = dC.get(Calendar.YEAR);
    dMonth = dC.get(Calendar.MONTH);
    dDay = dC.get(Calendar.DAY_OF_MONTH);

    rYear = rC.get(Calendar.YEAR);
    rMonth = rC.get(Calendar.MONTH);
    rDay = rC.get(Calendar.DAY_OF_MONTH);


    updateDisplay();




    tandCCB.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if(isChecked){

                btnSubmit.setEnabled(true);

            }
            else{

                btnSubmit.setEnabled(false);
            }
        }

    }); 

    this.btnClose.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
             finish();
             System.exit(0);
        }

    }); 

    this.btnSubmit.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {

            String depAir = departureSpinner.getSelectedItem().toString();
            String desAir = returnSpinner.getSelectedItem().toString();     
            int numOfPass = Integer.parseInt(noOfPassengersSpinner.getSelectedItem().toString());

            basket.putString("departureAir", depAir);
            basket.putString("destenationAir", desAir);
            basket.putInt("numOfPass", numOfPass);
            basket.putString("depDate", txD.getText().toString());

            String type = "";

            if(buttonR.isChecked()){

                String returnDate = txR.getText().toString();
                basket.putString("returnDate", returnDate);
                type = "Return";

            }
            else{
                type = "Oneway";
            }
            basket.putString("type", type);

            Intent intent = new Intent(context, Second.class);

            intent.putExtras(basket);
            startActivity(intent);


        }

    }); 

    this.buttonO.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            tr.setVisibility(View.GONE);
        }

    }); 

    this.buttonR.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            tr.setVisibility(View.VISIBLE);
        }

    }); 


    this.btnDepart.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            showDialog(dDATE_DIALOG_ID);
        }

    }); 

    this.btnReturn.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            showDialog(rDATE_DIALOG_ID);
        }

    }); 


        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }


        private void updateDisplay() {
            txD.setText(
                    new StringBuilder()
                    // Month is 0 based so add 1
                    .append(dYear).append("-")
                    .append(dMonth + 1).append("-")
                    .append(dDay).append(" ")
                    );

            txR.setText(
                    new StringBuilder()
                    .append(rYear).append("-")
                    .append(rMonth + 1).append("-")
                    .append(rDay).append(" ")
                    );      

        }

        private DatePickerDialog.OnDateSetListener dDateSetListener =
                new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year,
                    int monthOfYear, int dayOfMonth) {
                dYear = year;
                dMonth = monthOfYear;
                dDay = dayOfMonth;
                updateDisplay();
            }
        };

        private DatePickerDialog.OnDateSetListener rDateSetListener =
                new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year,
                    int monthOfYear, int dayOfMonth) {
                rYear = year;
                rMonth = monthOfYear;
                rDay = dayOfMonth;
                updateDisplay();
            }
        };

        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case dDATE_DIALOG_ID:
                return new DatePickerDialog(this,
                        dDateSetListener,
                        dYear, dMonth, dDay);
            case rDATE_DIALOG_ID:
                return new DatePickerDialog(this,
                        rDateSetListener,
                        rYear, dMonth, rDay);

            }
            return null;
        }

    }

我的第二个活动是:

    public class Second extends Activity {

private TextView typeTv, departureTv,returnTv,departureDateTv,returnDateTv,noOfPassengersTv,tvReturnDateLbl;
private RadioButton buttonR = null;



public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);    

    typeTv = (TextView)findViewById(R.id.typeTv);
    departureTv = (TextView)findViewById(R.id.departureTv);
    returnTv = (TextView)findViewById(R.id.returnTv);
    departureDateTv= (TextView)findViewById(R.id.departureTv);
    returnDateTv= (TextView)findViewById(R.id.returneDateTv);
    noOfPassengersTv = (TextView)findViewById(R.id.noOfPassengersTv);
    buttonR = (RadioButton) findViewById(R.id.radioR);
    tvReturnDateLbl = (TextView)findViewById(R.id.tvReturnDateLbl);

    Intent i = getIntent();//MAking an intent called i
    Bundle basket = this.getIntent().getExtras();

    String tTv = basket.getString("type");
    String dTv = basket.getString("departureAir");
    String rTv = basket.getString("destinationAir");
    String dDTv = basket.getString("depDateTv");
    int nOPTv = basket.getInt("numOfPass");


    if(buttonR.isChecked()){
        String rDTv = basket.getString("returnDate");
    returnDateTv.setText(rDTv);


    }
    else{
        returnDateTv.setVisibility(View.GONE);
        tvReturnDateLbl.setVisibility(View.GONE);
    }

    typeTv.setText(tTv);
    departureTv.setText(dTv);
    returnTv.setText(rTv);
    departureDateTv.setText(dDTv);
    noOfPassengersTv.setText(""+nOPTv);


}

}

最后我的清单文件是:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
        <activity
        android:name=".Second"
        android:label="@string/app_name">
        </activity>
</application>

我的日志猫

04-25 19:04:11.912: E/AndroidRuntime(999): FATAL EXCEPTION: main
04-25 19:04:11.912: E/AndroidRuntime(999): java.lang.NullPointerException
04-25 19:04:11.912: E/AndroidRuntime(999):  at com.example.assignment2.MainActivity$5.onClick(MainActivity.java:121)
04-25 19:04:11.912: E/AndroidRuntime(999):  at android.view.View.performClick(View.java:4084)
04-25 19:04:11.912: E/AndroidRuntime(999):  at android.view.View$PerformClick.run(View.java:16966)
04-25 19:04:11.912: E/AndroidRuntime(999):  at android.os.Handler.handleCallback(Handler.java:615)
04-25 19:04:11.912: E/AndroidRuntime(999):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-25 19:04:11.912: E/AndroidRuntime(999):  at android.os.Looper.loop(Looper.java:137)
04-25 19:04:11.912: E/AndroidRuntime(999):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-25 19:04:11.912: E/AndroidRuntime(999):  at java.lang.reflect.Method.invokeNative(Native Method)
04-25 19:04:11.912: E/AndroidRuntime(999):  at java.lang.reflect.Method.invoke(Method.java:511)
04-25 19:04:11.912: E/AndroidRuntime(999):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-25 19:04:11.912: E/AndroidRuntime(999):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-25 19:04:11.912: E/AndroidRuntime(999):  at dalvik.system.NativeStart.main(Native Method)

我的新日志猫

04-25 19:38:14.443: E/Trace(1155): error opening trace file: No such file or directory (2)
04-25 19:38:15.282: D/gralloc_goldfish(1155): Emulator without GPU emulation detected.
04-25 19:38:19.251: D/AndroidRuntime(1155): Shutting down VM
04-25 19:38:19.251: W/dalvikvm(1155): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-25 19:38:19.281: E/AndroidRuntime(1155): FATAL EXCEPTION: main
04-25 19:38:19.281: E/AndroidRuntime(1155): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.assignment2/com.example.assignment2.Second}: java.lang.NullPointerException
04-25 19:38:19.281: E/AndroidRuntime(1155):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-25 19:38:19.281: E/AndroidRuntime(1155):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-25 19:38:19.281: E/AndroidRuntime(1155):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-25 19:38:19.281: E/AndroidRuntime(1155):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-25 19:38:19.281: E/AndroidRuntime(1155):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 19:38:19.281: E/AndroidRuntime(1155):     at android.os.Looper.loop(Looper.java:137)
04-25 19:38:19.281: E/AndroidRuntime(1155):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-25 19:38:19.281: E/AndroidRuntime(1155):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 19:38:19.281: E/AndroidRuntime(1155):     at java.lang.reflect.Method.invoke(Method.java:511)
04-25 19:38:19.281: E/AndroidRuntime(1155):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-25 19:38:19.281: E/AndroidRuntime(1155):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-25 19:38:19.281: E/AndroidRuntime(1155):     at dalvik.system.NativeStart.main(Native Method)
04-25 19:38:19.281: E/AndroidRuntime(1155): Caused by: java.lang.NullPointerException
04-25 19:38:19.281: E/AndroidRuntime(1155):     at com.example.assignment2.Second.onCreate(Second.java:46)
04-25 19:38:19.281: E/AndroidRuntime(1155):     at android.app.Activity.performCreate(Activity.java:5008)
04-25 19:38:19.281: E/AndroidRuntime(1155):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-25 19:38:19.281: E/AndroidRuntime(1155):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-25 19:38:19.281: E/AndroidRuntime(1155):     ... 11 more
04-25 19:38:19.441: D/dalvikvm(1155): GC_CONCURRENT freed 163K, 3% free 8303K/8519K, paused 79ms+112ms, total 315ms
4

1 回答 1

1

我相信basketnull。您需要在调用它之前对其进行put()初始化

Bundle basket = new Bundle();

了解如何阅读 logcat 对于调试您的工作至关重要。看上面的输出

04-25 19:04:11.912: E/AndroidRuntime(999): FATAL EXCEPTION: main
04-25 19:04:11.912: E/AndroidRuntime(999): java.lang.NullPointerException
04-25 19:04:11.912: E/AndroidRuntime(999):  at     
com.example.assignment2.MainActivity$5.onClick(MainActivity.java:121)

从这里我们看到NPE在第 121 行有一个MainAcitivty

*编辑**

Second改变你获得额外服务的方式。改变

Intent i = getIntent();//MAking an intent called i
Bundle basket = this.getIntent().getExtras();

Intent i = getIntent();//MAking an intent called i
Bundle basket = i.getBundleExtra("basket");

由于您将其Bundle放入Intent而不是每个extra本身,因此您可以这样做。如果你把每个都extra放在那里,那么你会得到一个Bundle你目前拥有的方式。

看到这个所以回答(第二个)它可能会更清楚。

在你的第一次Activity改变

intent.putExtras(basket);

intent.putExtra("basket", basket);
于 2013-04-25T19:29:53.383 回答