双击edittext字段崩溃并重新启动应用程序

时间:2013-01-24 10:37:14

标签: android android-layout android-edittext

我的错误如下所示

 01-24 10:07:51.053: E/AndroidRuntime(809): FATAL EXCEPTION: main
01-24 10:07:51.053: E/AndroidRuntime(809): java.lang.IllegalStateException: Could not find a method res/drawable-hdpi/textfield_activated_holo_light.9.png(View) in the activity class com.example.iolcalci.Selection for onClick handler on view class android.widget.EditText with id 'k1_editText'
01-24 10:07:51.053: E/AndroidRuntime(809):  at android.view.View$1.onClick(View.java:3584)
01-24 10:07:51.053: E/AndroidRuntime(809):  at android.view.View.performClick(View.java:4202)
01-24 10:07:51.053: E/AndroidRuntime(809):  at android.view.View$PerformClick.run(View.java:17340)
01-24 10:07:51.053: E/AndroidRuntime(809):  at android.os.Handler.handleCallback(Handler.java:725)
01-24 10:07:51.053: E/AndroidRuntime(809):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-24 10:07:51.053: E/AndroidRuntime(809):  at android.os.Looper.loop(Looper.java:137)
01-24 10:07:51.053: E/AndroidRuntime(809):  at android.app.ActivityThread.main(ActivityThread.java:5039)
01-24 10:07:51.053: E/AndroidRuntime(809):  at java.lang.reflect.Method.invokeNative(Native Method)
01-24 10:07:51.053: E/AndroidRuntime(809):  at java.lang.reflect.Method.invoke(Method.java:511)
01-24 10:07:51.053: E/AndroidRuntime(809):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-24 10:07:51.053: E/AndroidRuntime(809):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-24 10:07:51.053: E/AndroidRuntime(809):  at dalvik.system.NativeStart.main(Native Method)
01-24 10:07:51.053: E/AndroidRuntime(809): Caused by: java.lang.NoSuchMethodException: res/drawable-hdpi/textfield_activated_holo_light.9.png [class android.view.View]
01-24 10:07:51.053: E/AndroidRuntime(809):  at java.lang.Class.getConstructorOrMethod(Class.java:460)
01-24 10:07:51.053: E/AndroidRuntime(809):  at java.lang.Class.getMethod(Class.java:915)
01-24 10:07:51.053: E/AndroidRuntime(809):  at android.view.View$1.onClick(View.java:3577)
01-24 10:07:51.053: E/AndroidRuntime(809):  ... 11 more

请帮助我,我是android开发的初学者。我不认为代码中有错误。

我的代码在这里

