有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何纠正“java.lang.IllegalStateException:无法为安卓执行方法:onClick”?

logcat中的错误包括:-

    2019-02-26 17:19:49.253 31623-31623/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2019-02-26 17:20:20.717 31623-31623/com.ms.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mohitsharma.myapplication, PID: 31623
    java.lang.IllegalStateException: Could not execute method for 安卓:onClick
        at 安卓.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
        at 安卓.view.View.performClick(View.java:6897)
        at 安卓.widget.TextView.performClick(TextView.java:12693)
        at 安卓.view.View$PerformClick.run(View.java:26101)
        at 安卓.os.Handler.handleCallback(Handler.java:789)
        at 安卓.os.Handler.dispatchMessage(Handler.java:98)
        at 安卓.os.Looper.loop(Looper.java:164)
        at 安卓.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.安卓.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at 安卓.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
        at 安卓.view.View.performClick(View.java:6897) 
        at 安卓.widget.TextView.performClick(TextView.java:12693) 
        at 安卓.view.View$PerformClick.run(View.java:26101) 
        at 安卓.os.Handler.handleCallback(Handler.java:789) 
        at 安卓.os.Handler.dispatchMessage(Handler.java:98) 
        at 安卓.os.Looper.loop(Looper.java:164) 
        at 安卓.app.ActivityThread.main(ActivityThread.java:6944) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.安卓.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
     Caused by: java.lang.ArrayIndexOutOfBoundsException: length=7; index=7
        at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:318)
        at com.mohitsharma.myapplication.MainActivity.onButtonClick1(MainActivity.java:62)
        at java.lang.reflect.Method.invoke(Native Method) 
        at 安卓.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384) 
        at 安卓.view.View.performClick(View.java:6897) 
        at 安卓.widget.TextView.performClick(TextView.java:12693) 
        at 安卓.view.View$PerformClick.run(View.java:26101) 
        at 安卓.os.Handler.handleCallback(Handler.java:789) 
        at 安卓.os.Handler.dispatchMessage(Handler.java:98) 
        at 安卓.os.Looper.loop(Looper.java:164) 
        at 安卓.app.ActivityThread.main(ActivityThread.java:6944) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.安卓.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

我的代码是:-

package com.mohitsharma.myapplication;

