eclipse微调项目ID

时间:2014-11-29 21:35:04

标签: java android eclipse

我试图为Android制作温度转换器应用程序。 我想使用2个微调器(" From"和#34; To")。 有3个项目,Celsius,Kelvin和Fahrenheit。 我还添加了一个文本字段,用户将在其中放入一个数字。 在底部有一个按钮"转换"和一个结果的textview。 现在我需要的是,当"转换"按下按钮textview由结果替换,但取决于2个微调器中的选定项目。 例如,如果选定的项目是从摄氏度到华氏度,它应该计算如下结果: 9/5 *(用户写的数字)+32 所以我的问题是我该怎么做,我想我应该使用"如果"但我不确定如何,因为物品没有被任何东西定义:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="From">
        <item>Celsius</item>
        <item>Kelvin</item>
        <item>Fahrenheit</item>
    </string-array>
</resources>

顺便说一下,这是我为旋转器显示的值中的xml文件。 这是我的MainActivity:

package com.usc.uscconverter;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {
    Spinner sp, sp2;
    TextView textFrom, textTo, textDegree, textResult;
    EditText editText1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sp=(Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> ar= ArrayAdapter.createFromResource(this, R.array.From, android.R.layout.simple_list_item_1);
        ar.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        sp.setAdapter(ar);

        sp2=(Spinner) findViewById(R.id.spinner2);
        ArrayAdapter<CharSequence> ar2= ArrayAdapter.createFromResource(this, R.array.To, android.R.layout.simple_list_item_1);
        ar2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        sp2.setAdapter(ar2);

        Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
        buttonConvert.setOnClickListener(new OnClickListener(){
            public void onClick(View v){

            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

修改 我编辑了我的主要活动并添加了这个:

 Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
        buttonConvert.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if (sp.getSelectedItem().toString().equals("Celsius") && sp2.getSelectedItem().toString().equals("Fahrenheit")){
                    editText1 = (EditText)findViewById(R.id.editText1);
                    num = editText1.getText().toString();
                    int d = 9/5;
                    int n = Integer.parseInt(num);
                    int r = d * n + 32;
                    textResult.setText(String.valueOf(r));
                }

            }


        });

虽然没有错误,但是当我启动应用程序时它会崩溃,而在logcat中它会像这样说:

11-29 18:42:24.890: D/dalvikvm(846): Not late-enabling CheckJNI (already on)
11-29 18:42:36.830: I/Choreographer(846): Skipped 165 frames!  The application may be doing too much work on its main thread.
11-29 18:42:37.840: D/gralloc_goldfish(846): Emulator without GPU emulation detected.
11-29 18:42:41.740: D/dalvikvm(846): GC_FOR_ALLOC freed 113K, 6% free 2971K/3156K, paused 39ms, total 46ms
11-29 18:42:41.980: I/Choreographer(846): Skipped 75 frames!  The application may be doing too much work on its main thread.
11-29 18:42:43.240: I/Choreographer(846): Skipped 118 frames!  The application may be doing too much work on its main thread.
11-29 18:42:44.190: I/Choreographer(846): Skipped 102 frames!  The application may be doing too much work on its main thread.
11-29 18:42:45.330: I/Choreographer(846): Skipped 79 frames!  The application may be doing too much work on its main thread.
11-29 18:42:45.630: I/Choreographer(846): Skipped 57 frames!  The application may be doing too much work on its main thread.
11-29 18:42:50.050: D/AndroidRuntime(846): Shutting down VM
11-29 18:42:50.050: W/dalvikvm(846): threadid=1: thread exiting with uncaught exception (group=0xb2a42ba8)
11-29 18:42:50.060: E/AndroidRuntime(846): FATAL EXCEPTION: main
11-29 18:42:50.060: E/AndroidRuntime(846): Process: com.usc.uscconverter, PID: 846
11-29 18:42:50.060: E/AndroidRuntime(846): java.lang.NullPointerException
11-29 18:42:50.060: E/AndroidRuntime(846):  at com.usc.uscconverter.MainActivity$1.onClick(MainActivity.java:49)
11-29 18:42:50.060: E/AndroidRuntime(846):  at android.view.View.performClick(View.java:4438)
11-29 18:42:50.060: E/AndroidRuntime(846):  at android.view.View$PerformClick.run(View.java:18422)
11-29 18:42:50.060: E/AndroidRuntime(846):  at android.os.Handler.handleCallback(Handler.java:733)
11-29 18:42:50.060: E/AndroidRuntime(846):  at android.os.Handler.dispatchMessage(Handler.java:95)
11-29 18:42:50.060: E/AndroidRuntime(846):  at android.os.Looper.loop(Looper.java:136)
11-29 18:42:50.060: E/AndroidRuntime(846):  at android.app.ActivityThread.main(ActivityThread.java:5017)
11-29 18:42:50.060: E/AndroidRuntime(846):  at java.lang.reflect.Method.invokeNative(Native Method)
11-29 18:42:50.060: E/AndroidRuntime(846):  at java.lang.reflect.Method.invoke(Method.java:515)
11-29 18:42:50.060: E/AndroidRuntime(846):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-29 18:42:50.060: E/AndroidRuntime(846):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-29 18:42:50.060: E/AndroidRuntime(846):  at dalvik.system.NativeStart.main(Native Method)
11-29 18:42:54.150: I/Process(846): Sending signal. PID: 846 SIG: 9

1 个答案:

答案 0 :(得分:0)

将此添加到您的代码中:

textResult = (TextView)findViewById(R.id.textResult);