我的应用程序上的java.lang.NullPointerException

时间:2014-05-21 02:02:15

标签: java android nullpointerexception android-studio

我使用Android Studio,这是我第一次为Android开发 还有java语言!

因此,在构建我的项目并在我自己的手机上运行它时,它崩溃了 我进入第二个活动。

这是我尝试打开的活动的xml。:( activity_infomenu.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fullscreen_content"
    android:background="#ff282bff"
    android:fitsSystemWindows="true"
    android:keepScreenOn="true"
    android:gravity="center"
    tools:context="com.luiscesjr.aquariodebolso.aquariodebolso.infomenu">


    <ImageButton
        android:layout_width="120dp"
        android:layout_height="150dp"
        android:id="@+id/button7"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:src="@drawable/calendar128"
        />

    <ImageButton
        android:layout_width="120dp"
        android:layout_height="150dp"
        android:id="@+id/button8"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/button11"
        android:layout_alignStart="@+id/button11"
        android:src="@drawable/fish128"/>

    <ImageButton
        android:layout_width="120dp"
        android:layout_height="150dp"
        android:id="@+id/button9"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/button12"
        android:layout_alignStart="@+id/button12"
        android:src="@drawable/clown_fish128"/>

    <ImageButton
        android:layout_width="120dp"
        android:layout_height="150dp"
        android:id="@+id/button10"
        android:layout_marginTop="102dp"
        android:layout_below="@+id/button7"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:src="@drawable/ruler128"/>

    <ImageButton
        android:layout_width="120dp"
        android:layout_height="150dp"
        android:id="@+id/button11"
        android:layout_alignTop="@+id/button10"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ruler128"/>

    <ImageButton
        android:layout_width="120dp"
        android:layout_height="150dp"
        android:id="@+id/button12"
        android:layout_alignTop="@+id/button11"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:src="@drawable/ruler128"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Calendário"
        android:id="@+id/textView"
        android:layout_below="@+id/button7"
        android:layout_alignRight="@+id/textView4"
        android:layout_alignEnd="@+id/textView4" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Aqua Doce"
        android:id="@+id/textView2"
        android:layout_below="@+id/button8"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Aqua Marinho"
        android:id="@+id/textView3"
        android:layout_below="@+id/button9"
        android:layout_alignLeft="@+id/button9"
        android:layout_alignStart="@+id/button9" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Ferramentas"
        android:id="@+id/textView4"
        android:layout_below="@+id/button10"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView5"
        android:layout_below="@+id/button11"
        android:layout_alignRight="@+id/button11"
        android:layout_alignEnd="@+id/button11" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView6"
        android:layout_below="@+id/button12"
        android:layout_alignLeft="@+id/button12"
        android:layout_alignStart="@+id/button12" />

    <!-- This FrameLayout insets its children based on system windows using
         android:fitsSystemWindows. -->

</RelativeLayout>

守则:(infomenu.java) (这是自动生成的活动,全屏​​形成Android Studio。)

package com.luiscesjr.aquariodebolso.aquariodebolso;

import com.luiscesjr.aquariodebolso.aquariodebolso.util.SystemUiHider;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.graphics.drawable.AnimationDrawable;


public class infomenu extends Activity {

    private static final boolean AUTO_HIDE = true;
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
    private static final boolean TOGGLE_ON_CLICK = true;
    private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
    private SystemUiHider mSystemUiHider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_infomenu);
        setupActionBar();

        final View controlsView = findViewById(R.id.fullscreen_content_controls);
        final View contentView = findViewById(R.id.fullscreen_content);

        mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
        mSystemUiHider.setup();
        mSystemUiHider
                .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
                    // Cached values.
                    int mControlsHeight;
                    int mShortAnimTime;

                    @Override
                    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
                    public void onVisibilityChange(boolean visible) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {

                            if (mControlsHeight == ) {
   // LINE 83 HERE              mControlsHeight = controlsView.getHeight(); 
                            }
                            if (mShortAnimTime == 0) {
                                mShortAnimTime = getResources().getInteger(
                                        android.R.integer.config_shortAnimTime);
                            }
                            controlsView.animate()
                                    .translationY(visible ? 0 : mControlsHeight)
                                    .setDuration(mShortAnimTime);
                        } else {

                            controlsView.setVisibility(visible ? View.VISIBLE : View.GONE);
                        }

                        if (visible) {

                            delayedHide(AUTO_HIDE_DELAY_MILLIS);
                        }
                    }
                });


        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TOGGLE_ON_CLICK) {
                    mSystemUiHider.toggle();
                } else {
                    mSystemUiHider.show();
                }
            }
        });
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        delayedHide(100);
    }


    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {

            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (AUTO_HIDE) {
                delayedHide(AUTO_HIDE_DELAY_MILLIS);
            }
            return false;
        }
    };

    Handler mHideHandler = new Handler();
    Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            mSystemUiHider.hide();
        }
    };


    private void delayedHide(int delayMillis) {
        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }
}

这是错误。:

05-20 22:48:24.737  18794-18794/com.luiscesjr.aquariodebolso.aquariodebolso E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.luiscesjr.aquariodebolso.aquariodebolso.infomenu$1.onVisibilityChange(infomenu.java:83)
            at com.luiscesjr.aquariodebolso.aquariodebolso.util.SystemUiHiderHoneycomb$1.onSystemUiVisibilityChange(SystemUiHiderHoneycomb.java:112)
            at android.view.View.dispatchSystemUiVisibilityChanged(View.java:15933)
            at android.view.ViewGroup.dispatchSystemUiVisibilityChanged(ViewGroup.java:1412)
            at android.view.ViewGroup.dispatchSystemUiVisibilityChanged(ViewGroup.java:1418)
            at android.view.ViewGroup.dispatchSystemUiVisibilityChanged(ViewGroup.java:1418)
            at android.view.ViewGroup.dispatchSystemUiVisibilityChanged(ViewGroup.java:1418)
            at android.view.ViewRootImpl.handleDispatchSystemUiVisibilityChanged(ViewRootImpl.java:3777)
            at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2966)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)
05-20 22:48:26.417  18794-18794/com.luiscesjr.aquariodebolso.aquariodebolso I/Process﹕ Sending signal. PID: 18794 SIG: 9

我在这里缺少什么? 哦,请,请不要介意应用程序的愚蠢名称。

编辑:自从我添加此内容后不会再出现错误。:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff282bff"
    android:fitsSystemWindows="true"
    android:keepScreenOn="true"
    android:gravity="center"
    tools:context="com.luiscesjr.aquariodebolso.aquariodebolso.infomenu"
    android:id="@+id/fullscreen_content_controls">

     <View //Without this it will still crash, but it also bugs the activity
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fullscreen_content">
     </View>

但现在代码无法正常运行。

1 个答案:

答案 0 :(得分:0)

您要将 controlsView 值设置为此

 final View controlsView = findViewById(R.id.fullscreen_content_controls);

但布局中不存在 fullscreen_content_controls

将您的必需视图添加到您的布局中,因为您的给定布局不包含 fullscreen_content_controls 之类的内容,因此它会抛出 NPE