android ImageButton 点击

标签 android android-layout onclick

我有个小问题 在我的布局中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:weightSum="1">

    <ImageButton android:id="@+id/startLogo"
    android:layout_height="230dp"
    android:adjustViewBounds="true"
    android:background="@drawable/whatelsecomics"
    android:layout_width="230dp"
    android:layout_gravity="center_horizontal"/>

</LinearLayout>

在我的 Activity 中我有

public class WhatelsecomicsActivity extends Activity {

    private ImageButton whatelsecomics;

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main); // your layout file name

       whatelsecomics = (ImageButton) findViewById(R.id.startLogo); // your image button

    // click event on your button
    whatelsecomics.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent wecSeriesList = new Intent(WhatelsecomicsActivity.this, wecSeriesListActivity.class);
            //Start next activity
            WhatelsecomicsActivity.this.startActivity(wecSeriesList);
        }
    });
}

在我的日志中

09-23 21:31:05.081: ERROR/AndroidRuntime(3805): FATAL EXCEPTION: main
09-23 21:31:05.081: ERROR/AndroidRuntime(3805): java.lang.RuntimeException: Unable to start activity ComponentInfo{zepod.whatelsecomics/zepod.whatelsecomics.WhatelsecomicsActivity}: java.lang.NullPointerException
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at android.os.Looper.loop(Looper.java:123)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at java.lang.reflect.Method.invokeNative(Native Method)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at java.lang.reflect.Method.invoke(Method.java:507)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at dalvik.system.NativeStart.main(Native Method)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805): Caused by: java.lang.NullPointerException
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at zepod.whatelsecomics.WhatelsecomicsActivity.onCreate(WhatelsecomicsActivity.java:21)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-23 21:31:05.081: ERROR/AndroidRuntime(3805):     ... 11 more

谁能告诉我为什么?

最佳答案

如果您使用的是 Eclipse,请单击 Project->Clean...(如果您没有自动执行此操作,请重新构建它)。此类问题的常见原因是 R 文件未正确生成,因此重建项目以再次生成它。

关于android ImageButton 点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7535104/

相关文章:

android - 如何在 android VideoView 中通过 LAN 传输视频

java - 为什么我们不能使用新关键字创建 Activity ?

java - 如何在分水岭的分割中添加坐标?

Android Layout 不会横向垂直滚动

java - 在适配器和 fragment 中获取正确的上下文

javascript - 使用 Javascript 从单击的元素获取属性

android - "javax.net.ssl.SSLHandshakeException: Handshake failed"即使在添加自定义 TrustManager 和证书固定之后

android - LinearLayout findViewById 问题 - 崩溃

javascript - 在javascript中让按钮显示文本

python - 单击一个图可在绘图中打开另一个图