实例化类给出了RuntimeException

时间:2016-05-27 19:09:59

标签: java android-studio runtimeexception

实例化MainActivity会产生错误:" java.lang.RuntimeException:无法在未调用Looper.prepare()"

的线程内创建处理程序

我已经看过很多关于这个错误的帖子,而且大部分内容都是关于UI线程的,但我对此无动于衷。

public void calculate() {
    MainActivity mainActivity = new MainActivity();
    best = mainActivity.getBest();
    if (total > 5000 && Math.abs(error) < best) {
        System.out.println("Evaluate: best update");
        mainActivity.updateBest(error);
    }
}

代码的结构和错误类型是这样的。

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.app.Activity.<init>(Activity.java:754)
at com.seung.pi_calculator.MainActivity.<init>(MainActivity.java:0)
at com.seung.pi_calculator.Dots.calculate(Dots.java:89)
at com.seung.pi_calculator.Dots.drawText(Dots.java:72)
at com.seung.pi_calculator.Dots.draw(Dots.java:66)
at com.seung.pi_calculator.CustomView.draw(CustomView.java:67)
at com.seung.pi_calculator.MainThread.run(MainThread.java:51)

方法中没有什么特别的,只是绘制画布。 (或者这是问题?)

我查了Handler或Asynctask,但是他们给出了错误:&#34; java.lang.RuntimeException:每个线程只能创建一个Looper&#34;

所以在浪费几个小时试图找到背后的问题后,我一无所知。

任何帮助都会非常有帮助。感谢。

添加了主要活动。 (顺便说一句,我如何缩进多行?添加四个空格需要很长时间..复制和粘贴代码并不像现在这样复制)

package com.seung.pi_calculator;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.Toast;

public class MainActivity extends Activity{
    private int count, total;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new CustomView(this));
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getBest();
    }

    public MainActivity() {

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Dots dots = new Dots();
        outState.putInt("count", dots.count);
        outState.putInt("total", dots.total);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Dots dots = new Dots();
        dots.count = savedInstanceState.getInt("count");
        dots.total = savedInstanceState.getInt("total");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.reset:
                reset();
                return true;
            case R.id.save:
                save();
                return true;
            case R.id.restore:
                restore();
                return true;
            case R.id.best:
                best();
                return true;
            case R.id.help:
                help();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void reset() {
        Dots dots = new Dots();
        dots.set(0, 0);
        finish();
        startActivity(getIntent());
    }

    public void save() {
        SharedPreferences sharedPreferences = getSharedPreferences("preferences",0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Dots dots = new Dots();
        editor.putInt("count", dots.count);
        editor.putInt("total", dots.total);
        editor.commit();
        System.out.println("saved. count:"+dots.count+", total:"+dots.total);
        //finish();
        //moveTaskToBack(true);
    }

    public void restore() {
        System.out.println("restore");
        SharedPreferences sharedPreferences = getSharedPreferences("preferences",0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        count = sharedPreferences.getInt("count", 0);
        total = sharedPreferences.getInt("total", 0);
        System.out.println("count, total: " +count+ " " + total);
        if (total != 0) {
            Dots dots = new Dots();
            dots.set(count, total);
            System.out.println("restored. count:" + count + ", total:" + total);
            editor.putInt("count", 0);
            editor.putInt("total", 0);
            editor.commit();
        }
    }

    public void best() {
        SharedPreferences sharedPreferences = getSharedPreferences("preferences",0);
        String best = sharedPreferences.getString("BestToast", "No Best yet");
        Toast.makeText(MainActivity.this, best, Toast.LENGTH_LONG).show();
    }

    public void updateBest(double best) {
        SharedPreferences sharedPreferences = getSharedPreferences("preferences", 0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        String bestString = Double.toString(best);
        editor.putString("Best", bestString);
        Dots dots = new Dots();
        editor.putString("BestToast", "Best: error = " + best + ", count/total = " + dots.count + "/" + dots.total);
        editor.commit();
        System.out.println("Best updated. best:" + best);
    }

    public double getBest() {
        SharedPreferences sharedPreferences = getSharedPreferences("preferences",0);
        System.out.println("getbest called");
        System.out.println("double : " +sharedPreferences.getString("Best","1"));
        double returnBest = Double.parseDouble(sharedPreferences.getString("Best", "1"));
        return returnBest;
    }

    public void help() {
        Intent intent = new Intent(this, Help.class);
        startActivity(intent);
    }
}

0 个答案:

没有答案