public class Selection extends Activity {
    private EditText k1_e,k2_e,al_e,alconst_e,dr_e;
    private float k1,k2,al,al_const,dr,Avg_k,IOLPower;
    private Spinner spin;
    private Button result;
    private int spinSelected=-1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.selective);
}

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        spin=(Spinner)findViewById(R.id.formulae);
        spin.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos,
                    long id) {
                // TODO Auto-generated method stub
                spinSelected=pos;

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
                spinSelected=-1;
            }
        });
        result=(Button)findViewById(R.id.result);
        result.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int flag = 0;
                String op="INVALID INPUT";
                dr_e=(EditText)findViewById(R.id.dr_editText);
                k1_e=(EditText)findViewById(R.id.k1_editText);
                k2_e=(EditText)findViewById(R.id.k2_editText);
                al_e=(EditText)findViewById(R.id.al_editText);
                alconst_e=(EditText)findViewById(R.id.al_const_editText);
                k1_e.isClickable();
                k2_e.isClickable();
                al_e.isClickable();
                alconst_e.isClickable();
                if(dr_e.getText().toString().length()==0|k1_e.getText().toString().length()==0|k2_e.getText().toString().length()==0|al_e.getText().toString().length()==0|alconst_e.getText().toString().length()==0){
                    flag=1;
                }else{

                dr=Float.parseFloat(dr_e.getText().toString());
                k1=Float.parseFloat(k1_e.getText().toString());
                k2=Float.parseFloat(k2_e.getText().toString());
                al=Float.parseFloat(al_e.getText().toString());
                al_const=Float.parseFloat(alconst_e.getText().toString()); 
                }

                Avg_k=(k1+k2)/2;
                if(spinSelected==0){
                IOLPower=Srkt();    
                Intent iSrkt=new Intent(Selection.this,Srkt.class);
                if(flag!=1){
                iSrkt.putExtra("RESULT", IOLPower);
                }else{
                    iSrkt.putExtra("FLAG", flag);
                    iSrkt.putExtra("RESULT", op);
                }
                startActivity(iSrkt);
            }else if(spinSelected==1){
                IOLPower=Binkhorst();
                Intent iBinkhorst=new Intent(Selection.this,Binkhorst.class);
                if(flag!=1){
                    iBinkhorst.putExtra("RESULT", IOLPower);
                }else{
                    iBinkhorst.putExtra("FLAG", flag);
                    iBinkhorst.putExtra("RESULT", op);
                }
                startActivity(iBinkhorst);
            }else if(spinSelected==2){
            IOLPower=Srk2();
            Intent iSrk2=new Intent(Selection.this,Srk2.class);
            if(flag!=1){
                iSrk2.putExtra("RESULT", IOLPower);
            }else{
                iSrk2.putExtra("FLAG", flag);
                iSrk2.putExtra("RESULT", op);
            }
            startActivity(iSrk2);
            }else{
                IOLPower=Holladay();
                Intent iHolladay=new Intent(Selection.this,Holladay.class);
                if(flag!=1){
                    iHolladay.putExtra("RESULT", IOLPower);
                }else{
                    iHolladay.putExtra("FLAG", flag);
                    iHolladay.putExtra("RESULT", op);
                }
                startActivity(iHolladay);
            }
            }
            });
    }
    public static float Round(float Rval, int Rpl) {
          float p = (float)Math.pow(10,Rpl);
          Rval = Rval * p;
          float tmp = Math.round(Rval);
          return (float)tmp/p;
          }
    public float Srkt(){
         float Rcor;
         float Lcor;
         float Crwdest;
         float Corneal_H;
         float Acd_Const;
         float Offset;
         float Acd_Est;
         float Na=(float) 1.336;
         float C2=(float) 0.3333;
         float C3;
         float C4;
         float C5;
         float C6;
         float C8;
         float C9;
         float Iolam;


         float Rcor1=(float)(337.5/Avg_k);Rcor=Round(Rcor1,2);

            if(al<=24.2){
                Lcor=al;
            }else{
                Lcor=(float) (-3.446+1.716*al-0.0237*(al*al));
            }Lcor=Round(Lcor,2);
            Crwdest=(float) (-5.41+0.58412*Lcor+0.098*Avg_k);Crwdest=Round(Crwdest,2);
            Corneal_H=(float) (Rcor-(Math.sqrt(Rcor*Rcor-Crwdest*Crwdest/4)));Corneal_H=Round(Corneal_H,2);
            Acd_Const=(float) (0.62467*al_const-68.747);
            Offset=(float) (Acd_Const-3.336);Offset=Round(Offset,2);
            Acd_Est=(Corneal_H+Offset); float Acd_Est1=Round(Acd_Est,2);
            C3=(float) (0.97971*al+0.65696);C3=Round(C3,2);     
            C4=C3-Acd_Est1;C4=Round(C4,2);
            C5=(float) ((Na*Rcor)-(C2*Acd_Est)); C5=Round(C5, 2);
            C6=(float) ((Na*Rcor1)-(C2*C3));
            C8=(float) ((12*C6)+(C3*Rcor1));C8=Round(C8,2);
            C9=(float) ((12*C5)+(Acd_Est*Rcor1));C9=Round(C9,2);
            Iolam=(float) ((1336*(C6-(0.001*C8*dr)))/(C4*(C5-(0.001*dr*C9))));Iolam=Round(Iolam,2);
            Log.i("value",String.valueOf(Avg_k));
            Log.i("value",String.valueOf(Rcor));
            Log.i("value",String.valueOf(Lcor));
            Log.i("value",String.valueOf(Crwdest));
            Log.i("value",String.valueOf(Corneal_H));
            Log.i("value",String.valueOf(Acd_Const));
            Log.i("value",String.valueOf(Offset));
            Log.i("value",String.valueOf(Acd_Est));
            Log.i("value",String.valueOf(Na));
            Log.i("value",String.valueOf(C2));
            Log.i("value",String.valueOf(C3));
            Log.i("value",String.valueOf(C4));
            Log.i("value",String.valueOf(C5));
            Log.i("value",String.valueOf(C6));
            Log.i("value",String.valueOf(C8));
            Log.i("value",String.valueOf(C9));
            Log.i("value",String.valueOf(Iolam));
        return(Iolam);
    }
    public float Binkhorst(){
        float K1;
        float LB2;
        float ACDbnk;
        float xb;
        float yb;
        float em;

        K1=(float)(337.5/Avg_k);
        LB2=(float) (al+0.1984);
        if(LB2>=26){
            ACDbnk=(float) (((0.58357*al_const)-63.896)*1.1087);
        }else{
            ACDbnk=(float) (((0.58357*al_const)-63.896)*LB2/23.45);
        }
        xb=(float) (1336*((1.336*K1-0.3333*LB2)-0.001*dr*(16.032*K1-4*LB2+LB2*K1)));
        yb=(float) ((LB2-ACDbnk)*(1.336*K1-0.3333*ACDbnk-0.001*dr*(16.032*K1-4*ACDbnk+ACDbnk*K1)));
        em=xb/yb;em=Round(em,2);
        return(em);
    }
    public float Srk2(){
        float X25 = 0;
        float Avg_k=(k1+k2)/2;
        if(al<20.0){
            X25=al_const+3;
        }else if((al>=20.0)&&(al<21.0)){
            X25=al_const+2;
        }else if((al>=21.0)&&(al<22.0)){
            X25=al_const+1;
        }else if((al>=22.0)&&(al<24.5)){
            X25=al_const;
        }else if(al>=24.5){
            X25=(float) (al_const-(0.5));
        }else{
            Toast.makeText(getApplicationContext(), "Please enter valid AL value", Toast.LENGTH_SHORT).show();
        }
        float SRK2 = (float) (X25-(0.9*Avg_k+2.5*al));
        return(SRK2);
    }
    public float Holladay(){
        float K;
        float Lhol;
        float SF;
        float Rag;
        float AGx;
        float AG;
        float ACDH;
        float CAhol;
        float xh;
        float yh;

        float K1=(float)(337.5/Avg_k);K=Round(K1,2);
        Lhol=(float) (al+0.2);
        SF=(float) ((0.5663*al_const)-65.6);
        if(K<7){
            Rag=7;
        }else{
            Rag=K;
        }
        AGx=(float) (al*0.533);
        if(AGx>13.5){
             AG=(float) 13.5;
        }else{
            AG=AGx;
        }
        ACDH=(float) (0.56+Rag-(Math.sqrt(Rag*Rag-AG*AG/4)));
        CAhol=ACDH+SF;
        xh=(float) (1336*((1.336*K1-0.3333*Lhol)-0.001*dr*(16.032*K1-4*Lhol+Lhol*K1)));
        yh=(float) ((Lhol-CAhol)*(1.336*K1-0.3333*CAhol-0.001*dr*(16.032*K1-4*CAhol+CAhol*K1)));
        float em=xh/yh;em=Round(em,2);
        return(em);
    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        dr_e=(EditText)findViewById(R.id.dr_editText);
        k1_e=(EditText)findViewById(R.id.k1_editText);
        k2_e=(EditText)findViewById(R.id.k2_editText);
        al_e=(EditText)findViewById(R.id.al_editText);
        alconst_e=(EditText)findViewById(R.id.al_const_editText);
        dr_e.setText(null);
        k1_e.setText(null);
        k2_e.setText(null);
        al_e.setText(null);
        alconst_e.setText(null);
        dr_e.requestFocus();
    }

}

