是什么导致我的Android应用程序强行关闭?

时间:2014-01-02 23:54:08

标签: android eclipse

我试图添加一个按钮,并在点击时切换布局,但每次我运行应用程序时;它意外停止了。我很困惑,如果有人可以帮助我那将是非常棒的。

Logcat消息 -

01-02 18:41:54.844: W/dalvikvm(2070): threadid=1: thread exiting with uncaught exception (group=0xb0f00648)
01-02 18:41:54.844: E/AndroidRuntime(2070): FATAL EXCEPTION: main
01-02 18:41:54.844: E/AndroidRuntime(2070): java.lang.NullPointerException
01-02 18:41:54.844: E/AndroidRuntime(2070):     at sehej.android.doge.FullscreenActivity$3.onVisibilityChange(FullscreenActivity.java:82)
01-02 18:41:54.844: E/AndroidRuntime(2070):     at sehej.android.doge.util.SystemUiHiderHoneycomb$1.onSystemUiVisibilityChange(SystemUiHiderHoneycomb.java:113)

FullScreenActivity.java(主屏幕的Java文件) -

public class FullscreenActivity 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;

    public void button1_click(final View view)
    {
      startActivity(new Intent(this, MainGameActivity.class));
    }

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

        setContentView(R.layout.activity_main);

        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 == 0) {
                                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 && AUTO_HIDE) {                         delayedHide(AUTO_HIDE_DELAY_MILLIS);
                        }
                    }
                });

        // Set up the user interaction to manually show or hide the system UI.
        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);
    }

    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);
    }
}

activity_main.xml(主屏幕的布局) -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/grass"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:padding="20sp"
    app:textAlignment="center"
    tools:context=".MainActivity" >

    <View
        app:id="@+id/fullscreen_content_controls"
        app:layout_width="wrap_content"
        app:layout_height="wrap_content" />

    <View
        app:id="@+id/fullscreen_content"
        app:layout_width="wrap_content"
        app:layout_height="wrap_content" />

    <requestFocus
        app:layout_width="wrap_content"
        app:layout_height="wrap_content" />

    <Button
        app:id="@+id/button1"
        style="@style/ButtonBar"
        app:layout_width="150dp"
        app:layout_height="wrap_content"
        app:layout_below="@+id/textView1"
        app:layout_centerHorizontal="true"
        app:layout_marginTop="45dp"
        app:onClick="button1_click"
        app:text="@string/button1"
        app:typeface="sans" />

    <TextView
        app:id="@+id/textView1"
        app:layout_width="wrap_content"
        app:layout_height="wrap_content"
        app:layout_alignParentTop="true"
        app:layout_centerHorizontal="true"
        app:layout_marginTop="14dp"
        app:text="@string/title"
        app:textColor="@color/blue"
        app:textSize="60sp"
        app:textStyle="bold"
        app:typeface="sans" />

    <Button
        app:id="@+id/button2"
        style="@style/ButtonBar"
        app:layout_width="150dp"
        app:layout_height="wrap_content"
        app:layout_alignLeft="@+id/button1"
        app:layout_below="@+id/button1"
        app:layout_marginTop="52dp"
        app:text="@string/button2"
        app:typeface="sans" />

    <ImageButton
        app:id="@+id/imageButton1"
        style="@style/FullscreenActionBarStyle"
        app:layout_width="wrap_content"
        app:layout_height="wrap_content"
        app:layout_alignParentBottom="true"
        app:layout_alignRight="@+id/textView1"
        app:contentDescription="@string/dogepic_info"
        app:src="@drawable/doge" />

    <TextView
        app:id="@+id/textView2"
        app:layout_width="wrap_content"
        app:layout_height="wrap_content"
        app:layout_alignTop="@+id/imageButton1"
        app:layout_centerHorizontal="true"
        app:text="@string/dogepic_caption"
        app:textColor="@color/green"
        app:textSize="15sp"
        app:textStyle="bold"
        app:typeface="sans" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

此方法应在onCreate()中注册为onClickListener。此外,它的View参数不能是最终的。

public void button1_click(final View view)
{
  startActivity(new Intent(this, MainGameActivity.class));
}

换句话说,你应该写:

public class FullscreenActivity 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;
    Button buttonView;
    View controlsView;
    View contentView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        contentView = (View)findViewById(R.id.fullscreen_content);
        controlsView = (View)findViewById(R.id.fullscreen_content_controls);
        buttonView = (View)findViewById(R.id.button1);
        buttonView.setOnClickListener(myButtonHandler);

        mSystemUiHider = SystemUiHider.getInstance(this, contentView,
                HIDER_FLAGS);
        mSystemUiHider.setup();
        mSystemUiHider
                .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
                    int mControlsHeight;
                    int mShortAnimTime;
       }
       View.OnClickListener myButtonHandler = new View.OnClickListener() {
       public void onClick(View v) {
           startActivity(new Intent(this, MainGameActivity.class));
       }
    };         
    @Override
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    public void onVisibilityChange(boolean visible) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {

                        if (mControlsHeight == 0) {
                            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 && AUTO_HIDE) {                          
                        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();
            }
        }
    });
    };

答案 1 :(得分:0)

是的,你声明事物的方式不是最好的做法,你得到一个空指针异常,这意味着没有找到,初始化,声明或不存在引用,变量等..希望帮助

答案 2 :(得分:0)

堆栈跟踪为您提供跟踪错误所需的所有信息。在顶部,两行告诉您,您尝试通过空引用访问变量或方法:

java.lang.NullPointerException
at sehej.android.doge.FullscreenActivity$3.onVisibilityChange(FullscreenActivity.java:82)

当只需要第82行周围的区域时,您已经为我们粘贴了此源文件的内容以及更多文件。当然,堆栈跟踪告诉你这一点,所以学会阅读它 - 它是一个非常宝贵的调试工具。

为了帮助您入门,以下是第81-83行:

if (mControlsHeight == 0) {
    mControlsHeight = controlsView.getHeight();
}

在程序的第36行,您尝试实例化 controlsView ,如下所示:

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

在此行之后添加一个调试语句,打印 controlsView 变量。它是否产生对象引用或“(null)”?如果它为null,为什么你认为 findViewById()可能会失败?