这里,我正在创建一个用户配置文件片段,该片段由名字,姓氏,性别,出生日期,地址1,地址2和手机号码组成。在我的程序中,单击img_click时需要显示一个弹出窗口。我遇到无法解决相机和图库的错误,这些错误是菜单项的ID。我是android编程的新手。帮我解决这个问题。 这是我的程序:
package com.example.aparna.listfragment.fragments;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.DrawableRes;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.aparna.listfragment.R;
import com.example.aparna.listfragment.database.DbCreate;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class ProfileFragment extends Fragment {
public TextView tv_firstname,tv_lastname,tv_dob,tv_setdate,tv_address1,tv_address2,tv_mob;
public EditText edittext_firstname,edittext_lastname,edittext_address1,edittext_address2,edittext_mob;
public ImageView img_same,img_calender,img_profile,img_click;
public Button btn_save;
public RadioGroup radioGroup;
public RadioButton radiobtn_male,radiobtn_female;
public DbCreate create;
public String firstname,lastname,saddress1,saddress2,mobile_no,gender,date_of_birth;
public static final int REQUEST_CODE=1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view= inflater.inflate(R.layout.fragment_profile, container, false);
tv_firstname=(TextView)view.findViewById(R.id.tv_firstname);
edittext_firstname=(EditText) view.findViewById(R.id.edittext_firstname);
tv_lastname=(TextView)view.findViewById(R.id.tv_lastname);
edittext_lastname=(EditText) view.findViewById(R.id.edittext_lastname);
tv_address1=(TextView)view.findViewById(R.id.tv_address1);
edittext_address1=(EditText)view.findViewById(R.id.edittext_address1);
tv_address2=(TextView)view.findViewById(R.id.tv_address2);
edittext_address2=(EditText)view.findViewById(R.id.edittext_address2);
radioGroup=(RadioGroup)view.findViewById(R.id.radioGroup);
btn_save=(Button)view.findViewById(R.id.btn_save);
tv_dob=(TextView)view.findViewById(R.id.tv_dob);
tv_setdate=(TextView) view.findViewById(R.id.tv_setdate);
img_same=view.findViewById(R.id.img_same);
img_calender=view.findViewById(R.id.img_calender);
tv_dob=(TextView)view.findViewById(R.id.tv_mob);
edittext_mob=(EditText) view.findViewById(R.id.edittext_mob);
img_profile=view.findViewById(R.id.img_profile);
img_click=view.findViewById(R.id.img_click);
//date picker
img_calender.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
Calendar newCalendar = Calendar.getInstance();
DatePickerDialog datePicker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year,monthOfYear,dayOfMonth);
tv_setdate.setText(dateFormatter.format(newDate.getTime()));
date_of_birth=tv_setdate.getText().toString();
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
datePicker.show();
}
});
//if address is same
img_same.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edittext_address2.setText(edittext_address1.getText().toString());
saddress2=saddress1;
edittext_address2.setSelection(edittext_address2.getText().length());//moving cursor to the end of edit text
}
});
//radio button
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb= (RadioButton)view.findViewById(checkedId);
gender=rb.getText().toString();
Log.e("gender","=="+gender );
}
});
// image upload
img_click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final PopupMenu popup=new PopupMenu(getActivity(),img_click);
popup.getMenuInflater().inflate(R.menu.popup_menu,popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()){
case R.id.camera:
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,REQUEST_CODE);
case R.id.gallery:
}
return false;
}
});
popup.show();
}
});
// save button pressed
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
create=new DbCreate(getActivity());
firstname=edittext_firstname.getText().toString();
lastname=edittext_lastname.getText().toString();
saddress1=edittext_address1.getText().toString();
saddress2=edittext_address2.getText().toString();
mobile_no=edittext_mob.getText().toString();
Log.e("mobile no ","=="+mobile_no);
//-------------------------------------VALIDATION---------------------------------------------------------------
if(firstname.isEmpty() && lastname.isEmpty() && gender==null && date_of_birth==null && saddress1==null && saddress2==null && mobile_no.isEmpty()){
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("ALERT");
alert.setIcon(R.drawable.ic_warning_black_24dp);
alert.setMessage("All fields are empty.Please fill the fields");
alert.setPositiveButton("ok",null);
alert.show();
}
if(firstname.isEmpty()) {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("ALERT");
alert.setIcon(R.drawable.ic_warning_black_24dp);
alert.setMessage("Please fill the firstname field");
alert.setPositiveButton("ok",null);
alert.show();
}
else if(!firstname.matches("[a-zA-Z ]+") ) {
edittext_firstname.setError("ENTER ONLY ALPHABETICAL CHARACTER");
}
else if(lastname.isEmpty()){
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("ALERT");
alert.setIcon(R.drawable.ic_warning_black_24dp);
alert.setMessage("Please fill the lastname field");
alert.setPositiveButton("ok",null);
alert.show();
}
else if( !lastname.matches("[a-zA-Z]+")) {
edittext_lastname.setError("ENTER ONLY ALPHABETICAL CHARACTER");
}
else if(gender==null){
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("ALERT");
alert.setIcon(R.drawable.ic_warning_black_24dp);
alert.setMessage("Please fill the gender field");
alert.setPositiveButton("ok",null);
alert.show();
}
else if(date_of_birth==null){
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("ALERT");
alert.setIcon(R.drawable.ic_warning_black_24dp);
alert.setMessage("Please fill the dob field");
alert.setPositiveButton("ok",null);
alert.show();
}
else if( saddress1.isEmpty()){
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("ALERT");
alert.setIcon(R.drawable.ic_warning_black_24dp);
alert.setMessage("Please fill the address1 field");
alert.setPositiveButton("ok",null);
alert.show();
}
else if(saddress2.isEmpty()){
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("ALERT");
alert.setIcon(R.drawable.ic_warning_black_24dp);
alert.setMessage("Please fill the address2 field");
alert.setPositiveButton("ok",null);
alert.show();
}
else if(mobile_no.isEmpty()){
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("ALERT");
alert.setIcon(R.drawable.ic_warning_black_24dp);
alert.setMessage("Please fill the mobile no field");
alert.setPositiveButton("ok",null);
alert.show();
}
else if( mobile_no.length()!=10){
edittext_mob.setError("ENTER VALID NUMBER");
}
else{
create.insertUser(firstname,lastname,gender,date_of_birth,saddress1,saddress2,mobile_no);
Toast.makeText(getActivity(), "data saved", Toast.LENGTH_SHORT).show();
}
Log.e("no. of","data in the database=="+create.userCount() );
}
});
return view;
}
}
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="camera" android:title="FROM CAMERA"/>
<item android:id="gallery" android:title="FROM GALLERY"/>
</menu>