尝试在Android中添加ClickListener时获取NullPointerException ..为什么?

时间:2013-12-21 02:39:41

标签: java android nullpointerexception

我是Android开发的新手,我正在尝试制作单元转换应用。我的距离活动能够自行解决,但是当我尝试添加ClickListeners时,这一切都变得很糟糕。我得到一个NullPointerException并且我不知道为什么,我的第一个想法是我引用了错误的Id但似乎我引用了正确的位置。应用程序启动到主屏幕,其中有一个按钮可打开距离活动。一旦我尝试打开距离,应用程序崩溃。这是距离:

package org.belliveau.convert;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

    public class Distance extends Activity {

        int inputInInches, output;
        Button inchInput, footInput, yardInput, mileInput, inchOutput, footOutput, yardOutput, mileOutput;
        TextView result;
        EditText inputText;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setButtons();
            setContentView(R.layout.activity_distance);
            setClickListeners();
            // Show the Up button in the action bar.
            setupActionBar();
        }

        /**
         * Set up the {@link android.app.ActionBar}, if the API is available.
         */
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        private void setupActionBar() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                getActionBar().setDisplayHomeAsUpEnabled(true);
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.distance, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
                // This ID represents the Home or Up button. In the case of this
                // activity, the Up button is shown. Use NavUtils to allow users
                // to navigate up one level in the application structure. For
                // more details, see the Navigation pattern on Android Design:
                //
                // http://developer.android.com/design/patterns/navigation.html#up-vs-back
                //
                NavUtils.navigateUpFromSameTask(this);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
        private void setButtons(){
            inchInput = (Button) findViewById(R.id.inch);
            footInput = (Button) findViewById(R.id.foot_button);
            yardInput = (Button) findViewById(R.id.yard_button);
            mileInput = (Button) findViewById(R.id.mile_button);

            inchOutput = (Button) findViewById(R.id.inch_post);
            footOutput = (Button) findViewById(R.id.foot_post);
            yardOutput = (Button) findViewById(R.id.yard_post);
            mileOutput = (Button) findViewById(R.id.mile_post);

            result = (TextView) findViewById(R.id.dist_result);
            inputText = (EditText) findViewById(R.id.distance_initial);
        }

        private void setClickListeners(){
            inchInput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    int input = Integer.valueOf(inputText.getText().toString());
                    inch2inch(input);
                }

            });
            footInput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    int input = Integer.valueOf(inputText.getText().toString());
                    foot2inch(input);
                }

            });
            yardInput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    int input = Integer.valueOf(inputText.getText().toString());
                    yard2inch(input);
                }

            });
            mileInput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    int input = Integer.valueOf(inputText.getText().toString());
                    mile2inch(input);
                }

            });
            inchOutput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    inchDuh();
                }

            });
            footOutput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    outputFoot();
                }

            });
            yardOutput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    outputYard();
                }

            });
            mileOutput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    outputMile();
                }

            });
        }

        public void inch2inch(int inch) {
            inputInInches = inch;
        }

        public void foot2inch(int foot) {
            inputInInches = foot * 12;
        }

        public void yard2inch(int yard) {
            int foot = yard * 3;
            inputInInches = foot * 3;

        }

        public void mile2inch(int mile) {
            int yard = mile * 1760;
            int foot = yard * 3;
            inputInInches = foot * 3;

        }

        public void outputFoot() {
            output = inputInInches / 12;
            result.setText(output);
        }

        public void outputYard() {
            int foot = inputInInches / 12;
            output = foot / 3;
            result.setText(output);
        }

        public void outputMile() {
            int foot = inputInInches / 12;
            int yard = foot / 3;
            output = yard / 1760;
            result.setText(output);
        }
        public void inchDuh(){
            output = inputInInches;
            result.setText(output);
        }

和LogCat:

12-20 21:34:35.674: I/Adreno-EGL(3199): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build:  (CL4169980)
12-20 21:34:35.674: I/Adreno-EGL(3199): OpenGL ES Shader Compiler Version: 17.01.10.SPL
12-20 21:34:35.674: I/Adreno-EGL(3199): Build Date: 10/30/13 Wed
12-20 21:34:35.674: I/Adreno-EGL(3199): Local Branch: 
12-20 21:34:35.674: I/Adreno-EGL(3199): Remote Branch: 
12-20 21:34:35.674: I/Adreno-EGL(3199): Local Patches: 
12-20 21:34:35.674: I/Adreno-EGL(3199): Reconstruct Branch: 
12-20 21:34:35.744: D/OpenGLRenderer(3199): Enabling debug mode 0
12-20 21:34:38.474: D/AndroidRuntime(3199): Shutting down VM
12-20 21:34:38.474: W/dalvikvm(3199): threadid=1: thread exiting with uncaught exception (group=0x4166dd28)
12-20 21:34:38.476: E/AndroidRuntime(3199): FATAL EXCEPTION: main
12-20 21:34:38.476: E/AndroidRuntime(3199): Process: org.belliveau.convert, PID: 3199
12-20 21:34:38.476: E/AndroidRuntime(3199): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.belliveau.convert/org.belliveau.convert.Distance}: java.lang.NullPointerException
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.ActivityThread.access$700(ActivityThread.java:139)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1411)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.os.Looper.loop(Looper.java:137)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.ActivityThread.main(ActivityThread.java:5083)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at java.lang.reflect.Method.invokeNative(Native Method)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at java.lang.reflect.Method.invoke(Method.java:515)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at dalvik.system.NativeStart.main(Native Method)
12-20 21:34:38.476: E/AndroidRuntime(3199): Caused by: java.lang.NullPointerException
12-20 21:34:38.476: E/AndroidRuntime(3199):     at org.belliveau.convert.Distance.setClickListeners(Distance.java:82)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at org.belliveau.convert.Distance.onCreate(Distance.java:28)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.Activity.performCreate(Activity.java:5260)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2154)
12-20 21:34:38.476: E/AndroidRuntime(3199):     ... 12 more

1 个答案:

答案 0 :(得分:1)

反转这些电话

setButtons();
setContentView(R.layout.activity_distance);

换句话说,它们应该是

setContentView(R.layout.activity_distance);
setButtons();

您正在调用setButtons(),它试图从单一化的UI中获取视图,因此

inchInput = (Button) findViewById(R.id.inch); // returns null

inchInput设置为null