TimePickerDialogFragment:“无法解析方法 .add(片段,字符串)
TimePickerDialogFragment: "Cannot resolve method .add(fragment, string)
首先,通过详尽的搜索,我发现大多数人遇到此错误是由于片段导入不当 class 而不是使用支持库。
另一方面,我正在学习位于 here.
的教程
我的代码与他的代码基本相同,但我将在下面附上我的 MainActivity 和 TimePickerDialogFragment。我正在关注的教程日期为 2012 年 8 月,这可能是我 运行 参与其中的原因,我可以交换教程,但我真的很喜欢更正此问题的调试体验及其原因正在发生。
代码出现错误//add the fragment object to the fragment transaction
ft.add(timePicker, "time_picker");
这是我的 MainActivity:
package com.stembo.android.timepickerdialogdemo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
int mHour = 15;
int mMinute = 15;
//This handles the message send from TimePickerDialogFragment (TPDF)
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message m){
//create a bundle object to pass the current set time
Bundle b = m.getData();
//getting the hour of day from bundle
mHour = b.getInt("set_hour");
//getting the minute from bundle
mMinute = b.getInt("set_minute");
//displaying a short time message containing the time set by the TPDF
Toast.makeText(getBaseContext(), b.getString("set_time"),Toast.LENGTH_LONG).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//click event handler for button
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
//create a bundle object to pass current time set
Bundle b = new Bundle();
//add currently set hour to bundle object
b.putInt("set_hour", mHour);
//add currently set minute to bundle object
b.putInt("set_minute", mMinute);
//instantiate TPDF
TimePickerDialogFragment timePicker = new TimePickerDialogFragment(mHandler);
//setting the bundle object on timepicker fragment
timePicker.setArguments(b);
//get a fragment manager for this activity
FragmentManager fm = getSupportFragmentManager();
//start a fragment transaction
FragmentTransaction ft = fm.beginTransaction();
//add the fragment object to the fragment transaction
ft.add(timePicker, "time_picker");
//opening the timepicker fragment
ft.commit();
}
};
//get an instance of Set button
Button btnSet = (Button)findViewById(R.id.btnSet);
btnSet.setOnClickListener(listener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我认为不需要它,但这是 TimePickerDialogFragment
package com.stembo.android.timepickerdialogdemo;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TimePicker;
/**
* Created by myxom on 16/02/16.
*/
public class TimePickerDialogFragment extends DialogFragment {
Handler mHandler ;
int mHour;
int mMinute;
//public TimePickerDialogFragment(){}; //solves a lint issue
public TimePickerDialogFragment(Handler h){ //another lint issue
/** Getting the reference to the message handler instantiated in MainActivity class */
mHandler = h;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
/** Creating a bundle object to pass currently set time to the fragment */
Bundle b = getArguments();
/** Getting the hour of day from bundle */
mHour = b.getInt("set_hour");
/** Getting the minute of hour from bundle */
mMinute = b.getInt("set_minute");
TimePickerDialog.OnTimeSetListener listener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
/** 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);
/** Adding Current time in a string to bundle object */
b.putString("set_time", "Set Time : " + Integer.toString(mHour) + " : " + Integer.toString(mMinute));
/** Creating an instance of Message */
Message m = new Message();
/** Setting bundle object on the message object m */
m.setData(b);
/** Message m is sending using the message handler instantiated in MainActivity class */
mHandler.sendMessage(m);
}
};
/** Opening the TimePickerDialog window */
return new TimePickerDialog(getActivity(), listener, mHour, mMinute, false);
}
}
问题出在你身上TimePickerDialogFragment.java
将
import android.app.DialogFragment
替换为
import android.support.v4.app.DialogFragment;
首先,通过详尽的搜索,我发现大多数人遇到此错误是由于片段导入不当 class 而不是使用支持库。
另一方面,我正在学习位于 here.
的教程我的代码与他的代码基本相同,但我将在下面附上我的 MainActivity 和 TimePickerDialogFragment。我正在关注的教程日期为 2012 年 8 月,这可能是我 运行 参与其中的原因,我可以交换教程,但我真的很喜欢更正此问题的调试体验及其原因正在发生。
代码出现错误//add the fragment object to the fragment transaction
ft.add(timePicker, "time_picker");
这是我的 MainActivity:
package com.stembo.android.timepickerdialogdemo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
int mHour = 15;
int mMinute = 15;
//This handles the message send from TimePickerDialogFragment (TPDF)
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message m){
//create a bundle object to pass the current set time
Bundle b = m.getData();
//getting the hour of day from bundle
mHour = b.getInt("set_hour");
//getting the minute from bundle
mMinute = b.getInt("set_minute");
//displaying a short time message containing the time set by the TPDF
Toast.makeText(getBaseContext(), b.getString("set_time"),Toast.LENGTH_LONG).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//click event handler for button
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
//create a bundle object to pass current time set
Bundle b = new Bundle();
//add currently set hour to bundle object
b.putInt("set_hour", mHour);
//add currently set minute to bundle object
b.putInt("set_minute", mMinute);
//instantiate TPDF
TimePickerDialogFragment timePicker = new TimePickerDialogFragment(mHandler);
//setting the bundle object on timepicker fragment
timePicker.setArguments(b);
//get a fragment manager for this activity
FragmentManager fm = getSupportFragmentManager();
//start a fragment transaction
FragmentTransaction ft = fm.beginTransaction();
//add the fragment object to the fragment transaction
ft.add(timePicker, "time_picker");
//opening the timepicker fragment
ft.commit();
}
};
//get an instance of Set button
Button btnSet = (Button)findViewById(R.id.btnSet);
btnSet.setOnClickListener(listener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我认为不需要它,但这是 TimePickerDialogFragment
package com.stembo.android.timepickerdialogdemo;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TimePicker;
/**
* Created by myxom on 16/02/16.
*/
public class TimePickerDialogFragment extends DialogFragment {
Handler mHandler ;
int mHour;
int mMinute;
//public TimePickerDialogFragment(){}; //solves a lint issue
public TimePickerDialogFragment(Handler h){ //another lint issue
/** Getting the reference to the message handler instantiated in MainActivity class */
mHandler = h;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
/** Creating a bundle object to pass currently set time to the fragment */
Bundle b = getArguments();
/** Getting the hour of day from bundle */
mHour = b.getInt("set_hour");
/** Getting the minute of hour from bundle */
mMinute = b.getInt("set_minute");
TimePickerDialog.OnTimeSetListener listener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
/** 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);
/** Adding Current time in a string to bundle object */
b.putString("set_time", "Set Time : " + Integer.toString(mHour) + " : " + Integer.toString(mMinute));
/** Creating an instance of Message */
Message m = new Message();
/** Setting bundle object on the message object m */
m.setData(b);
/** Message m is sending using the message handler instantiated in MainActivity class */
mHandler.sendMessage(m);
}
};
/** Opening the TimePickerDialog window */
return new TimePickerDialog(getActivity(), listener, mHour, mMinute, false);
}
}
问题出在你身上TimePickerDialogFragment.java
将import android.app.DialogFragment
替换为import android.support.v4.app.DialogFragment;