我在一个片段内有一个自定义对话框,问题是一个无法编程的按钮,每次我编程按钮和我运行程序我得到nullpointerexception错误,请有人帮助请,这里它的代码:< / p>
调用 customdialog 的类:
public class PruebaUsuario extends Fragment implements OnMapReadyCallback {
private SupportMapFragment sMapFragment;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private Button mPedir;
private Button mConfirmar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//conseguir usuario actual
// mUsuarioActual = ParseUser.getCurrentUser();
//preparar google maps
final View x = inflater.inflate(R.layout.prueba_usuario, container, false);
mPedir=(Button)x.findViewById(R.id.pedirBtn);
sMapFragment = SupportMapFragment.newInstance();
sMapFragment.getMapAsync(this);
android.support.v4.app.FragmentManager fm = getFragmentManager();
android.support.v4.app.FragmentManager sFm = getChildFragmentManager();
sFm.beginTransaction().add(R.id.mapa, sMapFragment).commit();
mPedir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialogo = new Dialog(getContext());
dialogo.setTitle("Confirmacion");
dialogo.setContentView(R.layout.confirmacion_pedido);
dialogo.show();
mConfirmar=(Button) x.findViewById(R.id.confirmarBtn2);
mConfirmar.setEnabled(false);
}
});
return x;
}
@Override
public void onMapReady(GoogleMap googleMap) {
if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
googleMap.setMyLocationEnabled(true);
LocationManager mng = (LocationManager) getContext().getSystemService(getContext().LOCATION_SERVICE);
Location location1 = mng.getLastKnownLocation(mng.getBestProvider(new Criteria(), false));
double lat = location1.getLatitude();
double lon = location1.getLongitude();
LatLng ubicacion = new LatLng(lat, lon);
float zoomLevel = 14;
googleMap.addMarker(new MarkerOptions().position(ubicacion).title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ubicacion, zoomLevel));
}
}
来自班级的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pedir Taxi"
android:id="@+id/pedirBtn" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mapa"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
来自自定义对话框的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Por favor confirme datos direccion"
android:id="@+id/textView5" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="94dp"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="198dp"
android:layout_height="144dp"
android:layout_weight="0.38">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="150dp"
android:layout_height="148dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText3" />
</LinearLayout>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText4"
android:layout_gravity="center_horizontal"
android:hint="Ej: independendcia y avellaneda, Frente a carlos v"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confirmar Pedido"
android:id="@+id/confirmarBtn2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="79dp" />
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:0)
更改代码,如下面的对话框代码所示,然后检查:
Dialog dialogo = new Dialog(getActivity()); //change getContext to getActivity
dialogo.setTitle("Confirmacion");
dialogo.setContentView(R.layout.confirmacion_pedido);
dialogo.show();
mConfirmar=(Button) dialogo.findViewById(R.id.confirmarBtn2); //instead of x use dialogo
mConfirmar.setEnabled(false);