我的安卓用户界面无法运行?



i实现了异步来执行结果。这是我遇到的错误...

FATAL EXCEPTION: AsyncTask #1
              Process: ai69.psoui, PID: 3287
              java.lang.RuntimeException: An error occurred while executing doInBackground()
                  at android.os.AsyncTask$3.done(AsyncTask.java:309)
                  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                  at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                  at java.lang.Thread.run(Thread.java:818)
               Caused by: 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 android.support.v4.app.SupportActivity.<init>(SupportActivity.java:31)
                  at android.support.v4.app.BaseFragmentActivityGingerbread.<init>(BaseFragmentActivityGingerbread.java:37)
                  at android.support.v4.app.BaseFragmentActivityHoneycomb.<init>(BaseFragmentActivityHoneycomb.java:29)
                  at android.support.v4.app.BaseFragmentActivityJB.<init>(BaseFragmentActivityJB.java:30)
                  at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:79)
                  at android.support.v7.app.AppCompatActivity.<init>(AppCompatActivity.java:61)
                  at ai69.psoui.MainActivity.<init>(MainActivity.java:0)
                  at android_tests.CustomUseCase.<init>(CustomUseCase.java:19)
                  at android_tests.TestFactory.getTest(TestFactory.java:15)
                  at ai69.psoui.ParticleActivity.runTest(ParticleActivity.java:91)
                  at ai69.psoui.ParticleActivity$runTests.doInBackground(ParticleActivity.java:53)
                  at ai69.psoui.ParticleActivity$runTests.doInBackground(ParticleActivity.java:50)
                  at android.os.AsyncTask$2.call(AsyncTask.java:295)

我已经查看了有关" looper.prepare(("的不同SOF帖子。

这是我的代码...

public class ParticleActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "PSOUI.MESSAGE";
private ProgressDialog pd;
private double[] results = {-1.0, -1.0, -1.0};
EditText particles;
EditText iterations;
EditText userSol;
EditText userBatt;
private double battery;
private double solution;
//int numberOfDimensions = MainActivity.dimensions.size();
//public ArrayList<Double> costData = MainActivity.costDATA; //costs that 
the user enters for each resource
//public ArrayList<Double> costWlan = MainActivity.costWLAN;
//public ArrayList<Double> costUtilities = MainActivity.costUTILITY;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_particle);
    particles = (EditText) findViewById(R.id.particles);
    iterations = (EditText) findViewById(R.id.iterations);
    userSol = (EditText) findViewById(R.id.solution);
    userBatt = (EditText) findViewById(R.id.battery);
    pd = null;
    runPSOButton();
}
@Override
public void onPause(){
    super.onPause();
    if(pd != null)
        pd.dismiss();
}
public class runTests extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) { //sort this out
        results = runTest("CustomUseCase"); //i only want to run this one!!!
        return null;
    }
    @Override
    protected void onPostExecute(Void v) {
        if (results != null && results.length > 0 && results[0] != -1) {
            loadIntent(results);
        } //otherwise it will evaluate the next logic statement results[0] != -1 with no chance of NulLPointerException
    }
    @Override
    protected void onPreExecute() {
        pd = ProgressDialog.show(ParticleActivity.this, "Busy", "Algorithm is currently executing");
        pd.setCancelable(true);
        pd.show();
    }
}
public void runPSOButton() {
    final Button runPSO = (Button) findViewById(R.id.runpso);
    runPSO.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            new runTests().execute();
        }
    });
}
public double[] runTest(String test) {
    int noPart = Integer.parseInt(particles.getText().toString());
    int noIter = Integer.parseInt(iterations.getText().toString());

    return new TestFactory(noPart, noIter).getTest(test).test();
}
public void loadIntent(double[] result) {
    double[] results = result;
    Intent intent = new Intent(this, SolutionActivity.class);
    intent.putExtra(EXTRA_MESSAGE, results);
    startActivity(intent);
}
public double setBatteryCost(){
    battery = Double.parseDouble(userBatt.getText().toString());
    return battery;
}
public double getBatteryCost(){return setBatteryCost();}
public double setUserSolution(){
    solution = Double.parseDouble(userSol.getText().toString());
    return solution;
}
public double getUserSolution(){return setUserSolution();}
}

有人可以解释发生了什么吗?Android Studio的新手,已经在Java开发了3个月,因此,对于任何解决方案,我也可以要求提供解释吗?非常感谢您

更新:

这是我的主要目的...