xml文件到此处

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:orientation="vertical" 
    android:id="@+id/form_layout" xmlns:android="http://schemas.android.com/apk/res/android">

    <Spinner
        android:id="@+id/formulae"
        android:layout_width="400dp"
        android:layout_height="125dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@color/LightSkyBlue"
        android:dropDownWidth="wrap_content"
        android:entries="@array/formulas"
        android:gravity="center"
        android:paddingLeft="0dp"
        android:textAlignment="center" />

    <EditText
        android:id="@+id/k1_editText"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/k1"
        android:layout_alignLeft="@+id/al_const_editText"
        android:layout_alignTop="@+id/k1"
        android:background="@color/black"
        android:ems="10"
        android:enabled="true"
        android:focusable="true"
        android:inputType="numberDecimal"
        android:textColor="@color/White"
        android:textSize="30sp"
        android:onClick="@drawable/textfield_activated_holo_light"
        android:visibility="visible" >
</EditText>

    <EditText
        android:id="@+id/k2_editText"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/k2"
        android:layout_alignLeft="@+id/k1_editText"
        android:layout_alignTop="@+id/k2"
        android:background="@color/black"
        android:ems="10"
        android:enabled="true"
        android:inputType="numberDecimal"
        android:onClick="@drawable/textfield_activated_holo_light"
        android:textSize="30sp"
        android:visibility="visible" >

    </EditText>

    <EditText
        android:id="@+id/al_editText"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/al"
        android:layout_alignLeft="@+id/al_const_editText"
        android:layout_alignTop="@+id/al"
        android:ems="10"
        android:enabled="true"
        android:inputType="numberDecimal"
        android:onClick="@drawable/textfield_activated_holo_light"
        android:textSize="30sp"
        android:visibility="visible" />

    <EditText
        android:id="@+id/al_const_editText"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/al_const"
        android:layout_alignTop="@+id/al_const"
        android:layout_marginLeft="21dp"
        android:layout_toRightOf="@+id/al_const"
        android:ems="10"
        android:enabled="true"
        android:inputType="numberDecimal"
        android:onClick="@drawable/textfield_activated_holo_light"
        android:textSize="30sp"
        android:visibility="visible" />

    <Button
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="28dp"
        android:background="@color/LightSkyBlue"
        android:gravity="center"
        android:height="100dp"
        android:maxWidth="80dp"
        android:minHeight="60dp"
        android:paddingLeft="150dp"
        android:paddingRight="200dp"
        android:text="@string/Result"
        android:textSize="60sp" />

    <TextView
        android:id="@+id/al_const"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/result"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="89dp"
        android:layout_marginLeft="50dp"
        android:text="@string/Rx"
        android:textColor="@color/White"
        android:textSize="60sp" />

    <TextView
        android:id="@+id/k2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/al"
        android:layout_alignLeft="@+id/al_const"
        android:layout_marginBottom="126dp"
        android:layout_marginLeft="23dp"
        android:text="@string/K2"
        android:textColor="@color/White"
        android:textSize="60sp" />

    <TextView
        android:id="@+id/al"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/al_const"
        android:layout_alignRight="@+id/k2"
        android:layout_marginBottom="125dp"
        android:text="@string/AL"
        android:textColor="@color/White"
        android:textSize="60sp" />

    <TextView
        android:id="@+id/k1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/k2"
        android:layout_alignLeft="@+id/al"
        android:layout_marginBottom="97dp"
        android:text="@string/K1"
        android:textColor="@color/white"
        android:textSize="60sp" />

    <EditText
        android:id="@+id/dr_editText"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/k1_editText"
        android:layout_below="@+id/formulae"
        android:layout_marginTop="58dp"
        android:ems="10"
        android:inputType="numberSigned|numberDecimal"
        android:paddingTop="35dp"
        android:textColor="@color/White"
        android:textSize="30sp" />

    <TextView
        android:id="@id/dr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/dr_editText"
        android:layout_alignLeft="@+id/k2"
        android:layout_alignTop="@+id/dr_editText"
        android:layout_toLeftOf="@+id/dr_editText"
        android:ems="10"
        android:paddingLeft="100dp"
        android:paddingTop="10dp"
        android:text="@string/dr"
        android:textColor="@color/White"
        android:textSize="50sp" />

