以下是我写的代码:
public class ProblemsDescription extends Fragment {
static final int REQUEST_IMAGE_CAPTURE = 1;
private Report report = null;
private EditText text = null;
private Button button = null, bottoneph = null, bottonelc = null;
private FragmentTransaction transaction;
private LocationManager lm;
private LocationListener ls;
private TextView text2=null;
private double[] locazione= new double[2];
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_problems_description_maps, container, false);
}
@Override
public void onActivityCreated(Bundle save) {
super.onActivityCreated(save);
}
@Override
public void onResume() {
super.onResume();
text2=(TextView) getActivity().findViewById(R.id.textView);
button = (Button) getActivity().findViewById(R.id.button3);
bottoneph = (Button) getActivity().findViewById(R.id.button4);
bottonelc = (Button) getActivity().findViewById(R.id.button5);
text = (EditText) getActivity().findViewById(R.id.editText);
lm = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE);
ls = new LocationListener() {
@Override
public void onLocationChanged(Location location){
text2.append(location.getLatitude() + " " + location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Intent intent= new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}; if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.INTERNET
},10);
return;
}else
{
configurebutton();
}
bottone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
report.setReport(text.getText().toString());
}
});
bottoneph.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[]permission, int[]grantResults)
{
switch(requestCode)
{
case 10:
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
configurebutton();
return;
}
}
private void configurebutton() {
bottonelc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lm.requestLocationUpdates("gps", 5000, 0, ls);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap= (Bitmap) data.getExtras().get("data");
report.setPhoto(bitmap);
}
public void setSegnalazione(Segnalazione segnalazione)
{
this.report=report;
}
}
发布于 2018-07-23 13:55:32
使一个私有的voidinit()函数并在其中初始化变量,就能清楚的知道这是用于诸如findviewbyid之类的东西。
https://stackoverflow.com/questions/-100005626
复制相似问题