简体   繁体   English

Android Studio运行时错误

[英]Android Studio Runtime Errors

I keep getting this error in Android Studio: 我在Android Studio中不断收到此错误:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{dit.assignment3/dit.assignment3.Timer}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference java.lang.RuntimeException:无法实例化活动ComponentInfo {dit.assignment3 / dit.assignment3.Timer}:java.lang.NullPointerException:尝试调用虚拟方法'android.view.View android.view.Window.findViewById(int) '在空对象引用上

but I can't seem to find where in my code I'm going wrong, have tried commenting out all the findViewById's that I have but no luck. 但是我似乎找不到我的代码出了错,尝试注释掉我所有但没有运气的所有findViewById。

Code: 码:

public class Timer extends AppCompatActivity {       
    //declaring all items on page in class    
       EditText hoursIn = (EditText) findViewById(R.id.hoursET);   
       EditText minIn = (EditText) findViewById(R.id.minET);

       Button start = (Button) findViewById(R.id.startButton);
       Button stop = (Button) findViewById(R.id.stopButton);

       TextView textViewTime = (TextView) findViewById(R.id.timeDisp);


    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
      // private GoogleApiClient client;

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

        textViewTime.setText("00:00:30");

        final CounterClass timer = new CounterClass(30000, 1000);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.start();
            }
        });


        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.cancel();
            }
        });
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        //client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    //@Override
    /*public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Timer", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://dit.assignment3/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Timer Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://dit.assignment3/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }*/

       public class CounterClass extends CountDownTimer {

        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public CounterClass(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            long millis = millisUntilFinished;
            String hrsminsec = String.format("%02:%02:%02", TimeUnit.MILLISECONDS.toHours(millis),
                    TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toMinutes(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toSeconds(millis)))
            );

            textViewTime.setText(hrsminsec);
        }

        @Override
        public void onFinish() {
            textViewTime.setText("DONE");
        }
    }
}

Cheers 干杯

findViewById cannot be called before both onCreate and setContentView . 不能在onCreatesetContentView之前调用findViewById

Change around the following 围绕以下内容进行更改

EditText hoursIn;

public void onCreate(...){
    ...

    setContentView(...);

    hoursIn = (EditText) findViewById(R.id.hoursET);

    ...
}

The reason for this is that findViewById is going to lookup an element that is currently on the display, before setting the content view, there is nothing to be found, thus leaving the result of findViewById to be null . 原因是findViewById将查找当前在显示中的元素,在设置内容视图之前,找不到任何内容,因此将findViewById的结果保留为null

You should put your findViewById calls in your onCreate you are trying to assign them before the Window has been created. 您应该在创建窗口之前将findViewById调用放入要分配它们的onCreate

EditText hoursIn;   
EditText minIn;

Button start;
Button stop;

TextView textViewTime;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer);
    hoursIn = (EditText) findViewById(R.id.hoursET);   
    minIn = (EditText) findViewById(R.id.minET);

    start = (Button) findViewById(R.id.startButton);
    stop = (Button) findViewById(R.id.stopButton);

    textViewTime = (TextView) findViewById(R.id.timeDisp);
    You should assign all controls in onCreate()

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer);
        EditText hoursIn = (EditText)  findViewById(R.id.hoursET);   
        EditText minIn = (EditText) findViewById(R.id.minET);
        textViewTime.setText("00:00:30");

        final CounterClass timer = new CounterClass(30000, 1000);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.start();
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM