onClick按钮Android

时间:2016-05-13 08:12:09

标签: android eclipse button spinner

我是Android开发的初学者,因此我使用基于克单位转换的Eclipse创建了一个Android应用程序,但它无法正常工作。

我的Android应用程序的概念是我使用的导航抽屉有三个项目:Home(Accueil),Converter(Convertisseur)和About(A Propos)。 因此,当我点击转换器选项时,它必须显示包含克单位转换的片段。当我输入我的值进行转换并选择我想要的转换单位时,当我点击转换按钮(转换)时,应用程序将获得堆栈并自行关闭。

任何人都可以帮助我!

这是我的片段类:

    package com.hw.converter;

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class ConverterFragment extends Fragment {
    String[] units;
    Spinner spn1, spn2;
    EditText editValue;
    TextView tvResult;
    double value;
    Button myBtn;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_converter, container, false);

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getView().getContext(),
                R.array.units, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spn1 = (Spinner)getView().findViewById(R.id.spnUnits1);
        spn2 = (Spinner)getView().findViewById(R.id.spnUnits2);
        units = getResources().getStringArray(R.array.units);
        spn1.setAdapter(adapter);
        spn2.setAdapter(adapter);


    }
    public void convertir(View view){
        String edit = editValue.getText().toString();
        if(edit == null || edit.isEmpty()) {
          value = 0.0;
        } else {
          value = Double.parseDouble(edit);
        }
        tvResult = (TextView)getView().findViewById(R.id.tvResult);
        int choix1 = spn1.getSelectedItemPosition();
        int choix2 = spn2.getSelectedItemPosition();
        switch(choix1){
        case 0:{
            switch(choix2){
            case 0:
                tvResult.setText(value+" mg");break;
            case 1:
                value/=10;
                tvResult.setText(value+" cg");break;
            case 2:
                value/=100;
                tvResult.setText(value+" dg");break;
            case 3:
                value/=1000;
                tvResult.setText(value+" g");break;
            case 4:
                value/=10000;
                tvResult.setText(value+" dag");break;
            case 5:
                value/=100000;
                tvResult.setText(value+" hg");break;
            case 6:
                value/=1000000;
                tvResult.setText(value+" kg");break;
            default:
                tvResult.setText(value+" g");break;
            }break;}

        case 1:{
            switch(choix2){
            case 0:
                value*=10;
                tvResult.setText(value+" mg");break;
            case 1:
                editValue.setText(value+" cg");break;
            case 2:
                value/=10;
                tvResult.setText(value+" dg");break;
            case 3:
                value/=100;
                tvResult.setText(value+" g");break;
            case 4:
                value/=1000;
                tvResult.setText(value+" dag");break;
            case 5:
                value/=10000;
                tvResult.setText(value+" hg");break;
            case 6:
                value/=100000;
                tvResult.setText(value+" kg");break;
            default:
                tvResult.setText(value+" g");break;
            }break;
            }

        case 2:{
            switch(choix2){
            case 0:
                value*=100;
                tvResult.setText(value+" mg");break;
            case 1:
                value*=10;
                tvResult.setText(value+" cg");break;
            case 2:
                tvResult.setText(value+" dg");break;
            case 3:
                value/=10;
                tvResult.setText(value+" g");break;
            case 4:
                value/=100;
                tvResult.setText(value+" dag");break;
            case 5:
                value/=1000;
                tvResult.setText(value+" hg");break;
            case 6:
                value/=10000;
                tvResult.setText(value+" kg");break;
            default:
                tvResult.setText(value+" g");break;
            }break;
        }

        case 3:{
            switch(choix2){
            case 0:
                value*=1000;
                tvResult.setText(value+" mg");break;
            case 1:
                value*=100;
                tvResult.setText(value+" cg");break;
            case 2:
                value*=10;
                tvResult.setText(value+" dg");break;
            case 3:
                tvResult.setText(value+" g");break;
            case 4:
                value/=10;
                tvResult.setText(value+" dag");break;
            case 5:
                value/=100;
                tvResult.setText(value+" hg");break;
            case 6:
                value/=1000;
                tvResult.setText(value+" kg");break;
            default:
                tvResult.setText(value+" g");break;
            }break;
            }
        case 4:
        {switch(choix2){
        case 0:
            value*=10000;
            tvResult.setText(value+" mg");break;
        case 1:
            value*=1000;
            tvResult.setText(value+" cg");break;
        case 2:
            value*=100;
            tvResult.setText(value+" dg");break;
        case 3:
            value*=10;
            tvResult.setText(value+" g");break;
        case 4:
            tvResult.setText(value+" dag");break;
        case 5:
            value/=10;
            tvResult.setText(value+" hg");break;
        case 6:
            value/=100;
            editValue.setText(value+" kg");break;
        default:
            tvResult.setText(value+" g");break;
        }break;
        }
        case 5:{
            switch(choix2){
            case 0:
                value*=100000;
                tvResult.setText(value+" mg");break;
            case 1:
                value*=10000;
                tvResult.setText(value+" cg");break;
            case 2:
                value*=1000;
                tvResult.setText(value+" dg");break;
            case 3:
                value*=100;
                tvResult.setText(value+" g");break;
            case 4:
                value*=10;
                tvResult.setText(value+" dag");break;
            case 5:
                tvResult.setText(value+" hg");break;
            case 6:
                value/=10;
                tvResult.setText(value+" kg");break;
            default:
                tvResult.setText(value+" g");break;
            }break;
        }
        case 6:{
            switch(choix2){
            case 0:
                value*=1000000;
                tvResult.setText(value+" mg");break;
            case 1:
                value*=100000;
                tvResult.setText(value+" cg");break;
            case 2:
                value*=10000;
                tvResult.setText(value+" dg");break;
            case 3:
                value*=1000;
                tvResult.setText(value+" g");break;
            case 4:
                value*=100;
                tvResult.setText(value+" dag");break;
            case 5:
                value*=10;
                tvResult.setText(value+" hg");break;
            case 6:
                tvResult.setText(value+" kg");break;
            default:
                tvResult.setText(value+" g");break;
            }break;

        }

        }
    }


}

