1

ログキャット:

09-05 19:18:35.166: E/AndroidRuntime(25586): FATAL EXCEPTION: main
09-05 19:18:35.166: E/AndroidRuntime(25586): java.lang.NullPointerException
 09-05 19:18:35.166: E/AndroidRuntime(25586):   at com.example.eveminingcalculator.UpdatePrices$2.run(UpdatePrices.java:215)
 09-05 19:18:35.166: E/AndroidRuntime(25586):   at android.os.Handler.handleCallback(Handler.java:615)
 09-05 19:18:35.166: E/AndroidRuntime(25586):   at android.os.Handler.dispatchMessage(Handler.java:92)
 09-05 19:18:35.166: E/AndroidRuntime(25586):   at android.os.Looper.loop(Looper.java:137)
 09-05 19:18:35.166: E/AndroidRuntime(25586):   at android.app.ActivityThread.main(ActivityThread.java:4961)
 09-05 19:18:35.166: E/AndroidRuntime(25586):   at java.lang.reflect.Method.invokeNative(Native Method) 09-05 19:18:35.166: E/AndroidRuntime(25586):    at java.lang.reflect.Method.invoke(Method.java:511)
 09-05 19:18:35.166: E/AndroidRuntime(25586):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
 09-05 19:18:35.166: E/AndroidRuntime(25586):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
 09-05 19:18:35.166: E/AndroidRuntime(25586):   at dalvik.system.NativeStart.main(Native Method)

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.eveminingcalculator"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".UpdatePrices"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="portrait"
            android:label="@string/app_name"
            android:theme="@style/FullscreenTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

UpdatePrices.java

package com.example.eveminingcalculator;

import com.example.eveminingcalculator.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.widget.ArrayAdapter;
import android.widget.Spinner;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 *
 * @see SystemUiHider
 */

public class UpdatePrices extends Activity {
    /**
     * Whether or not the system UI should be auto-hidden after
     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
     */
    private static final boolean AUTO_HIDE = true;

    /**
     * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
     * user interaction before hiding the system UI.
     */
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

    /**
     * If set, will toggle the system UI visibility upon interaction. Otherwise,
     * will show the system UI visibility upon interaction.
     */
    private static final boolean TOGGLE_ON_CLICK = true;

    /**
     * The flags to pass to {@link SystemUiHider#getInstance}.
     */
    private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;

    /**
     * The instance of the {@link SystemUiHider} for this activity.
     */
    private SystemUiHider mSystemUiHider;

