2013-08-03 74 views
-1

我的程序包含一個名爲日期的按鈕來顯示日曆視圖。但應用程序結束時點擊它。請給我一個解決方案。我的程序包含一個名爲date的按鈕來顯示日曆視圖。但應用程序結束時點擊它。請給我一個解決方案。應用程序結束,而點擊日期選擇按鈕

FirstActivity.java

package example.showevent1; 

import java.text.SimpleDateFormat; 
import java.util.Calendar; 

import android.os.Bundle; 
import android.app.DatePickerDialog.OnDateSetListener; 
import android.content.ContentValues; 
import android.database.sqlite.SQLiteDatabase; 
import android.support.v4.app.*; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.Spinner; 
import android.widget.TextView; 
import android.widget.AdapterView.OnItemSelectedListener; 

public class FirstActivity extends FragmentActivity implements OnItemSelectedListener, OnDateSetListener { 
    /** Called when the activity is first created. */ 
    private static final String DATE_FORMAT = "yyyy-MM-dd"; 
    classdbOpenHelper eventsData; 
    TextView userSelection; 
    Button okButton; 
    Button date1; 
    public EditText date; 
    private Calendar mCalendar; 
    private static final String[] items = { "Yalahanka", "Rajai 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); 
     date1 = (Button) findViewById(R.id.button1); 

     userSelection = (TextView) findViewById(R.id.textView1); 
     Spinner my_spin = (Spinner) findViewById(R.id.spinner1);// data1 
     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); 
     date1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       showDatePicker(); 
      } 
     }); 

    } 

    static final String YEAR = "year"; 
    static final String MONTH = "month"; 
    static final String DAY = "day"; 
    static final String HOUR = "hour"; 
    static final String MINS = "mins"; 
    static final String CALENDAR = "calendar"; 

    private void showDatePicker() { 
     Object ft = getFragmentManager().beginTransaction(); 
     DatePickerDialogFragment newFragment = new DatePickerDialogFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(YEAR, mCalendar.get(Calendar.YEAR)); 
     args.putInt(MONTH, mCalendar.get(Calendar.MONTH)); 
     args.putInt(DAY, mCalendar.get(Calendar.DAY_OF_MONTH)); 
     newFragment.setArguments(args); 
     newFragment.show((FragmentManager) ft, "datePicker"); 
    } 

    @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 = date1.getText().toString(); 
      String Userselectvalue = userSelection.getText().toString(); 
      SQLiteDatabase db = eventsData.getWritableDatabase(); 

      ContentValues cv = new ContentValues(); 
      // cv.put(classdbOpenHelper.KEY_COUNTED, 
      // metersave.getText().toString()); 
      cv.put(classdbOpenHelper.KEY_DESC, Userselectvalue); 
      cv.put(classdbOpenHelper.KEY_DATE, datevalue); 
      db.insert(classdbOpenHelper.DATABASE_TABLE, null, cv); 
     } 

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

    } 

    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
     mCalendar.set(Calendar.YEAR, year); 
     mCalendar.set(Calendar.MONTH, monthOfYear); 
     mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
     updateButtons(); 
    } 

    private void updateButtons() { 

     // Set the date button text 
     SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); 
     String dateForButton = dateFormat.format(mCalendar.getTime()); 
     date1.setText(dateForButton); 
    } 
} 

DatePickerDialogFragment.java

import android.app.DatePickerDialog; 
import android.app.DatePickerDialog.OnDateSetListener; 
import android.app.Dialog; 
import android.os.Bundle; 
import android.support.v4.app.*; 
public class DatePickerDialogFragment extends DialogFragment { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     Bundle args = getArguments(); 
     Fragment editFragment = getFragmentManager().findFragmentByTag(FirstActivity.ACCESSIBILITY_SERVICE); 
     OnDateSetListener listener = (OnDateSetListener) editFragment; 
     return new DatePickerDialog(getActivity(), listener, 
     args.getInt(FirstActivity.YEAR), 
     args.getInt(FirstActivity.MONTH), 
     args.getInt(FirstActivity.DAY)); 
    } 
} 
+1

你得到一個NullPointerException,因爲你的變量mCalendar是null。 –

+0

使用logcat來找出你的問題是什麼。如果你已經使用過它,你會知道你在哪一行得到異常。 – erdomester

+0

您必須創建日曆的實例。 mCalendar = new ....在你的onCreate方法中。 –

回答

1

你得到一個空指針,因爲你從來沒有真正初始化日曆對象:

mCalendar 
相關問題