public class MainActivity extends AppCompatActivity {
//declare variables
EditText name;
EditText data;
EditText wlan;
EditText utility;
Button addservice;
ListView lv;
ListView lv2;
ListView lv3;
ListView lv4;
public ArrayList<String> servicenames;
public ArrayList<String> dimensions;
public ArrayList<Double> costDATA;
public ArrayList<Double> costWLAN;
public ArrayList<Double> costUTILITY;
ArrayAdapter<String> namesAdapter;
ArrayAdapter<Double> dataAdapter;
ArrayAdapter<Double> wlanAdapter;
ArrayAdapter<Double> utilityAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //map the components to the variables
    name = (EditText) findViewById(R.id.servicename);
    data = (EditText) findViewById(R.id.data);
    wlan = (EditText) findViewById(R.id.wlan);
    utility = (EditText) findViewById(R.id.utility);
    addservice = (Button) findViewById(R.id.addservice);
    lv = (ListView) findViewById(R.id.lv);
    lv2 = (ListView) findViewById(R.id.lv2);
    lv3 = (ListView) findViewById(R.id.lv3);
    lv4 = (ListView) findViewById(R.id.lv4);
    //create arraylists for each component
    servicenames = new ArrayList<String>();
    dimensions = new ArrayList<String>();
    costDATA = new ArrayList<Double>();
    costWLAN = new ArrayList<Double>();
    costUTILITY = new ArrayList<Double>();
    //create adapters to pass on the arraylist
    namesAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, servicenames);
    dataAdapter = new ArrayAdapter<Double>(MainActivity.this, android.R.layout.simple_list_item_1, costDATA);
    wlanAdapter = new ArrayAdapter<Double>(MainActivity.this, android.R.layout.simple_list_item_1, costWLAN);
    utilityAdapter = new ArrayAdapter<Double>(MainActivity.this, android.R.layout.simple_list_item_1, costUTILITY);
    //display each arraylist in the listviews
    lv.setAdapter(namesAdapter);
    lv2.setAdapter(wlanAdapter);
    lv3.setAdapter(dataAdapter);
    lv4.setAdapter(utilityAdapter);
    namesAdapter.notifyDataSetChanged();
    dataAdapter.notifyDataSetChanged();
    wlanAdapter.notifyDataSetChanged();
    utilityAdapter.notifyDataSetChanged();
    dimensions.add("DATA");
    dimensions.add("WLAN");
    onClickBtn();
}
public void onClickBtn() { //when user clicks button, the user input is added to the listview, and cleared for the next service
    addservice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String namesOfService = name.getText().toString(); //user input for service names
            String costOfData = data.getText().toString(); //user input for data costs
            String costOfWLAN = wlan.getText().toString(); //user input for wlan costs
            String costOfUtility = utility.getText().toString(); //user input for utility costs
            Double doubleWLAN = Double.parseDouble(costOfWLAN); //convert user input into double
            Double doubleData = Double.parseDouble(costOfData);
            Double doubleUtility = Double.parseDouble(costOfUtility);
            costDATA.add(doubleData); //add the double costs to each resource arraylist
            costWLAN.add(doubleWLAN);
            costUTILITY.add(doubleUtility);
            servicenames.add(namesOfService);
            dimensions.add(namesOfService);
            namesAdapter.notifyDataSetChanged();
            dataAdapter.notifyDataSetChanged();
            wlanAdapter.notifyDataSetChanged();
            utilityAdapter.notifyDataSetChanged();
            name.setText(""); //empty the edit text fields when button is clicked
            wlan.setText("");
            data.setText("");
            utility.setText("");
        }
    });
}
public void nextButton(View view) //next button, onto the next activity
{
    Intent intent = new Intent(MainActivity.this, ParticleActivity.class);
    startActivity(intent);
}
public int getDimensions(){ return dimensions.size();}
public ArrayList<String> getElements(){ return servicenames;}
public ArrayList<Double> getCostDATA(){;return costDATA;}
public ArrayList<Double> getCostWLAN(){return costUTILITY;}
public ArrayList<Double> getCostUTILITY(){return costUTILITY;}

}

