2013-02-25 73 views
8

我是Java新手,嘗試使用EditText創建活動。在創建時,它會加載當前日期,並在用戶選擇EditText時顯示DatePicker。一旦用戶選擇日期,我需要將結果放入EditText。但是,我目前收到以下錯誤:錯誤:「無法對類型爲Activity的非靜態方法findViewById(int)進行靜態引用」

Cannot make a static reference to the non-static method findViewById(int) from the type Activity

我知道我無法對非靜態方法進行靜態引用。我試圖刪除所有靜態引用,但是這給了我其他的錯誤。我的代碼如下。

你能幫我解釋一下我如何使這項工作的一些示例代碼?錯誤是在我嘗試將結果放入EditText的時候。

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Get Current Date and assign to EditText Begin 
    Calendar c = Calendar.getInstance(); 
    System.out.println("Current time => " + c.getTime()); 

    SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.US); 
    String formattedDate = df.format(c.getTime()); 

    EditText ShowDate = (EditText) findViewById(R.id.editTextToShowDate); 
    ShowDate.setText(formattedDate); 
    //Get Current Date and assign to EditText End 
} 

//Date Picker Start 
@TargetApi(Build.VERSION_CODES.HONEYCOMB) public void showDatePickerDialog(View v) { 
    DialogFragment newFragment = new DatePickerFragment(); 
    newFragment.show(getFragmentManager(), "datePicker"); 
}  

@TargetApi(Build.VERSION_CODES.HONEYCOMB) public static class DatePickerFragment extends DialogFragment 
implements DatePickerDialog.OnDateSetListener { 

@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
// Use the current date as the default date in the picker 
final Calendar c = Calendar.getInstance(); 
int year = c.get(Calendar.YEAR); 
int month = c.get(Calendar.MONTH); 
int day = c.get(Calendar.DAY_OF_MONTH); 

// Create a new instance of DatePickerDialog and return it 
return new DatePickerDialog(getActivity(), this, year, month, day); 
} 

public void onDateSet(DatePicker view, int year, int month, int day) { 
// Do something with the date chosen by the user 
    String sToDate = createStringFromDateElements(year, month, day); 
    EditText ShowDate = (EditText) findViewById(R.id.editTextToShowDate); //I get the error here 
    ShowDate.setText(sToDate); 
} 

private String createStringFromDateElements(int year, int month, int day) { 
    // TODO Auto-generated method stub 
    return null; 
} 
} 
//Date Picker End 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 
} 
+3

當你是新來的java,請花一些時間瞭解java然後步入android。 Android是java的超集。 – NewUser 2013-02-25 17:22:38

+0

試試我的答案,它會工作 – Pragnani 2013-02-25 17:27:56

+0

你只需要讓你的DatePickerFragment類非靜態,所以它包含在MainActivity實例範圍內。 – njzk2 2013-02-25 17:34:37

回答

14

編輯:

EditText ShowDate = (EditText)getActivity(). findViewById(R.id.editTextToShowDate); 

這將工作...

+0

這應該工作...... – 2013-02-25 17:32:23

+0

不,請閱讀問題。這不是關於轉換,而是關於訪問方法的範圍。 – njzk2 2013-02-25 17:33:52

+0

@ njzk2這不是關於鑄造。它會工作....在給予downvote之前嘗試一次...我從活動級別獲取edittext,所以不會有任何問題 – Pragnani 2013-02-25 17:37:43

0

裏面Activity類,我已經EDITTEXT靜態和

EditText ShowDate = (EditText)this.findViewById(R.id.editTextToShowDate); 

它爲我工作。

欲瞭解更多here

相關問題