0

我的应用程序运行良好,直到我进行了一些更改以提高可用性。现在它总是显示这个错误: ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2460

这是我的代码:

package de.nibor.theconverter;
public class MainActivity extends Activity implements OnItemSelectedListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);

//Gets all resources you need to get the input measure, calculate and printout the converted measure
final Button convertButton = (Button) findViewById(R.id.convertButton); 
final EditText inputEditText = (EditText) findViewById(R.id.inputEditText);
final TextView hintTextView1 = (TextView) findViewById(R.id.hintTextView1);
final TextView hintTextView2 = (TextView) findViewById(R.id.hintTextView2);
final TextView outputTextView1 = (TextView) findViewById(R.id.outputTextView1);
final TextView outputTextView2 = (TextView) findViewById(R.id.outputTextView2);
final CharSequence inches = getResources().getString(R.string.inches);
final CharSequence cm = getResources().getString(R.string.cm);
final CharSequence feet = getResources().getString(R.string.feet);
final CharSequence cm_or_inch_hint_inputEditText = getResources().getString(R.string.cm_or_inch_hint_inputEditText);
final CharSequence cm_to_inch_hintTextView1 = getResources().getString(R.string.cm_to_inch_hintTextView1);
final CharSequence inch_to_cm_hintTextView2 = getResources().getString(R.string.inch_to_cm_hintTextView2);
final CharSequence are = getResources().getString(R.string.are);
final String measure_string = inputEditText.getText().toString();
final Double measure_double = Double.parseDouble(measure_string);



final Spinner measure_unit_spinner = (Spinner) findViewById(R.id.measure_unit_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.measure_units, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
measure_unit_spinner.setAdapter(adapter);
OnItemSelectedListener listener = new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
        if(measure_unit_spinner.getSelectedItemPosition() == 0){
            inputEditText.setHint(cm_or_inch_hint_inputEditText);

            convertButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    printoutConvertedMeasure1();
                    printoutConvertedMeasure2();
                }
                private void printoutConvertedMeasure1() {

                    //first part: if cm convert to inch
                    hintTextView1.setText(cm_to_inch_hintTextView1);
                    Double cm_to_inch_decimal = (double) (measure_double/2.54);
                    Integer cm_to_inch_no_decimal = (int) (cm_to_inch_decimal*100.0);
                    Double cm_to_inch_output = (double) cm_to_inch_no_decimal/100.0;
                    outputTextView1.setText(measure_string + " " + cm + are + " " + cm_to_inch_output.toString() + " " + inches);
                    }

                private void printoutConvertedMeasure2() {
                    //second part: if inch convert to cm
                    hintTextView2.setText(inch_to_cm_hintTextView2);
                    Double inch_to_cm_decimal = (double) (measure_double*2.54);
                    Integer inch_to_cm_no_decimal = (int) (inch_to_cm_decimal*100.0);
                    Double inch_to_cm_output = (double) (inch_to_cm_decimal/100.0);
                    outputTextView2.setText(measure_string + " " + cm + are + " " + inch_to_cm_output.toString() + " " + cm);
                }



            });

        }


    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

};
measure_unit_spinner.setOnItemSelectedListener(listener);






}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}
}

谁能帮我解决这个错误?(我想创建一个单位转换器应用程序)

4

2 回答 2

0

您必须将视图变量初始化代码移至 onCreate() 方法:

public class MainActivity extends Activity implements OnItemSelectedListener{

 Button convertButton; 
 EditText inputEditText;
 TextView hintTextView1;
 TextView hintTextView2;
 TextView outputTextView1;
 TextView outputTextView2;
 CharSequence inches = getResources().getString(R.string.inches);
 CharSequence cm = getResources().getString(R.string.cm);
 CharSequence feet = getResources().getString(R.string.feet);
 CharSequence cm_or_inch_hint_inputEditText = getResources().getString(R.string.cm_or_inch_hint_inputEditText);
 CharSequence cm_to_inch_hintTextView1 = getResources().getString(R.string.cm_to_inch_hintTextView1);
 CharSequence inch_to_cm_hintTextView2 = getResources().getString(R.string.inch_to_cm_hintTextView2);
 CharSequence are = getResources().getString(R.string.are);
 String measure_string = inputEditText.getText().toString();
 Double measure_double = Double.parseDouble(measure_string);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);

     convertButton = (Button) findViewById(R.id.convertButton); 
     inputEditText = (EditText) findViewById(R.id.inputEditText);
     hintTextView1 = (TextView) findViewById(R.id.hintTextView1);
     hintTextView2 = (TextView) findViewById(R.id.hintTextView2);
     outputTextView1 = (TextView) findViewById(R.id.outputTextView1);
     outputTextView2 = (TextView) findViewById(R.id.outputTextView2);

    //Gets all resources you need to get the input measure, calculate and printout the converted measure

    ...
于 2013-11-05T16:26:39.443 回答
0

删除这行代码

convertButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        printoutConvertedMeasure1();
                        printoutConvertedMeasure2();
                    }

@Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
        if(measure_unit_spinner.getSelectedItemPosition() == 0){
            inputEditText.setHint(cm_or_inch_hint_inputEditText);
            //Your class variable
            position=pos;
            // Removed from here

并在 onCreate() 中添加这一行

convertButton.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                      switch(position)
                     {
                    case 0:
                    // Do Your Operation
                     printoutConvertedMeasure1();
                     printoutConvertedMeasure2();
                    break;
                    case 1:
                    // Do Your Operation
                    break;
                    // etc...
                       }
                    }

这两种方法在 onCreate() 之外

private void printoutConvertedMeasure1() {

                    //first part: if cm convert to inch
                    hintTextView1.setText(cm_to_inch_hintTextView1);
                    Double cm_to_inch_decimal = (double) (measure_double/2.54);
                    Integer cm_to_inch_no_decimal = (int) (cm_to_inch_decimal*100.0);
                    Double cm_to_inch_output = (double) cm_to_inch_no_decimal/100.0;
                    outputTextView1.setText(measure_string + " " + cm + are + " " + cm_to_inch_output.toString() + " " + inches);
                    }

                private void printoutConvertedMeasure2() {
                    //second part: if inch convert to cm
                    hintTextView2.setText(inch_to_cm_hintTextView2);
                    Double inch_to_cm_decimal = (double) (measure_double*2.54);
                    Integer inch_to_cm_no_decimal = (int) (inch_to_cm_decimal*100.0);
                    Double inch_to_cm_output = (double) (inch_to_cm_decimal/100.0);
                    outputTextView2.setText(measure_string + " " + cm + are + " " + inch_to_cm_output.toString() + " " + cm);
                }
于 2013-11-03T19:15:01.497 回答