|
|
|
Android - Error เมื่อกดปุ่มแล้วโชว์ DatePicker Popup ใน Fragment |
|
|
|
|
|
|
|
ผมกำลังทำปุ่มกดแล้วให้มันโชว์ DatePicker
นี่เป็น Code ของผมครับ
Code (Android-Java)
package com.example.trainning;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
public class View2Activity extends Fragment{
private Button test;
Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener Mydate = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
}
};
public View2Activity() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_view2,
container, false);
test = (Button)getView().findViewById(R.id.testDate);
test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(getActivity(),
Mydate,
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
return rootView;
}
}
แต่ว่าเมื่อรันโปรแกรมไป มันเกิด error has stopped ขึ้น ตาม log นี้ครับ
Code
11-27 00:10:52.598: E/AndroidRuntime(1864): FATAL EXCEPTION: main
11-27 00:10:52.598: E/AndroidRuntime(1864): Process: com.example.trainning, PID: 1864
11-27 00:10:52.598: E/AndroidRuntime(1864): java.lang.NullPointerException
11-27 00:10:52.598: E/AndroidRuntime(1864): at com.example.trainning.View2Activity.onCreateView(View2Activity.java:37)
11-27 00:10:52.598: E/AndroidRuntime(1864): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
11-27 00:10:52.598: E/AndroidRuntime(1864): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
11-27 00:10:52.598: E/AndroidRuntime(1864): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
11-27 00:10:52.598: E/AndroidRuntime(1864): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-27 00:10:52.598: E/AndroidRuntime(1864): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
11-27 00:10:52.598: E/AndroidRuntime(1864): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
11-27 00:10:52.598: E/AndroidRuntime(1864): at android.os.Handler.handleCallback(Handler.java:733)
11-27 00:10:52.598: E/AndroidRuntime(1864): at android.os.Handler.dispatchMessage(Handler.java:95)
11-27 00:10:52.598: E/AndroidRuntime(1864): at android.os.Looper.loop(Looper.java:137)
11-27 00:10:52.598: E/AndroidRuntime(1864): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-27 00:10:52.598: E/AndroidRuntime(1864): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 00:10:52.598: E/AndroidRuntime(1864): at java.lang.reflect.Method.invoke(Method.java:515)
11-27 00:10:52.598: E/AndroidRuntime(1864): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-27 00:10:52.598: E/AndroidRuntime(1864): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-27 00:10:52.598: E/AndroidRuntime(1864): at dalvik.system.NativeStart.main(Native Method)
ผมไม่รู้ว่าควรจะแก้ยังไง เพราะผมก้ดู Code ตัวอย่างมาจาก internet Code ก็ไม่ error แต่ว่าทำงานไม่ได้
Tag : Mobile
|
|
|
|
|
|
Date :
2013-11-27 12:15:56 |
By :
lotte |
View :
1318 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|