2014-09-20 2 views
0

내 Android 앱에 Button 옆에있는 대화 상자가 있습니다. 이제 해당 단추의 텍스트를 변경하고 싶지만 NullPointer 예외가 발생합니다.Android : 대화 상자에서 버튼의 텍스트 변경

이 버튼은 MainActivity에 (Button으로) 데이트를 사용하고자하는 날짜를 제공하는 새로운 Dialog (Datepicker)를 엽니 다. 분명히 버튼을 선언하고 정의 할 수는 있지만 상호 작용은 할 수 없습니다. try-statement 내에서 (문장이 있는지는 확실하지 않음) 첫 번째 행은 작동하지만 두 번째 행에 주석을 달지 않고 호출하면 충돌이 발생하거나 예외가 발생합니다. 나중에 try 부분을 추가했습니다. 나는 이것이 충분히 구체적이기를 바란다. 여기

버튼의 코드이다에서 MainActivity하고 대화 버튼 거주지 :

대화 (된 .java)

public class CreateDialogFragment extends DialogFragment { 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstance) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     LayoutInflater layoutInflater = getActivity().getLayoutInflater(); 

     builder.setView(layoutInflater.inflate(R.layout.dialog_create, null)) 
       .setMessage(R.string.create) 
       .setPositiveButton(R.string.create, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 

        } 
       }) 
       .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 

        } 
       }); 

     return builder.create(); 
    } 
} 

대화 (.XML)

<?xml version="1.0" encoding = "utf-8"?> 
<RelativeLayout 
    xmlns:android = "http://schemas.android.com/apk/res/android" 
    xmlns:tools = "http://schemas.android.com/tools" 
    android:id = "@+id/dialog_create" 
    android:layout_width = "match_parent" 
    android:layout_height = "match_parent" 
    android:background="@color/create_bg_1" 
    tools:context = ".CreateDialogFragment"> 

    <EditText 
     android:id = "@+id/dialog_create_name" 
     android:layout_width = "match_parent" 
     android:layout_height = "@dimen/textFieldHight" 
     android:text = "@string/create_name" 
     android:background="@color/white" 
     android:layout_marginTop="4dp" 
     android:layout_marginBottom="4dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="4dp" /> 

    <Button 
     android:layout_width = "wrap_content" 
     android:layout_height = "@dimen/textFieldHight" 
     android:ems = "6" 
     android:text = "@string/date" 
     android:background="@color/white" 
     android:id = "@+id/create_date_button" 
     android:onClick = "pickDate" 
     android:layout_below = "@id/dialog_create_name" /> 

</RelativeLayout> 

MainActivity -> 문제를 일으키는 메서드 및 대화 상자를 호출하는 메서드 (.java - 부분적으로)

이문제의 원인 : 당신은 변경해야

public void dateForWhatever(int year, int month, int day) { 
    DateFormatter dateFormatter = new DateFormatter(); 
    String date = dateFormatter.toStringDE(year, month, day); 

    try { 
     Button createDateButton = (Button) findViewById(R.id.create_date_button); 
     createDateButton.setText(date); 
    } catch (Exception cannotFindButton) { 
     System.out.println("Geh kacken\n" + year + " " + month + " " + day); 
    } 
} 

답변

0

실용적인 해결책을 찾았습니다. Dialog (.java) 파일의 변수에 Button 참조를 저장해야했습니다. 그런 다음 방금 Getter를 만들었고 Button을 얻을 수있었습니다. 아주 간단합니다. 실제로 MainActivity와 Dialog에는 2 개의 수명주기가 있어야합니다. 사람이 같은 문제를 가지고 있으며,이 dicription이 충분하지 않으면 단지 주석

builder.setView(rootView).set [...] ; 

을 나는 것 :

View rootView = layoutInflater.inflate(R.layout.dialog_create, null); 
createDateButton = (Button) rootView.findViewById(R.id.create_date_button); 

및 추가

가 작동 : 나는 또한 버튼의 참조를 얻기 위해이 두 줄을 추가 그것을 더 자세하게 기입하십시오.

-1
 make constructure in CreateDialogFragment class 
    String text1,text2; 
    public CreateDialogFragment(String btn1Text,String btn2Text 
    { 
    text1=btn1Text; 
    text2=btn2Text; 
    } 


    in this code change 

     builder.setView(layoutInflater.inflate(R.layout.dialog_create, null)) 
      .setMessage(R.string.create) 
      .setPositiveButton(text1, new DialogInterface.OnClickListener() 
      { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) 
       { 

       } 
      }) 
      .setNegativeButton(text2, new DialogInterface.OnClickListener() 
      { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) 
       { 

       } 
      }); 
+0

-1 당신의 대답은 연산 문제를 해결하는 방법에 대한 설명이 없습니다 때문입니다. 당신이 설명을 추가하면 나는 그것을 upvote 것입니다. –

+0

나는 당신의 대답을 정말로 이해하지 못합니다. 그리고 positiveButton과 NegativeButton을 변경하지 않고 Button이 runtume에서 명확하게 chaanged ... – JRsz

0

Button createDateButton = (Button) findViewById(R.id.create_date_button); 

Button createDateButton = (Button) createDialogFragment.getView().findViewById(R.id.create_date_button); 

귀하의 create_date_button 당신이 필요로하므로 Dialog의 XML에

에 기준 t o 얻으려는 사람 Button

+0

이 작동하지 않습니다. CreateDialogFragment는 DialogFragment를 확장하기 때문에 findViewById와 같은 메소드가 없습니다. findViewById가 전 세계적으로 작동한다고 생각했습니다. 그것이 바로 그 이유입니다. 다음은 컴파일러 오류입니다. 오류 : (108, 68) 오류 : 기호 메서드를 찾을 수 없습니다. findViewById (int) – JRsz

+0

이것은 작동 할 수 있습니다. 개체에 참조를 저장 했으므로 액세스하는 방법입니다. 하지만 당신의 대답을 주셔서 감사합니다 :) – JRsz

관련 문제