Android Eclipse FC java.lang.NullPointerException?

时间:2012-12-03 16:58:38

标签: android eclipse

我是Android的新手。我按照教程创建了一个使用PHP和MySQL的登录屏幕...它在几天前工作..现在当我尝试运行应用程序时,它给了我一个强制关闭错误和一个Java.lang.NullPointerException ..我猜这意味着什么东西是空的,不应该是?有人可以帮帮我吗?

这是我的LogCat:

12-03 11:48:06.089: D/AndroidRuntime(13538): Shutting down VM
12-03 11:48:06.099: W/dalvikvm(13538): threadid=1: thread exiting with uncaught exception (group=0x2aac8560)
12-03 11:48:06.099: E/AndroidRuntime(13538): FATAL EXCEPTION: main
12-03 11:48:06.099: E/AndroidRuntime(13538): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.groupone.the.health.effect/com.groupone.the.health.effect.AndroidDashboardDesignActivity}: java.lang.NullPointerException
12-03 11:48:06.099: E/AndroidRuntime(13538):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
12-03 11:48:06.099: E/AndroidRuntime(13538):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
12-03 11:48:06.099: E/AndroidRuntime(13538):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
12-03 11:48:06.099: E/AndroidRuntime(13538):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
12-03 11:48:06.099: E/AndroidRuntime(13538):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 11:48:06.099: E/AndroidRuntime(13538):    at android.os.Looper.loop(Looper.java:123)
12-03 11:48:06.099: E/AndroidRuntime(13538):    at android.app.ActivityThread.main(ActivityThread.java:3701)
12-03 11:48:06.099: E/AndroidRuntime(13538):    at java.lang.reflect.Method.invokeNative(Native Method)
12-03 11:48:06.099: E/AndroidRuntime(13538):    at java.lang.reflect.Method.invoke(Method.java:507)
12-03 11:48:06.099: E/AndroidRuntime(13538):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
12-03 11:48:06.099: E/AndroidRuntime(13538):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
12-03 11:48:06.099: E/AndroidRuntime(13538):    at dalvik.system.NativeStart.main(Native Method)
12-03 11:48:06.099: E/AndroidRuntime(13538): Caused by: java.lang.NullPointerException
12-03 11:48:06.099: E/AndroidRuntime(13538):    at com.groupone.the.health.effect.AndroidDashboardDesignActivity.onCreate(AndroidDashboardDesignActivity.java:92)
12-03 11:48:06.099: E/AndroidRuntime(13538):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-03 11:48:06.099: E/AndroidRuntime(13538):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
12-03 11:48:06.099: E/AndroidRuntime(13538):    ... 11 more

这是我的.AndroidDashboardDesignActivity:

package com.groupone.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AndroidDashboardDesignActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard_layout);

        /**
         * Creating all buttons instances
         * */
        // Dash board News feed button
        Button btn_newsfeed = (Button) findViewById(R.id.btn_news_feed);

        // Dash board Friends button
        Button btn_friends = (Button) findViewById(R.id.btn_friends);

        // Dash board Messages button
        Button btn_messages = (Button) findViewById(R.id.btn_messages);

        // Dash board Places button
        Button btn_places = (Button) findViewById(R.id.btn_places);

        // Dash board Events button
        Button btn_events = (Button) findViewById(R.id.btn_events);

        // Dash board Photos button
        Button btn_photos = (Button) findViewById(R.id.btn_photos);

        /**
         * Handling all button click events
         * */

        // Listening to News Feed button click
        btn_newsfeed.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // Launching News Feed Screen
                Intent i = new Intent(getApplicationContext(), NewsFeedActivity.class);
                startActivity(i);
            }
        });

       // Listening Friends button click
        btn_friends.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // Launching News Feed Screen
                Intent i = new Intent(getApplicationContext(), AndroidFacebookConnectActivity.class);
                startActivity(i);
            }
        });

        // Listening Messages button click
        btn_messages.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // Launching News Feed Screen
                Intent i = new Intent(getApplicationContext(), MessagesActivity.class);
                startActivity(i);
            }
        });

        // Listening to Places button click
        btn_places.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // Launching News Feed Screen
                Intent i = new Intent(getApplicationContext(), MessagesActivity.class);
                startActivity(i);
            }
        });

        // Listening to Events button click
        btn_events.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // Launching News Feed Screen
                Intent i = new Intent(getApplicationContext(), MessagesActivity.class);
                startActivity(i);
            }
        });

        // Listening to Photos button click
        btn_photos.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // Launching News Feed Screen
                Intent i = new Intent(getApplicationContext(), PhotosActivity.class);
                startActivity(i);
            }
        });
    }
}

这是我的布局:

!-- Your package folder -->
<com.groupone.the.health.effect.DashboardLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#3b3b3b" >
    <!--  News Feed Button -->
    <Button
        android:id="@+id/btn_news_feed"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/healthbook"
        android:text="Activities" />

    <Button
        android:id="@+id/btn_messages"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/mental"
        android:text="Mental Input" />

    <!--  Events Button -->
    <Button
        android:id="@+id/btn_events"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/graph"
        android:text="Results" />

    <!--  Photos Button -->


    <!--  Friends Button -->
    <Button
        android:id="@+id/btn_friends"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/btn_facebook"
        android:text="Facebook" />

    <!--  Messages Button -->


    <!--  Places Button -->
    <Button
        android:id="@+id/btn_places"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/twitter_logo"
        android:text="Health Tweets" />



    <Button 
        android:id="@+id/btnLogout"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/logout"
        android:text="Log Out"/>



</com.groupone.the.health.effect.DashboardLayout>

1 个答案:

答案 0 :(得分:1)

Button btn_photos = (Button) findViewById(R.id.btn_photos);

此行导致问题,因为当前布局xml中不存在R.id.btn_photos按钮ID。因此,请确保您在活动setContentView(R.layout.dashboard_layout);

中使用正确的布局