</RelativeLayout>

我在drawable文件夹中有textfield_activated_holo_light.9.png。但为什么我会收到此错误?

3 个答案:

答案 0 :(得分:1)

从xml中删除它(在EditText中)

 android:onClick="@drawable/textfield_activated_holo_light"

就突出显示editext而言,您可以这样做:

1)定义一个xml文件,说mybackground.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true"
   android:drawable="@drawable/textfield_activated_holo_light" /> <!-- pressed -->
  <item android:state_focused="true"
   android:drawable="@drawable/textfield_activated_holo_light" /> <!-- focused -->
  <item android:drawable="@drawable/default" /> <!-- default -->
</selector>

2)将此xml复制到drawable文件夹

3)将此xml设置为xml文件中editext的背景:

  <EditText
        android:id="@+id/k1_editText"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/k1"
        android:layout_alignLeft="@+id/al_const_editText"
        android:layout_alignTop="@+id/k1"
        android:background="@drawable/mybackground"
        android:ems="10"
        android:enabled="true"
        android:focusable="true"
        android:inputType="numberDecimal"
        android:textColor="@color/White"
        android:textSize="30sp"            
        android:visibility="visible" >

答案 1 :(得分:0)

覆盖OnClickListener中的onStart(),然后在onClick(View v)中,设置v.setBackgroundResource(getResources().getDrawable(R.drawable.textfield_activated_holo_light)

答案 2 :(得分:0)

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int flag = 0;
                String op="INVALID INPUT";
                dr_e=(EditText)findViewById(R.id.dr_editText);
                k1_e=(EditText)findViewById(R.id.k1_editText);
                k2_e=(EditText)findViewById(R.id.k2_editText);
                al_e=(EditText)findViewById(R.id.al_editText);
                alconst_e=(EditText)findViewById(R.id.al_const_editText);
                k1_e.isClickable();
                k2_e.isClickable();
                al_e.isClickable();
                alconst_e.isClickable();
                k1_e.setOnClickListener(new View.OnClickListener(){                 
                @Override
                public void onClick(View arg0) 
                {
                    //put your action here
                }
            });                  
                k2_e.setOnClickListener(new View.OnClickListener(){                 
            @Override
            public void onClick(View arg0) 
            {
                //put your action here
            }
            });
     //and other EditText also  
       if(dr_e.getText().toString().length()==0|k1_e.getText().toString().length()==0|k2_e.getText().toString().length()==0|al_e.getText().toString().length()==0|alconst_e.getText().toString().length()==0){
                    flag=1;
                }else{

                dr=Float.parseFloat(dr_e.getText().toString());
                k1=Float.parseFloat(k1_e.getText().toString());
                k2=Float.parseFloat(k2_e.getText().toString());
                al=Float.parseFloat(al_e.getText().toString());
                al_const=Float.parseFloat(alconst_e.getText().toString()); 
                }

                Avg_k=(k1+k2)/2;
                if(spinSelected==0){
                IOLPower=Srkt();    
                Intent iSrkt=new Intent(Selection.this,Srkt.class);
                if(flag!=1){
                iSrkt.putExtra("RESULT", IOLPower);
                }else{
                    iSrkt.putExtra("FLAG", flag);
                    iSrkt.putExtra("RESULT", op);
                }
                startActivity(iSrkt);
            }else if(spinSelected==1){
                IOLPower=Binkhorst();
                Intent iBinkhorst=new Intent(Selection.this,Binkhorst.class);
                if(flag!=1){
                    iBinkhorst.putExtra("RESULT", IOLPower);
                }else{
                    iBinkhorst.putExtra("FLAG", flag);
                    iBinkhorst.putExtra("RESULT", op);
                }
                startActivity(iBinkhorst);
            }else if(spinSelected==2){
            IOLPower=Srk2();
            Intent iSrk2=new Intent(Selection.this,Srk2.class);
            if(flag!=1){
                iSrk2.putExtra("RESULT", IOLPower);
            }else{
                iSrk2.putExtra("FLAG", flag);
                iSrk2.putExtra("RESULT", op);
            }
            startActivity(iSrk2);
            }else{
                IOLPower=Holladay();
                Intent iHolladay=new Intent(Selection.this,Holladay.class);
                if(flag!=1){
                    iHolladay.putExtra("RESULT", IOLPower);
                }else{
                    iHolladay.putExtra("FLAG", flag);
                    iHolladay.putExtra("RESULT", op);
                }
                startActivity(iHolladay);
            }
            }
            });
    }