您可以看到,使用getters和setters可以访问用户输入的阵列列表,而不是设置Arraylist static(我以前做过(。我在另一个名为Customusecase和customservice的类中访问这些阵列列表。这是Customusecase的代码:

public class CustomUseCase extends Test {
MainActivity mainActivity = new MainActivity();
ParticleActivity particleActivity = new ParticleActivity();
private int numberOfDimensions = mainActivity.getDimensions();
private ArrayList<Double> costData = mainActivity.getCostDATA(); //costs that the user enters for each resource
private ArrayList<Double> costWlan = mainActivity.getCostWLAN();
private ArrayList<Double> costUtilities = mainActivity.getCostUTILITY();
private double batteryCost = particleActivity.getBatteryCost();
private int maxIter;
private int noParticles;

public CustomUseCase(int noParticles, int maxIterations) {
    this.noParticles = noParticles;
    this.maxIter = maxIterations;
}
@Override
public double[] test() {
    long max = 10000; //maximum number of iterations, override //2 bits for the WLAN/DATA and the rest for the amount of services the user inputs
    double[] results = new double[numberOfDimensions]; //new array of results with numOfBits as number of elements
    for (int i = 1; i <= max; i++) {
        BinaryPso bpso = new BinaryPso(noParticles,
                numberOfDimensions);
        ParticleActivity getUserInput = new ParticleActivity();
        CustomService customService =
                new CustomService(batteryCost, costData, costWlan, costUtilities);
        long start = System.currentTimeMillis(); //start time
        bpso.setSolution(getUserInput.getUserSolution()); //changed this to user selection
        bpso.optimize(maxIter, customService, true);
        this.found += (bpso.getFound() ? 1 : 0);
        this.iterations += bpso.getSolIterations(); //use the method in bpso to get number of iterations taken
        long end = System.currentTimeMillis() - start; //end time minus start time
        this.sumTimes += end; //override the time spent variable
        System.out.println("P-value: " + Particle.getValue(Particle.bestGlobal()));
        System.out.println("P-bitCombo: " + Arrays.toString(Particle.bestGlobal()));
        System.out.println("P-goodness: " + customService.getGoodness(Particle.bestGlobal()));
    }
    System.out.println("Time: " + sumTimes / max);
    System.out.println("Iterations: " + iterations / max);
    System.out.println("Success Rate: " + found);
    boolean[] bestCombo = Particle.bestGlobal();
    for (Boolean b : bestCombo) {
        System.out.print(b + " ");
    }
    System.out.println();
    results[0] = sumTimes / max;
    results[1] = iterations / max;
    results[2] = found;
    return results;
}
public static List<Boolean> getBestComboArray() { //method to get best global array
    boolean[] bestCombo = Particle.bestGlobal(); //calculate best global
    List<Boolean> bestCombi = new ArrayList<>(bestCombo.length);
    for (int x = 0; x < bestCombo.length; x++) {
            bestCombi.add(bestCombo[x]);
        }
    return bestCombi;
}
}

这是我的海关服务类:

public class CustomService implements Goodness {
MainActivity mainActivity = new MainActivity();
private int numOfDimensions = mainActivity.getDimensions();
private ArrayList<String> serviceNames = mainActivity.getElements();
private ArrayList<Double> costData = mainActivity.getCostDATA();
private ArrayList<Double> costWlan = mainActivity.getCostWLAN();
private ArrayList<Double> costUtilities = mainActivity.getCostUTILITY();
private double batteryCost;
public void setBatteryCost(double batteryCost) {
    this.batteryCost = batteryCost;
}
public CustomService(double batteryCost, ArrayList<Double> costData, ArrayList<Double> costWlan,
                     ArrayList<Double> costUtilities) {
    if (costUtilities == null || costUtilities.size() < 1 || costData.size() < 1 || costWlan.size() < 1) {
        throw new RuntimeException("Please add atleast 1 cost to Data, WLAN and Utility");
    }

    this.batteryCost = batteryCost; //make sure you add battery field to UI, user enters battery level
    this.costData = costData;
    this.costWlan = costWlan;
    this.costUtilities = costUtilities;
}

public double getGoodness(boolean[] bits) {
    double utility = 0.0;
    double rcost = 0.0;
    ArrayList<Double> resourceCost = new ArrayList<Double>();
    Collections.sort(costUtilities); //sort the costUtilities arraylist
    double maxValue = Collections.max(costUtilities); //get the maximum value from the costUtilities arraylist

    if(bits[0] && bits[1]){
        return -500;
    }
    if(!bits[0] || bits[1]){
        return -1000;
    }
    for(int x = 1; x < numOfDimensions; x++){
        if(bits[x] == costUtilities.contains(maxValue)){
            return -1900;
        }
    }
    if (bits[0]) {
        resourceCost = costData;
    } else if (bits[1]) {
        resourceCost = costWlan;
    }
    for (int i = 2; i <= serviceNames.size(); i++) { //if i = 2, 2<=4
        if (bits[i]) {
            utility += costUtilities.get(i-2);
            rcost   += resourceCost.get(i-2);
        }
    }
    if (rcost < batteryCost) {
        return utility;
    }
    return utility * 0.50;
}
}

您无法在Noniuthread上更新UI项目。在Google上搜索RunonuitHread的用法。在RunonuitHread((中调用您的方法。

@Override
protected Void doInBackground(Void... params) { //sort this out
     runOnUiThread (new Runnable() { 
     public void run() {
          results = runTest("CustomUseCase");
         }
     }
    return null;
}

这解释了全部:"无法创建未称为looper.prepare((的线程中的处理程序" >没有循环的处理程序。

在辅助线程中,处理程序应该像这样

....

      Looper.prepare();
      mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
               // do work with received messages
                }
           };
       Looper.loop();

....

更多信息:Looper的目的是什么?

相关内容

  • 没有找到相关文章

最新更新