-3

我的程序包含两个活动。我通过方法 showevent 调用了第二个活动。但是发生了错误,空指针异常。这是为什么?我的程序包含两个活动。我通过方法 showevent 调用了第二个活动。但是发生错误,空指针异常。这是为什么

第一活动.java

public class FirstActivity extends FragmentActivity implements OnItemSelectedListener {
    /** Called when the activity is first created. */
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    public final static String EXTRA_MESSAGE1 = "com.example.myfirstapp.MESSAGE";


    classdbOpenHelper eventsData;
     TextView userSelection;
     Button okButton;
     Button addButton;

     Button change_date_but;
     TextView date;
     TextView show;
     EditText edittext; 
      public static final int Date_dialog_id = 1;
      private int mYear;
      private int mMonth;
      private int mDay;

    private static final String[] items={"Yalahanka","Rajaji nagar","Sivaji Nagar","Koramangala","RT Nagar", "Banashankari","Yashwanthpura","Hebbal"};



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
         okButton = (Button)findViewById(R.id.button2);
         addButton = (Button)findViewById(R.id.button3);

         change_date_but = (Button)findViewById(R.id.button1);
         date = (TextView)findViewById(R.id.textView2);//KEY_DATE
         userSelection=(TextView)findViewById(R.id.textView1);//KEY_DESC
         edittext=(EditText)findViewById(R.id.editText1);//KEY_EVENT


         show=(TextView)findViewById(R.id.textView5);

         Spinner my_spin=(Spinner)findViewById(R.id.spinner1);
        my_spin.setOnItemSelectedListener(this);
        ArrayAdapter aa=new ArrayAdapter(this, android.R.layout.simple_spinner_item,items);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        my_spin.setAdapter(aa);

        okButton.setOnClickListener(new clicker());

        eventsData = new classdbOpenHelper(this);

        change_date_but.setOnClickListener(new View.OnClickListener() {



                   @Override
                   public void onClick(View v) {
                     DatePickerDialog DPD = new DatePickerDialog(
                   FirstActivity.this, mDateSetListener, mYear, mMonth,mDay);
                                DPD.show();
               }
                  });
        final Calendar c = Calendar.getInstance();
              mYear = c.get(Calendar.YEAR);
              mMonth = c.get(Calendar.MONTH);
              mDay = c.get(Calendar.DAY_OF_MONTH);

              updateDisplay();
    }
     @Override
         @Deprecated

         protected void onPrepareDialog(int id, Dialog dialog) {
          // TODO Auto-generated method stub
       super.onPrepareDialog(id, dialog);

          ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);

         }

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

       public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
           mYear = year;
           mMonth = monthOfYear;
       mDay = dayOfMonth;
           updateDisplay();
          }
         };

         private void updateDisplay() {
       // TODO Auto-generated method stub
          date.setText(new StringBuilder()
            // Month is 0 based so add 1
         .append(mMonth + 1).append("-").append(mDay).append("-")
            .append(mYear));
         }







    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3) {
        userSelection.setText(items[pos]);
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
        userSelection.setText("");
    }



    class clicker implements Button.OnClickListener {
        public void onClick(View v) {
            String datevalue = date.getText().toString();
            String Userselectvalue = userSelection.getText().toString();
            String Userevent = edittext.getText().toString();
            SQLiteDatabase  db = eventsData.getWritableDatabase();

            ContentValues cv = new ContentValues();
            cv.put(classdbOpenHelper.KEY_DESC, Userselectvalue);
            cv.put(classdbOpenHelper.KEY_EVENT, Userevent);
            cv.put(classdbOpenHelper.KEY_DATE,datevalue);
            db.insert(classdbOpenHelper.DATABASE_TABLE, null, cv);
            db.close();
            }

         }

    public void ShowEvent(View view){
        Intent intent = new Intent(this, Getclicker.class);
         date = (TextView)findViewById(R.id.textView2);
        String datevalue = date.getText().toString();
        userSelection=(TextView)findViewById(R.id.textView1);
        String Userselectvalue = userSelection.getText().toString();

        intent.putExtra(EXTRA_MESSAGE, datevalue);
        intent.putExtra(EXTRA_MESSAGE1, Userselectvalue);

        startActivity(intent);
    }
}

Getclicker.java

package example.events1;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;

public class Getclicker  extends ListActivity {
    Intent intent = getIntent();
    String datevalue = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE);
    String Userselectvalue = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE1);
    classdbOpenHelper eventsData1;
    Cursor cursor1;
     ListView listView ; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);                       /*String datevalue = date.getText().toString();
        String Userselectvalue = userSelection.getText().toString();

        String showevent = eventsData.getContact(datevalue,Userselectvalue);
        show.setText(showevent);*/

         cursor1 = eventsData1.getContact(datevalue,Userselectvalue);
         String[] fromColumns = {classdbOpenHelper.KEY_EVENT};
         int[] toViews = {R.id.event};
         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.events, cursor1, fromColumns, toViews,0 );
          listView = (ListView) findViewById(android.R.id.list); 
         listView.setAdapter(adapter);
        }

    public void onDestroy() {
        eventsData1.close();
      }

}

日志猫:

08-07 10:00:58.396: E/AndroidRuntime(889): FATAL EXCEPTION: main
08-07 10:00:58.396: E/AndroidRuntime(889): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{example.events1/example.events1.Getclicker}: java.lang.NullPointerException
08-07 10:00:58.396: E/AndroidRuntime(889):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
08-07 10:00:58.396: E/AndroidRuntime(889):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-07 10:00:58.396: E/AndroidRuntime(889):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-07 10:00:58.396: E/AndroidRuntime(889):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-07 10:00:58.396: E/AndroidRuntime(889):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 10:00:58.396: E/AndroidRuntime(889):  at android.os.Looper.loop(Looper.java:137)
08-07 10:00:58.396: E/AndroidRuntime(889):  at android.app.ActivityThread.main(ActivityThread.java:5041)
08-07 10:00:58.396: E/AndroidRuntime(889):  at java.lang.reflect.Method.invokeNative(Native Method)
08-07 10:00:58.396: E/AndroidRuntime(889):  at java.lang.reflect.Method.invoke(Method.java:511)
08-07 10:00:58.396: E/AndroidRuntime(889):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-07 10:00:58.396: E/AndroidRuntime(889):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-07 10:00:58.396: E/AndroidRuntime(889):  at dalvik.system.NativeStart.main(Native Method)
08-07 10:00:58.396: E/AndroidRuntime(889): Caused by: java.lang.NullPointerException
08-07 10:00:58.396: E/AndroidRuntime(889):  at example.events1.Getclicker.<init>(Getclicker.java:12)
08-07 10:00:58.396: E/AndroidRuntime(889):  at java.lang.Class.newInstanceImpl(Native Method)
08-07 10:00:58.396: E/AndroidRuntime(889):  at java.lang.Class.newInstance(Class.java:1319)
08-07 10:00:58.396: E/AndroidRuntime(889):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
08-07 10:00:58.396: E/AndroidRuntime(889):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
08-07 10:00:58.396: E/AndroidRuntime(889):  ... 11 more
4

1 回答 1

5

移动这些:

Intent intent = getIntent();
String datevalue = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE);
String Userselectvalue = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE1);

进入onCreate()方法。我猜是因为 Intent 还没有出现。

希望这可以帮助。

于 2013-08-07T10:17:14.117 回答