2014-02-18 15 views
0

我想通過XML在我的圖書館項目中製作一個自定義對話框。圖書館項目中來自XML的空指針異常處理按鈕

下面是XML文件:

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

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginLeft="25dp" > 

     <Button 
      android:id="@+id/likeitb" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/likeitd" /> 

     <Button 
      android:id="@+id/dontlikeitb" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/dontliked" /> 

     <Button 
      android:id="@+id/notnowb" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/notnowd" /> 
    </LinearLayout> 

</RelativeLayout> 

在簡單的Java類,我創建對話框像這樣:

final Dialog feed_back_dialog = new Dialog(mAct); 

feed_back_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0)); 
feed_back_dialog.setCancelable(true); 
feed_back_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 

feed_back_dialog.setContentView(R.layout.ratedialog); 


Button LikeIt = (Button) mAct.findViewById(R.id.likeitb); 
Button DontLikeIt = (Button) mAct.findViewById(R.id.dontlikeitb); 
Button NotNow = (Button) mAct.findViewById(R.id.notnowb); 


LikeIt.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    String url = "market://details?id="+mAct.getPackageName(); 
    Intent i = new Intent(Intent.ACTION_VIEW); 
    i.setData(Uri.parse(url)); 
    mAct.startActivity(i); 
    mAct.finish(); 
    } 
}); 

如果我不設置點擊LikeIt按鈕的監聽器顯示的對話框,但是當我嘗試在其上設置點擊偵聽器時。它給了我一個空指針異常。 我無法找到解決方案。當我嘗試訪問它時,LikeIt按鈕爲空。 我沒有任何相同的名稱資源。請幫忙。

參考mAct是另一個項目中使用庫項目的活動。 期待積極快速的迴應。 謝謝。

+0

我們可以看到堆棧跟蹤嗎? –

+0

問題解決了,這是一個程序錯誤。莫辛幫我修理它。 –

回答

1

只要改變在這幾行代碼mActfeed_back_dialog
作爲feed_back_dialog是您當前佈局

Button LikeIt = (Button) mAct.findViewById(R.id.likeitb); Button DontLikeIt = (Button) mAct.findViewById(R.id.dontlikeitb); Button NotNow = (Button) mAct.findViewById(R.id.notnowb);

它必須是像這樣

Button LikeIt = (Button) feed_back_dialog.findViewById(R.id.likeitb); Button DontLikeIt = (Button) feed_back_dialog.findViewById(R.id.dontlikeitb); Button NotNow = (Button) feed_back_dialog.findViewById(R.id.notnowb);

1

更改爲

Button LikeIt = (Button) feed_back_dialog.findViewById(R.id.likeitb); 

假設對話框佈局具有id爲一個按鈕likeitb

類似地,對於其它視圖

Button DontLikeIt = (Button) feed_back_dialog.findViewById(R.id.dontlikeitb); 
Button NotNow = (Button) feed_back_dialog.findViewById(R.id.notnowb); 

findViewById查找在提到與ID的圖目前充氣佈局。因此,您需要使用對話框對象來初始化視圖,因爲您可以將對話框的佈局內容設置爲對話框。視圖屬於對話框

1

你可以做的是你可以像這樣膨脹佈局。然後使用充氣的視圖,你可以得到Button控件。 [我希望MACT活動對象]

LayoutInflater li = LayoutInflater.from(mAct); 
    View inflatedView = li.inflate(R.layout.ratedialog, null, false); 

    //button initialization 
    Button LikeIt = (Button) inflatedView .findViewById(R.id.likeitb); 
    Button DontLikeIt = (Button) inflatedView .findViewById(R.id.dontlikeitb); 
    Button NotNow = (Button) inflatedView .findViewById(R.id.notnowb);