这是我的转换器布局代码:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.hw.converter.ConverterActivity" >

   <ListView android:id="@+id/drawerList"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:entries="@array/mymenu"
        android:layout_gravity="start">

   </ListView>

    <RelativeLayout android:id="@+id/convertContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

       <TextView
           android:id="@+id/tvTitle"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentTop="true"
           android:layout_centerHorizontal="true"
           android:text="@string/tvTitle"
           android:textAppearance="?android:attr/textAppearanceLarge" />
        <TextView 
          android:id="@+id/tv1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentLeft="true"
           android:layout_alignParentStart="true"
           android:paddingTop="25dp"
           android:layout_below="@id/tvTitle"
           android:text="@string/tv1"
           android:textAppearance="?android:attr/textAppearanceLarge" />

         <Spinner
             android:id="@+id/spnUnits1"
             android:layout_width="100dp"
             android:layout_height="wrap_content"
             android:layout_alignTop="@+id/tv1"
             android:layout_marginLeft="41dp"
             android:layout_marginStart="41dp"
             android:layout_marginTop="30dp"
             android:layout_toEndOf="@+id/tv1"
             android:layout_toRightOf="@+id/tv1"

              />

        <EditText
            android:id="@+id/editQte"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_below="@+id/tv1"
            android:inputType="number"
            android:hint="@string/editHint"
            >

        </EditText>
        <Spinner
            android:id="@+id/spnUnits2"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/tv2"
            android:layout_below="@+id/editQte"
           android:layout_marginLeft="41dp"
           android:layout_marginStart="41dp"
           android:layout_marginTop="30dp"
           android:layout_toEndOf="@+id/tv2"
           android:layout_toRightOf="@+id/tv2"
            />

         <TextView 
          android:id="@+id/tv2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentLeft="true"
           android:layout_alignParentStart="true"
           android:paddingTop="25dp"
           android:layout_marginTop="20dp"
           android:layout_below="@id/tv1"
           android:text="@string/tv2"
           android:textAppearance="?android:attr/textAppearanceLarge"
          />

         <Button
            android:id="@+id/btnConvert"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/spnUnits2"
            android:layout_marginTop="56dp"
            android:layout_centerInParent="true"
            android:text="@string/btnConvert"
            android:onClick="com.hw.converter.ConverterFragment.convertir" />

         <TextView
            android:id="@+id/tvResult"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btnConvert"
            android:layout_marginTop="24dp"
            android:text="@string/tvResult"
            android:layout_marginLeft="20dp"
            android:layout_marginStart="20dp"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>



