java - Android Studio 运行时错误

标签 java android android-studio runtime-error findviewbyid

我在 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

但我似乎找不到代码中出错的地方,尝试注释掉我拥有的所有 findViewById 但没有运气。

代码:

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

干杯

最佳答案

您应该将您的 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);

关于java - Android Studio 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35997321/

相关文章:

java - 如何在Java中调度循环

java - takeWhile, dropWhile 惰性 java9

android - Unity 应用程序因 Firebase SDK 而崩溃

java - 使用 Jsoup 和 Android Studio 截断网站

java - android studio 找不到指定的文件

java - 如何以这种格式打印星形图案* ** *** *** ***?

java - 在 Groovy 中查找一个月的第 n 个工作日

android - Android 将 JSON 键值对转换为 JSON 数组

android - 从命令行开发适用于 Android 的 Qt 应用程序

java - 如何关闭来自其他java的音乐bgm(背景音乐)