我在我的Android应用程序中使用Fragments,我有两个第一类是FirstClass.java,它扩展了FragmentActivity,SecondClass扩展了Fragments并在其布局文件中有按钮。 所以点击那个按钮,我打电话给#34; mylistener"像这样的功能
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4.64"
android:text="Button"
android:onClick="mylistener"
/>
和FirstClass.java
public class FirstClass extends FragmentActivity {
Button btnSet1;
int mHour = 15;
int mMinute = 15;
/** This handles the message send from TimePickerDialogFragment on setting Time */
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message m){
/** Creating a bundle object to pass currently set Time to the fragment */
Bundle b = m.getData();
/** Getting the Hour of day from bundle */
mHour = b.getInt("set_hour");
/** Getting the Minute of the hour from bundle */
mMinute = b.getInt("set_minute");
/** Displaying a short time message containing time set by Time picker dialog fragment */
Toast.makeText(getBaseContext(), b.getString("set_time"), Toast.LENGTH_SHORT).show();
btnSet1.getText().toString();
btnSet1.setText("set_time");
}
};
public void mylistener(View v){
/** Creating a bundle object to pass currently set time to the fragment */
Bundle b = new Bundle();
/** Adding currently set hour to bundle object */
b.putInt("set_hour", mHour);
/** Adding currently set minute to bundle object */
b.putInt("set_minute", mMinute);
/** Instantiating TimePickerDialogFragment */
TimePickerDialogFragment timePicker = new TimePickerDialogFragment(mHandler);
/** Setting the bundle object on timepicker fragment */
timePicker.setArguments(b);
/** Getting fragment manger for this activity */
FragmentManager fm = getSupportFragmentManager();
/** Starting a fragment transaction */
FragmentTransaction ft = fm.beginTransaction();
/** Adding the fragment object to the fragment transaction */
ft.add(timePicker, "time_picker");
/** Opening the TimePicker fragment */
ft.commit();
};
}
现在我可以在按钮的OnClick上获得定时器对话框,当试图在Button获取NullPointer异常时显示该时间格式时,我知道这个异常是因为FirstClass FragmentActivity中的按钮声明,这里没有,请帮我解决这个问题。
03-09 10:40:53.222: E/AndroidRuntime(23637): java.lang.NullPointerException
03-09 10:40:53.222: E/AndroidRuntime(23637): at com.bikepooling.FirstClass$1.handleMessage(FirstClass.java:63)
03-09 10:40:53.222: E/AndroidRuntime(23637): at android.os.Handler.dispatchMessage(Handler.java:110)
03-09 10:40:53.222: E/AndroidRuntime(23637): at android.os.Looper.loop(Looper.java:193)
03-09 10:40:53.222: E/AndroidRuntime(23637): at android.app.ActivityThread.main(ActivityThread.java:5292)
03-09 10:40:53.222: E/AndroidRuntime(23637): at java.lang.reflect.Method.invokeNative(Native Method)
03-09 10:40:53.222: E/AndroidRuntime(23637): at java.lang.reflect.Method.invoke(Method.java:515)
03-09 10:40:53.222: E/AndroidRuntime(23637): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
03-09 10:40:53.222: E/AndroidRuntime(23637): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
答案 0 :(得分:1)
不需要这种方法,
public void mylistener(View v)
使用这个
return new TimePickerDialog(Common.con, mTimeSetListener2, pHour, pMinute, false);
并初始化ex
按钮 btn = view.findviewbyid(...);
我希望工作正常,
答案 1 :(得分:0)
您尚未初始化btnSet1
尝试在onViewCreated
中执行此操作:
btnSet1 = view.findViewById(R.id.b1);