2014-09-20 98 views
1

我有一個對話框,它被我的MainActivity調用。在這個對話框裏面有一個自定義按鈕。當我試圖讓該按鈕在我的MainActivity與Android:在對話框中查找按鈕

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

總是空(所以後來我得到一個空指針異常)。我認爲是與對話框中的按鈕有關...

該對話框擴展DialogFragment。它在運行時被稱爲MainActivity內部,作爲對另一個Button的反應。如果您需要了解更多信息,請告訴我

下面是一些代碼:

MainActivity(部分,因爲它有200多個線)

public class MainActivity extends Activity implements UseDateInParentActivityInterface 
{ 
    FragmentManager fragmentManager = getFragmentManager(); 

    CreateDialogFragment createDialogFragment; 
    DialogFragment datePicker; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     homeFragment = new HomeFragment(); 

     fragmentManager.beginTransaction().add(R.id.mainFrame, homeFragment).commit(); 
     findViewById(R.id.bottomButton_home).setBackgroundResource(R.drawable.ic_home); 
    } 

    public void openCreate(View view) 
    { 
     createDialogFragment = new CreateDialogFragment(); 
     createDialogFragment.show(fragmentManager, "TEST"); 

     updateSelectedButton(3); 
    } 


    public void pickDate(View v) 
    { 
     datePicker = new DatePickerFragment(); 
     datePicker.show(getFragmentManager(), String.format(getResources().getString(R.string.pickDate))); 
    } 

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

     Button createDateButton = (Button) findViewById(R.id.create_date_button); 
     if (createDateButton != null) 
     { 
      createDateButton.setText(date); 
     } 
     else 
     { 
      System.out.println("bloeder Button ist gleich null"); 
     } 
    } 
} 

這是DialogFragment的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.File

<?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" 
    > 

    <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> 

約翰

+0

我更新了我的答案。 – 2014-09-20 20:44:49

回答

1

在你CreateDialogFragment,修改onCreateDialog()

@Override 
public Dialog onCreateDialog(Bundle savedInstance) 
{ 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater layoutInflater = getActivity().getLayoutInflater(); 
    View root = layoutInflater.inflate(R.layout.dialog_create, null); 

    builder.setView(root).setMessage(R.string.create); 
    // ... 

    Button createDateBtn = (Button) root.findViewById(R.id.create_date_button); 
    // set click listener here ... 

    return builder.create(); 
} 

的問題是,該按鈕在Dialog,而不是Activity的觀層次。如果您從您的活動中調用findViewById(),您將始終得到空:層次結構存在於兩個不同的窗口中。

這段代碼獲取對話框中按鈕的句柄。它從對話框的根視圖(不是活動)中調用findViewById()並檢索按鈕。然後,您可以將其保存到一個參考中供以後使用,或直接在那裏設置一個點擊監聽器。在這個監聽器中,你可以編寫自己的邏輯,例如調用一個你的活動的方法。

+1

它的作品,令人驚歎!!!!!你真的幫我解決了爲什麼它不起作用。非常感謝!! – JRsz 2014-09-21 12:58:09

0

試試這個: 按鈕createDateButton =(按鈕)rootView.findViewById(R.id.create_date_button);

這應該是工作。

+0

你對rooView究竟意味着什麼? – JRsz 2014-09-20 17:40:14

+0

查看rootView = layoutInflater.inflate(R.layout.dialog_create,null); builder.setView(rootView); – dannyroa 2014-09-20 17:43:03

+0

好吧,我還沒有那麼多Android的,但我必須把什麼東西,我現在很困惑 – JRsz 2014-09-20 17:53:14