    protected void onCreate(Bundle savedInstanceState, ArrayAdapter<CharSequence> Adapter) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_update_prices);

        final View controlsView = findViewById(R.id.SaveButtonLayout);
        final View contentView = findViewById(R.id.Background);

        Spinner ShipSpinner = (Spinner) findViewById(R.id.spinnerShips);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.Ships, R.layout.spinner_layout);
        adapter.setDropDownViewResource(R.layout.spinner_layout);
        ShipSpinner.setAdapter(adapter);

        Spinner TurretSpinner = (Spinner) findViewById(R.id.spinnerTurrets);
        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
                R.array.Turrets, R.layout.spinner_layout);
        adapter2.setDropDownViewResource(R.layout.spinner_layout);
        TurretSpinner.setAdapter(adapter2);

        Spinner TurretNumSpinner = (Spinner) findViewById(R.id.spinnerTurretNumber);
        ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this,
                R.array.NumTurrets, R.layout.spinner_layout);
        adapter3.setDropDownViewResource(R.layout.spinner_layout);
        TurretNumSpinner.setAdapter(adapter3);

        Spinner UpgradesSpinner = (Spinner) findViewById(R.id.spinnerUpgrades);
        ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(this,
                R.array.Upgrades, R.layout.spinner_layout);
        adapter4.setDropDownViewResource(R.layout.spinner_layout);
        UpgradesSpinner.setAdapter(adapter4);

        Spinner UpgradesNumSpinner = (Spinner) findViewById(R.id.spinnerUpgradesNum);
        ArrayAdapter<CharSequence> adapter5 = ArrayAdapter.createFromResource(this,
                R.array.NumUpgrades, R.layout.spinner_layout);
        adapter5.setDropDownViewResource(R.layout.spinner_layout);
        UpgradesNumSpinner.setAdapter(adapter5);

        Spinner ImplantSpinner = (Spinner) findViewById(R.id.spinnerImplant);
        ArrayAdapter<CharSequence> adapter6 = ArrayAdapter.createFromResource(this,
                R.array.Implants, R.layout.spinner_layout);
        adapter6.setDropDownViewResource(R.layout.spinner_layout);
        ImplantSpinner.setAdapter(adapter6);

        Spinner ShipSkillSpinner = (Spinner) findViewById(R.id.spinnerShipSkill);
        ArrayAdapter<CharSequence> adapter7 = ArrayAdapter.createFromResource(this,
                R.array.SkillLevel, R.layout.spinner_layout);
        adapter7.setDropDownViewResource(R.layout.spinner_layout);
        ShipSkillSpinner.setAdapter(adapter7);

        Spinner MiningSkillSpinner = (Spinner) findViewById(R.id.spinnerMiningSkill);
        ArrayAdapter<CharSequence> adapter8 = ArrayAdapter.createFromResource(this,
                R.array.SkillLevel, R.layout.spinner_layout);
        adapter8.setDropDownViewResource(R.layout.spinner_layout);
        MiningSkillSpinner.setAdapter(adapter8);

        Spinner AstroSkillSpinner = (Spinner) findViewById(R.id.spinnerAstroSkill);
        ArrayAdapter<CharSequence> adapter9 = ArrayAdapter.createFromResource(this,
                R.array.SkillLevel, R.layout.spinner_layout);
        adapter9.setDropDownViewResource(R.layout.spinner_layout);
        AstroSkillSpinner.setAdapter(adapter9);

        Spinner IceSkillSpinner = (Spinner) findViewById(R.id.spinnerIceHarvSkill);
        ArrayAdapter<CharSequence> adapter10 = ArrayAdapter.createFromResource(this,
                R.array.SkillLevel, R.layout.spinner_layout);
        adapter10.setDropDownViewResource(R.layout.spinner_layout);
        IceSkillSpinner.setAdapter(adapter10);

        Spinner DeepCoreSkillSpinner = (Spinner) findViewById(R.id.spinnerDeepCoreSkill);
        ArrayAdapter<CharSequence> adapter11 = ArrayAdapter.createFromResource(this,
                R.array.SkillLevel, R.layout.spinner_layout);
        adapter11.setDropDownViewResource(R.layout.spinner_layout);
        DeepCoreSkillSpinner.setAdapter(adapter11);

        // Set up an instance of SystemUiHider to control the system UI for
        // this activity.
        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 the ViewPropertyAnimator API is available
                            // (Honeycomb MR2 and later), use it to animate the
                            // in-layout UI controls at the bottom of the
                            // screen.
                            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 {
                            // If the ViewPropertyAnimator APIs aren't
                            // available, simply show or hide the in-layout UI
                            // controls.
                            controlsView.setVisibility(visible ? View.VISIBLE : View.GONE);
                        }

                        if (visible && AUTO_HIDE) {
                            // Schedule a 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();
                }
            }
        });

        // Upon interacting with UI controls, delay any scheduled hide()
        // operations to prevent the jarring behavior of controls going away
        // while interacting with the UI.
        findViewById(R.id.buttonCalculateIPH).setOnTouchListener(mDelayHideTouchListener);
    }

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

        // Trigger the initial hide() shortly after the activity has been
        // created, to briefly hint to the user that UI controls
        // are available.
        delayedHide(100);
    }


    /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */
    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();
        }
    };

    /**
     * Schedules a call to hide() in [delay] milliseconds, canceling any
     * previously scheduled calls.
     */
    private void delayedHide(int delayMillis) {
        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }
}
4

1 に答える 1

1

これが起こっていると私が推測していることです。

あなたが呼んでいます

super.onCreate(savedInstanceState);

あなたが呼び出す前に呼び出されます

mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);

これは、androids OnCreate が呼び出しOnPostCreate();先であなたを呼び出すことを意味します。delayedHide(100);このコードは、あなたmSystemUiHiderが使用されている他のメソッドを呼び出します。この時点で、 yourmSystemUiHiderはまだ null であることに注意してください。onCreate()コードが実行されていないため。(私たちはまだsuper.onCreate()あなた自身のonCreate()方法にいます)。

OnPostExecute実行する代わりに、delayedHide(100)おそらくそれをonCreateメソッドの最後に置くことで、それが初期化されたことを知ることmSystemUiHiderができます。

于 2013-09-06T05:18:45.013 に答える