2014-09-20 42 views
0

我的Android应用程序中有一个对话框,其中包含一个按钮旁边的其他内容。现在我想改变那个Button的文本,但是我得到一个NullPointer异常。Android:在对话框中更改按钮的文本

该按钮打开一个新的对话框(日期选择器),它给MainActivity一个日期(作为字符串),然后我想要用于按钮。很明显,我可以声明按钮并定义他,但不能互动。在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 openCreate(View view) { 
    createDialogFragment = new CreateDialogFragment(); 
    createDialogFragment.show(fragmentManager, "TEST"); 
    updateSelectedButton(3); 
} 

这导致问题:

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

我找到了一个工作解决方案。我必须将Button的引用保存在对话框(.java)文件的变量中。然后我做了一个getter并且可以得到Button。实际上很简单,所以这意味着MainActivity和Dialog必须有两个不同的生命周期。我还添加了这两条线来获得按钮的参考:

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

而且除了工作:

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

如果任何人有同样的问题,这dicription是不够的,只是评论,我会用更多的细节填充它。

约翰

-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因为没有描述你的答案如何解决op问题。如果您添加说明,我会加倍努力。 – 2014-09-20 12:33:27

+0

我真的不明白你的答案。我想改变positiveButton和NegativeButton,并且按钮在runtume中被定义为... – JRsz 2014-09-20 13:49:40

0

你必须改变

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

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

create_date_buttonDialog的XML,所以你需要一个参考t o得到Button

+0

不起作用。 CreateDialogFragment扩展了DialogFragment,因此它没有findViewById之类的方法。我认为findViewById全局工作,这就是它的目的?!这是编译器错误:错误:(108,68)错误:找不到符号方法findViewById(int) – JRsz 2014-09-20 13:47:32

+0

这可能工作。我保存了对象中的引用,这是我访问它的方式。但谢谢你的回答:) – JRsz 2014-09-22 07:15:30

相关问题