Android自定義TopBar中給ImageButton設置自定義點擊監聽器,當點擊ImageButton時在listener處出現NullPointerException。
會出錯的地方我在後面寫了//TODO 。
請問為什麼會出現listener沒有被實例化的情況?
還有為什麼在MainActivity中使用findViewById實例化TopBar也會出現不能實例化控件的情況?
IDE : Android Studio ;
Compile Sdk Version : API 23 ;
Build Tool Version : 23.0.1 ;
Min Sdk Version : API 15 ;
Target Sdk Version : API 19 ;
部分代碼:
#MainActivity.java
public class MainActivity extends AppCompatActivity {
private TopBar topBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// topBar = (TopBar) findViewById(R.id.topBar); //TODO 這樣無法實例化topBar ???
topBar = new TopBar(getApplicationContext(), null);
if (topBar == null) {
Toast.makeText(MainActivity.this, "NULL", Toast.LENGTH_SHORT).show();
}
topBar.setOnTopBarClickListener(new TopBar.OnClickListener() {
@Override
public void onLeftButtonClick() {
Toast.makeText(MainActivity.this, "Left", Toast.LENGTH_SHORT).show();
}
@Override
public void onRightButtonClick() {
Toast.makeText(MainActivity.this, "Right", Toast.LENGTH_SHORT).show();
}
});
}
}
#TopBar.java
public class TopBar extends RelativeLayout {
private ImageButton ibtLeft;
private ImageButton ibtRight;
private TextView tvTitle;
private OnClickListener listener;
public interface OnClickListener {
void onLeftButtonClick();
void onRightButtonClick();
}
public void setOnTopBarClickListener(OnClickListener listener) {
this.listener = listener;
}
public TopBar(Context context, AttributeSet attrs) {
super(context);
LayoutInflater.from(context).inflate(R.layout.layout_top_bar, this);
ibtLeft = (ImageButton) findViewById(R.id.ibt_left);
ibtRight = (ImageButton) findViewById(R.id.ibt_right);
tvTitle = (TextView) findViewById(R.id.tv_title);
ibtLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "leftButton", Toast.LENGTH_SHORT).show(); //Toast可以顯示
// listener.onLeftButtonClick(); //TODO NullPointerException 為什麼沒有實例化listener?
}
});
ibtRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onRightButtonClick(); //TODO NullPointerException
}
});
}
}
#logcat輸出
10-14 13:42:14.442 1366-1366/com.vder.puzzlegame E/AndroidRuntime: FATAL EXCEPTION: main
10-14 13:42:14.442 1366-1366/com.vder.puzzlegame E/AndroidRuntime: java.lang.NullPointerException
10-14 13:42:14.442 1366-1366/com.vder.puzzlegame E/AndroidRuntime: at com.vder.puzzlegame.ui.TopBar$2.onClick(TopBar.java:55)
10-14 13:42:14.442 1366-1366/com.vder.puzzlegame E/AndroidRuntime: at android.view.View.performClick(View.java:4204)
10-14 13:42:14.442 1366-1366/com.vder.puzzlegame E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17355)
10-14 13:42:14.442 1366-1366/com.vder.puzzlegame E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725)
10-14 13:42:14.442 1366-1366/com.vder.puzzlegame E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
10-14 13:42:14.442 1366-1366/com.vder.puzzlegame E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
10-14 13:42:14.442 1366-1366/com.vder.puzzlegame E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
10-14 13:42:14.442 1366-1366/com.vder.puzzlegame E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-14 13:42:14.442 1366-1366/com.vder.puzzlegame E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
10-14 13:42:14.442 1366-1366/com.vder.puzzlegame E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-14 13:42:14.442 1366-1366/com.vder.puzzlegame E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-14 13:42:14.442 1366-1366/com.vder.puzzlegame E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
構造方法不對,像這樣,把三個全上才可以在Xml文件中使用
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}