错误是:
无法解析方法' show(android.app.FragmentTrasanction,java.lang.String)' on line dialog.show(ft," DatePicker");
为什么我收到此错误?我已附加此问题所需的两个文件。帮我解决这个错误。主要课程如下:
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
public class OwnerInformation extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
public EditText text_owner_name,text_owner_address;
public EditText text_owner_mob,text_owner_adhaar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_owner_information);
text_owner_name = (EditText) findViewById(R.id.owner_name);
text_owner_address = (EditText) findViewById(R.id.owner_address);
text_owner_mob = (EditText) findViewById(R.id.mobile_no);
text_owner_adhaar = (EditText) findViewById(R.id.adhaar_no);
Spinner spinner_blood_group = (Spinner) findViewById(R.id.blood_group);
Spinner spinner_gender = (Spinner) findViewById(R.id.gender);
ArrayAdapter adapter5 = ArrayAdapter.createFromResource(OwnerInformation.this,R.array.blood_group_array,android.R.layout.simple_spinner_item);
ArrayAdapter adapter6 = ArrayAdapter.createFromResource(OwnerInformation.this,R.array.gender_array,android.R.layout.simple_spinner_item);
adapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_blood_group.setAdapter(adapter5);
spinner_gender.setAdapter(adapter6);
spinner_blood_group.setOnItemSelectedListener(this);
spinner_gender.setOnItemSelectedListener(this);
}
public void onStart(){
super.onStart();
EditText datePicker2=(EditText) findViewById(R.id.datePicker2);
datePicker2.setOnFocusChangeListener(new View.OnFocusChangeListener(){
public void onFocusChange(View view, boolean hasfocus){
if(hasfocus){
DateDialog2 dialog=new DateDialog2(view);
FragmentTransaction ft =getFragmentManager().beginTransaction();
dialog.show(ft, "DatePicker");
}
}
});
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String sSelected=adapterView.getItemAtPosition(i).toString();
//Toast.makeText(getContext(),sSelected,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
日期对话类如下: -
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import java.util.Calendar;
@SuppressLint("ValidFragment")
public class DateDialog2 extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
EditText datePicker2;
public DateDialog2(View view){
datePicker2=(EditText)view;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
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) {
//show to the selected date in the text box
String date2=day+"-"+(month+1)+"-"+year;
datePicker2.setText(date2);
}
}
答案 0 :(得分:0)
您需要将FragmentManager
传递给DialogFragment的show()
方法,而不是FragmentTransaction
。
替换
FragmentTransaction ft =getFragmentManager().beginTransaction();
dialog.show(ft, "DatePicker");
使用
dialog.show(getFragmentManager(), "DatePicker");