对EditText的Loacalisation passt - null对象引用

时间:2017-06-29 11:09:39

标签: android object null

好的,我是Android新手......

上下文是一个alertView隶属于AlertDialog.Builder,它取决于MainActivity。

在Dialogview中,我有一个Button和一个Edittext。 单击该按钮,该按钮应该捕获用户位置并将其粘贴到EditText视图。

结果?

单击我的按钮时,应用程序崩溃。

继承人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());
            }}
        });
}}

这里是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>

最后是错误报告:

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)

提前感谢您的耐心......

3 个答案:

答案 0 :(得分:0)

实际上在内部点击按钮,它无法找到editText

你需要改变

EditText userlocation = (EditText) findViewById(R.id.profile_city);

EditText userlocation = (EditText) dialogView.findViewById(R.id.profile_city);

答案 1 :(得分:0)

这两个都找不到。结帐

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();

答案 2 :(得分:0)

因为您试图获取editText内部对话框,因此您需要传递dialog的视图才能找到editText的ID。 更新

EditText userlocation = (EditText) findViewById(R.id.profile_city);
userlocation.setText(addr.getLocality());

EditText userlocation = (EditText) alertDialog.findViewById(R.id.profile_city);
userlocation.setText(addr.getLocality());