</android.support.v4.widget.DrawerLayout>

修改:现在错误日志有意义,请帮忙! 这是我改变之后得到的错误日志:

05-13 10:30:29.972: E/AndroidRuntime(5621): FATAL EXCEPTION: main
05-13 10:30:29.972: E/AndroidRuntime(5621): Process: com.hw.converter, PID: 5621
05-13 10:30:29.972: E/AndroidRuntime(5621): java.lang.IllegalStateException: Could not find a method com.hw.converter.ConverterFragment.convertir(View) in the activity class com.hw.converter.MainActivity for onClick handler on view class android.widget.Button with id 'btnConvert'
05-13 10:30:29.972: E/AndroidRuntime(5621):     at android.view.View$1.onClick(View.java:3864)
05-13 10:30:29.972: E/AndroidRuntime(5621):     at android.view.View.performClick(View.java:4492)
05-13 10:30:29.972: E/AndroidRuntime(5621):     at android.view.View$PerformClick.run(View.java:18568)
05-13 10:30:29.972: E/AndroidRuntime(5621):     at android.os.Handler.handleCallback(Handler.java:733)
05-13 10:30:29.972: E/AndroidRuntime(5621):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-13 10:30:29.972: E/AndroidRuntime(5621):     at android.os.Looper.loop(Looper.java:136)
05-13 10:30:29.972: E/AndroidRuntime(5621):     at android.app.ActivityThread.main(ActivityThread.java:5021)
05-13 10:30:29.972: E/AndroidRuntime(5621):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 10:30:29.972: E/AndroidRuntime(5621):     at java.lang.reflect.Method.invoke(Method.java:515)
05-13 10:30:29.972: E/AndroidRuntime(5621):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
05-13 10:30:29.972: E/AndroidRuntime(5621):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
05-13 10:30:29.972: E/AndroidRuntime(5621):     at dalvik.system.NativeStart.main(Native Method)
05-13 10:30:29.972: E/AndroidRuntime(5621): Caused by: java.lang.NoSuchMethodException: com.hw.converter.ConverterFragment.convertir [class android.view.View]
05-13 10:30:29.972: E/AndroidRuntime(5621):     at java.lang.Class.getConstructorOrMethod(Class.java:472)
05-13 10:30:29.972: E/AndroidRuntime(5621):     at java.lang.Class.getMethod(Class.java:857)
05-13 10:30:29.972: E/AndroidRuntime(5621):     at android.view.View$1.onClick(View.java:3857)
05-13 10:30:29.972: E/AndroidRuntime(5621):     ... 11 more

2 个答案:

答案 0 :(得分:3)

it4Astuces,

按钮的onClick操作的默认签名应为

public void NameOfTheMethod(View view) {
}

所以改变你的

public void convertir(View view){
}

你还没有粘贴你的错误日志:)虽然我相信它必须说没有找到'convertir'方法。这是因为android会查找将视图作为参数的方法convertir:)

public void convertir(View view)与public void convertir()不同。所以更新你的方法以匹配android的要求,你应该很高兴:)

修改 根据您在评论中发布的错误日志,我认为崩溃的原因是

value = Double.parseDouble(editValue.getText().toString());

虽然我不能确定你发布的错误日志至少对我来说没有多大意义:(你可以改变你的代码如下并且知道它是否能解决你的问题

String editValue = editValue.getText().toString();
if(editValue == null || editValue.isEmpty()) {
  value = 0.0;
} else {
  value = Double.parseDouble(editValue);
}

在JAVA Double.parseDouble中,空值或空值不被视为parceable

问题应该解决:)快乐编码

答案 1 :(得分:1)

xml文件

       <Button
        android:id="@+id/btnConvert"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spnUnits2"
        android:layout_marginTop="56dp"
        android:layout_centerInParent="true"
        android:text="@string/btnConvert"
        android:onClick="onClick" />

您的主文件

public class ConverterFragment extends Fragment {
String[] units;
Spinner spn1, spn2;
EditText editValue;
TextView tvResult;
double value;
Button myBtn;

.....
.....


public void onClick(View view) 
{

//action you want on button clicked   
}

}