2014-03-13 61 views
0

好吧,我不知道這一點.. 這裏是我想做的事:如何在操作欄中的自定義對話框中設置自定義按鈕點擊偵聽器?

我有一個圖標在行動吧 - 當我點擊它膨脹與自定義佈局文件的對話框。此佈局有一個EditText和一個Button。整個事情的目的是寫電子郵件,然後通過單擊按鈕發送它。但是,當我按一下按鈕,我收到了Nullpoint exception ..

下面是一些代碼:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int itemId = item.getItemId(); 

    switch (itemId) { 
     case R.id.action_logout: 
      navigateToLogin(); 
      break; 
     case R.id.action_edit_friends: 
      Intent intent = new Intent(this, EditFriendsActivity.class); 
      startActivity(intent); 
      break; 
     case R.id.action_message: 
      Dialog messageDialog = new Dialog(this); 
      messageDialog.setContentView(R.layout.message_bar); 
      messageDialog.show(); 
      Button messageButton = (Button)messageDialog.findViewById(R.id.send_button); 
      messageButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        EditText messageText = (EditText) findViewById(R.id.edit_text_message); 
        if (TextUtils.isEmpty(messageText.getText())) { 
         Toast.makeText(MainActivity.this, "Enter a message first!", Toast.LENGTH_SHORT).show(); 
        } else { 
         String message = messageText.getText().toString(); 
         Intent recipientsIntent = new Intent(MainActivity.this, RecipientsActivity.class); 
         recipientsIntent.putExtra("Text", message); 
         startActivity(recipientsIntent); 
        } 
       } 
      }); 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 

的對話我的自定義佈局文件很簡單:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:id="@+id/structured_relative_interaction" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:gravity="bottom"> 

     <EditText 
      android:id="@+id/edit_text_message" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_margin="5dp" 
      android:layout_toLeftOf="@+id/send_button" 
      android:gravity="bottom" 
      android:hint="@string/send_message_hint" 
      android:inputType="textCapSentences|textMultiLine" 
      android:maxLines="15" 
      android:padding="10dp" /> 

     <Button 
      android:id="@+id/send_button" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:layout_alignBottom="@id/edit_text_message" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/ic_launcher"/> 
    </RelativeLayout> 
</RelativeLayout> 

那麼是什麼我做錯了?我無法解決這個問題,我已經看過其他的例子,並閱讀官方文檔的例子,但是......無賴。

回答

0

使用

EditText messageText = (EditText) messageDialog.findViewById(R.id.edit_text_message); 

,而不是

EditText messageText = (EditText) findViewById(R.id.edit_text_message);

+0

你的EditText是定製對話框的一部分,而不是主要佈局。所以你應該像調用button一樣調用自定義對話框的findviewbyid。 – Gaurav

+0

是的,就是這樣..我不知道我是如何錯過的:)花1小時看它:)謝謝! – lapadets

+0

很高興我能幫到你。祝你好運 :) – Gaurav

0

DialogsetContentView(View),所以你可以膨脹的觀點和傳遞到Dialog膨脹的看法。您也可以使用充氣的視圖來查找您需要的視圖,以獲取onClickListener的用途。我的猜測是,該視圖不是Dialog的視圖層次結構的一部分,除非它在屏幕上可見。

所以我會說

View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.message_bar, null); 
messageDialog.setContentView(view); 
View messageButton = view.findViewById(R.id.send_button); 
messageButton.setOnClickListener(...); 
相關問題