2017-06-29 8 views
0

[OK]を、私は、Androidに新たなんだ...Loacalisationのpasst - nullのオブジェクト参照

コンテキストはAlertDialog.BuilderウィッヒにdialogViewアフィリエイトはMainActivityに依存しています。

Dialogviewでは、私はボタンとEdittextを持っています。 Onclick、ボタンはユーザーの場所をキャッチし、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) 

はあなたの忍耐のために事前にありがとう...

答えて

0

そのuはあなたがeditTextのIDを見つけるために、dialogのビューを渡す必要があるので、ダイアログの中にあるeditTextを取得しようとしているため。

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

ありがとうございますアニル!それは仕事をする。私には恥ずかしい.. – Flo

0

Aボタンをクリックすると、実際にはeditTextが見つかりません。あなたは

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

へ 更新

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

@floねえ、あなたの問題が解決され? – Shivam

+0

Shivamありがとうございます。問題は以前のコメントによって解決されました。 dialogViewへの参照は、alertDialogへの参照が機能するエラーを返します。なぜ私は不思議です。私はたくさんのことを学びました... – Flo

0

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

を変更する必要が

この二人は見つかりません。チェックアウト

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(); 
関連する問題