import 安卓.content.res.AssetManager;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.EditText;
import 安卓.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onButtonClick(View v)
    {
        EditText e1=(EditText)findViewById(R.id.editText);  // volume of the part
        EditText e2=(EditText)findViewById(R.id.editText2); //Cost of the part
        EditText e3=(EditText)findViewById(R.id.editText3); //Lead time
        EditText e4=(EditText)findViewById(R.id.editText4); //Tolerance
        EditText e5=(EditText)findViewById(R.id.editText5); //Number of parts
        EditText e6=(EditText)findViewById(R.id.editText6);
        TextView t1=(TextView)findViewById(R.id.textView);
        double volume=Double.parseDouble(e1.getText().toString());
        double conventional_cost=Double.parseDouble(e2.getText().toString());
        double lead_time=Double.parseDouble(e3.getText().toString());
        double tolerance=Double.parseDouble(e4.getText().toString());
        double num_parts=Double.parseDouble(e5.getText().toString());
        String material=e6.getText().toString();
        AssetManager am=getAssets();
        Workbook wb =null;
        try (InputStream is = am.open("Printer Specification.xls")) {
            wb = Workbook.getWorkbook(is);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
        assert wb != null;
        Sheet s=wb.getSheet(0);
        int row=s.getRows();
        int[] x=new int[2];
        List<Integer> possible_printers = new ArrayList<>();
        List<Integer> possible_printers_2 = new ArrayList<>();
        List<Integer> possible_printers_3 = new ArrayList<>();
        int count=0;
        for(int i=1;i<row;i++)
        {
            count=0;
            Cell c=s.getCell(i,1);  //Checking for print volume vs part volume
            String st=c.getContents();
            int length=st.length();
            for(int j=0;j<length;j++)
            {
                if(st.charAt(j)=='x')
                {
                    x[count]=j;     // Finding the location of x in volume of the printer
                    count++;
                }
            }
            if(x[1]!=0)
            {
                double l= Double.parseDouble(st.substring(0,x[0]-1));
                double b= Double.parseDouble(st.substring(x[0]+2,x[1]-1));      //Checking for the cartesian printers volume
                double h=Double.parseDouble(st.substring(x[1]+2));
                double vol=l*b*h;
                if(vol>=volume)
                {
                    possible_printers.add(i);
                }
            }
            else
            {
                double d=Double.parseDouble(st.substring(0,x[0]-2));
                double h=Double.parseDouble(st.substring(x[0]+2,length-1));     // Checking for the volume of the delta printer
                double vol=Math.PI*Math.pow(d,2)*h;
                if (vol>=volume)
                {
                    possible_printers.add(i);
                }
            }

        }
        int[] y=new int[2];
        count=0;
        if (possible_printers.size()>0)     // Checking for the build rate
        {
            for(int i=0;i<possible_printers.size();i++)
            {
                Cell c=s.getCell(possible_printers.get(i),4);
                String st=c.getContents();
                if(!st.equals(""))
                {
                    for(int j=0;j<st.length();j++)
                    {
                        if (st.charAt(j)=='c')
                        {
                            y[count]=j;     //Finding the location of cm3/hr in build rate
                            //count++;
                        }
                    }
                    double build_rate=Double.parseDouble(st.substring(0,y[0]-1));
                    double print_time=(volume/1000)/build_rate;
                    if (lead_time>=print_time)      // Comparing the printing time vs the lead time
                    {
                        possible_printers_2.add(possible_printers.get(i));
                    }
                }
                //count=0;
            }
        }
        else
        {
            String print="The part cannot be 3D printed as the part volume is too big";
            t1.setText(print);
            return;         // Display that there are no suitable printers to print your part due to build volume
        }

        if (possible_printers_2.size()>0)       // Check for whether the required tolerance is achieved using printing
        {
            for(int i=0;i<possible_printers_2.size();i++)
            {
                Cell c=s.getCell(possible_printers_2.get(i),3);
                String st=c.getContents();
                double tol=Double.parseDouble(st);
                if (tol<=tolerance)
                {
                    possible_printers_3.add(possible_printers_2.get(i));
                }
            }
        }
        else
        {
            String print="The part cannot be 3D printed as printing time is too big";
            t1.setText(print);
            return;     //Display that the given part is not printable due to time issues
        }
        if(possible_printers_3.size()==0)
        {
            String print="The part cannot be 3D printed as the required tolerance cannot be achieved";
            t1.setText(print);
            return;     //Display that the part is not printable as the possible tolerance cannot be achieved by printing
        }

        // COST OF PRINTING

        double cost=0;
        Workbook wb2 = null;
        try (InputStream is2 = am.open("Cost of material.xls")) {
            wb2 = Workbook.getWorkbook(is2);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
        assert wb2 != null;
        Sheet s2=wb2.getSheet(0);
        int row2=s2.getRows();
        Workbook wb3=null;
        try (InputStream is3 = am.open("Material densities.xls")) {
            wb3 = Workbook.getWorkbook(is3);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
        assert wb3 != null;
        Sheet s3=wb3.getSheet(0);
        int row3=s3.getRows();
        double density=0;
        for(int i=0;i<row3;i++)
        {
            Cell c=s3.getCell(i,0);
            String mat=c.getContents();
            if(mat.equalsIgnoreCase(material))
            {
                Cell c2=s3.getCell(i,1);
                density=Double.parseDouble(c2.getContents());
                break;
            }
        }
        for(int i=1;i<row2;i++)
        {
            Cell c=s2.getCell(i,0);     //Getting the material name
            String mat=c.getContents();
            if(mat.equalsIgnoreCase(material))
            {
                Cell c2=s2.getCell(i,1);        //Getting the filament diameter
                double dia=Double.parseDouble(c2.getContents());
                if(dia==3)
                {
                    Cell c3=s2.getCell(i,2);
                    cost=(volume*density/Math.pow(10,9))*Double.parseDouble(c3.getContents())*70.99*num_parts;
                }
            }
        }
        if(cost<=conventional_cost)
        {
            String print="The part is 3D printable as the printing cost "+cost+" is less than conventional cost "+conventional_cost;
            t1.setText(print);//Display that the part is printable as cost of printing is suitable
        }
        else
        {
            String print="The part is not 3D printable as the printing cost "+cost+" is more than conventional cost "+conventional_cost;
            t1.setText(print);
            return;// Display that part is not printable due to cost issues
        }
        for(int i=0;i<possible_printers_3.size();i++)
        {
            Cell c=s.getCell(possible_printers_3.get(i),0);
            String printer=c.getContents();
            printer=printer+"\n";
            t1.setText(printer);
        }
    }
}

代码和错误信息可以在上面看到。代码将用户输入的一些数据与应用程序代码中资产文件夹中的excel文件中的数据进行比较。jxl用于读取和写入此代码中的文件


共 (1) 个答案

  1. # 1 楼答案

    您在活动的第62行收到ArrayIndexOutOfBoundsException。第62行是这一行:

    Cell c=s.getCell(i,1); //Checking for print volume vs part volume

    在这行设置Breakpooint并调试应用程序。您希望访问阵列中的位置7,但其长度仅为7

    也许你的int row=s.getRows();应该是int row=s.getRows() - 1;(第53行)