我想通過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
是另一個項目中使用庫項目的活動。 期待積極快速的迴應。 謝謝。
我們可以看到堆棧跟蹤嗎? –
問題解決了,這是一個程序錯誤。莫辛幫我修理它。 –