我在此代码中遇到以下错误。
The method showDialog(int) is undefined for the type training details.
The constructor DatePickerDialog() is undefined.
The method getMenuInflator() is undefined.
如何解决这些错误?请指导。
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.app.Activity;
import android.widget.TextView;
import com.empover.odch.R;
public class TrainingDetails extends BaseFragment
{
View view;
private EditText nameEdt;
private Spinner trainingTypes;
private Spinner trainerProfile;
private EditText descriptionEdt;
private Calendar calendar;
private EditText startDt;
private EditText endDt;
private int year, month, day;
String data1[] = {"1", "2", "3"};
String data2[] = {"a", "b", "c"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view=inflater.inflate(R.layout.training_details, container,false);
nameEdt = (EditText) view.findViewById(R.id.name_edt);
trainingTypes = (Spinner) view.findViewById(R.id.trainingtypes_spn);
ArrayAdapter<String> adapter_state1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, data1);
adapter_state1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
trainingTypes.setAdapter(adapter_state1);
trainingTypes.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
int index = arg0.getSelectedItemPosition();
String selTrainingTypes = data1[index];
//Toast.makeText(arg0.getContext(), "Selected: " + selectedItem, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
trainerProfile = (Spinner) view.findViewById(R.id.trainerprofile_spn);
ArrayAdapter<String> adapter_state2 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, data2);
adapter_state2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
trainingTypes.setAdapter(adapter_state2);
trainingTypes.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
int index = arg0.getSelectedItemPosition();
String selTrainerProfiles = data2[index];
//Toast.makeText(arg0.getContext(), "Selected: " + selectedItem, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
descriptionEdt = (EditText) view.findViewById(R.id.description_edt);
startDt = (EditText) view.findViewById(R.id.startdate_edt);
endDt = (EditText) view.findViewById(R.id.enddate_edt);
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
showDate(year, month+1, day);
return view;
}
public void setDate(View view)
{
showDialog(999);
//Toast.makeText(getApplicationContext(), "Calender", Toast.LENGTH_SHORT).show();
}
protected Dialog onCreateDialog(int id)
{
if (id == 999)
{
return new DatePickerDialog(this, myDateListener, year, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3)
{
int x = arg2;
x = x+1;
showDate(arg1, x, arg3);
}
};
private void showDate(int year, int month, int day)
{
startDt.setText(new StringBuilder().append(day).append("/").append(month).append("/").append(year));
}
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:0)
而不是:
getMenuInflater().inflate(R.menu.main, menu);
使用:
getActivity().getMenuInflater().inflate(R.menu.main, menu);
而不是:
showDialog(999);
使用:
getActivity().showDialog(999);
而不是:
return new DatePickerDialog(this, myDateListener, year, month, day);
使用:
return new DatePickerDialog(getActivity(), myDateListener, year, month, day);