I'm new to Android...
The context is, a dialogView affiliate to an AlertDialog.Builder which depends on the MainActivity.
In the Dialogview, I have a Button and an Edittext. Onclick, the button should catch the User location and pass it to the EditText View.
The result?
When I click my button, the application crashes.
Here's the Javacode:
public void editProfile(){
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from(context);
View dialogView = inflater.inflate(R.layout.update_profile_dialog, null);
Spinner spinner = (Spinner) dialogView.findViewById(R.id.update_profile_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.title_selector_update_profile, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
dialogBuilder.setView(dialogView);
alertDialog = dialogBuilder.create();
alertDialog.show();
Button ok = (Button) alertDialog.findViewById(R.id.update_profile_ok);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = ((EditText) alertDialog.findViewById(R.id.profile_name)).getText().toString();
//TODO use profession
final String profession = ((EditText) alertDialog.findViewById(R.id.profile_profession)).getText().toString();
String city = ((EditText) alertDialog.findViewById(R.id.profile_city)).getText().toString();
Address address = googleHelper.getGeo(city);
ImageView img = (ImageView)alertDialog.findViewById(R.id.update_profile_photo);
BitmapDrawable bitmapDrawable = ((BitmapDrawable) img.getDrawable());
final String encodedImage;
if (bitmapDrawable != null) {
Bitmap bitmap = bitmapDrawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
final byte[] imageInByte = stream.toByteArray();
encodedImage = Base64.encodeToString(imageInByte, Base64.DEFAULT);
}
else{
encodedImage = "";
}
rpcHelper.name = name;
rpcHelper.city = city;
rpcHelper.latitude = address.getLatitude();
rpcHelper.longitude = address.getLongitude();
rpcHelper.updateUser(new AsyncResponseListener() {
@Override
public void onResponse(JSONArray response) throws JSONException {
if (encodedImage != ""){
rpcHelper.addPhotoToUser(encodedImage, new AsyncResponseListener() {
@Override
public void onResponse(JSONArray response) throws JSONException {
}
@Override
public void onResponse() {
}
@Override
public void onResponse(Bitmap bm) {
}
@Override
public void onPreExecute() {
}
});
}
rpcHelper.addKeywordToUser(profession, new AsyncResponseListener() {
@Override
public void onResponse(JSONArray response) throws JSONException {
}
@Override
public void onResponse() {
}
@Override
public void onResponse(Bitmap bm) {
}
@Override
public void onPreExecute() {
}
});
}
@Override
public void onResponse() {
}
@Override
public void onResponse(Bitmap bm) {
}
@Override
public void onPreExecute() {
}
});
alertDialog.dismiss();
}
});
Button no = (Button)alertDialog.findViewById(R.id.update_profile_no);
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
ImageButton localisation = (ImageButton) dialogView.findViewById(R.id.btn_localisation);
localisation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Location loc = googleHelper.getLastKnownLocation();
if (loc != null){
Address addr = googleHelper.getGeoFromCoord(loc);
if (addr == null){
return;
}
EditText userlocation = (EditText) findViewById(R.id.profile_city);
userlocation.setText(addr.getLocality());
}}
});
}}
Here the XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:background="@drawable/stroke_rounded_corners_grey"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp">
<ImageButton
android:id="@+id/btn_localisation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:background="@android:color/transparent"
android:cropToPadding="false"
android:scaleType="centerCrop"
android:tint="@android:color/white"
app:srcCompat="@drawable/custom_button_localisation_filter"
tools:ignore="ContentDescription,NestedWeights" />
<EditText
android:id="@+id/profile_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:ems="10"
android:gravity="center"
android:hint="@string/hint_city_update_profile"
android:imeOptions="actionDone"
android:inputType="textCapWords|textAutoCorrect"
android:selectAllOnFocus="true"
android:textAlignment="center" />
</LinearLayout>
and finally the error report:
06-29 12:57:09.081 1706-1706/com.example.philipp.us E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.philipp.us, PID: 1706
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at com.example.philipp.us.MainActivity$15.onClick(MainActivity.java:1196)
at android.view.View.performClick(View.java)
at android.view.View$PerformClick.run(View.java)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
Thank you in advance for your patience...
Its because u are try to get editText
which is inside dialog so you need pass view of dialog
to find Id of an editText
.
Update
EditText userlocation = (EditText) findViewById(R.id.profile_city);
userlocation.setText(addr.getLocality());
To
EditText userlocation = (EditText) alertDialog.findViewById(R.id.profile_city);
userlocation.setText(addr.getLocality());