![](/img/trans.png)
[英]Android Fragment .setAdapter giving null pointer Exception onClick
[英]Android ImageButton OnClick giving null pointer exception
我從一個菜單選項中調用一個彈出對話框,上面寫着登錄:然后將 Twitter 和 Google 徽標顯示為 2 個圖像按鈕。
AlertDialog.Builder alertadd = new AlertDialog.Builder(
SummaryActivity.this);
LayoutInflater factory = LayoutInflater.from(SummaryActivity.this);
final View view = factory.inflate(R.layout.social_network_selection, null);
alertadd.setView(view);
它膨脹包含 2 個圖像按鈕的視圖。
當我添加代碼時
googleButton2 = (ImageButton)findViewById(R.id.googleButton);
twitterButton2 = (ImageButton)findViewById(R.id.twitterButton);
一切正常,但是當我添加
googleButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MyAndroidAppActivity.this,
"googleButton2 is clicked!", Toast.LENGTH_SHORT).show();
}
});
它因空指針而崩潰。 不是當我點擊按鈕時,而是第二次我調用包含 ImageButtons 的 AlertDialog
如何將偵聽器添加到 AlertDialog 膨脹視圖上的圖像按鈕?
邏輯貓
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at android.support.v7.internal.view.SupportMenuInflater$InflatedOnMenuItemClickListener.onMenuItemClick(SupportMenuInflater.java:259)
at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
at android.support.v7.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191)
問題出在這些行中,
googleButton2 = (ImageButton)findViewById(R.id.googleButton);
twitterButton2 = (ImageButton)findViewById(R.id.twitterButton);
findViewById(int) 方法在上下文布局中找不到 id 因此它返回空值
解決方案
您應該必須使用 findViewById 方法來查找由布局上下文膨脹的視圖。
這是您的任務所需的解決方案,
AlertDialog.Builder alertadd = new AlertDialog.Builder(
SummaryActivity.this);
LayoutInflater factory = LayoutInflater.from(SummaryActivity.this);
final View view = factory.inflate(R.layout.social_network_selection, null);
alertadd.setView(view);
googleButton2 = (ImageButton) view.findViewById(R.id.googleButton);
twitterButton2 = (ImageButton) view.findViewById(R.id.twitterButton);
googleButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MyAndroidAppActivity.this,
"googleButton2 is clicked!", Toast.LENGTH_SHORT).show();
}
});
使用 View 視圖的名稱調用 imagebutton,您的 onClick 使用 getBaseContext()
AlertDialog.Builder alertadd = new AlertDialog.Builder(
SummaryActivity.this);
LayoutInflater factory = LayoutInflater.from(SummaryActivity.this);
final View view = factory.inflate(R.layout.social_network_selection, null);
ImageButton googleButton2 = (ImageButton)view.findViewById(R.id.googleButton);
ImageButton twitterButton2 = (ImageButton)view.findViewById(R.id.twitterButton);
googleButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getBaseContext(),
"googleButton2 is clicked!", Toast.LENGTH_SHORT).show();
}
});
alertadd.setView(view);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.