代码不能正常工作,虽然之前有效

时间:2012-03-05 18:01:00

标签: android gettext tostring textedit

我想知道是否有人可以帮助我。我正在创建一个Android应用程序作为我的最终项目,除了一部分外,它目前运行正常。我遇到的问题是这个班级不能正常工作

public class convert extends Activity
{
Button   mButton;
EditText eweight;
EditText eheight;
TextView t;
String s,s2;
double meters = 0;
double kg = 0;

public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
   getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.    FLAG_FULLSCREEN);
setContentView(R.layout.convert);
eweight   = (EditText)findViewById(R.id.eweight);
eheight   = (EditText)findViewById(R.id.eheight);
}
public void converth(View view) 
{

    s2 = convert.this.eweight.getText().toString();
    int stone = Integer.parseInt(s2);
    kg = stone*6.35;
    t = (TextView)findViewById(R.id.newweight);
    t.setText(""+kg);
}
public void convertw(View view) 
{
    s  = convert.this.eheight.getText().toString();
    int feet = Integer.parseInt(s);
    meters = feet*0.3048;
    t = (TextView)findViewById(R.id.newheight);
    t.setText(""+meters);
}   
public void pass(View view) 
{
    Intent i = new Intent();
        i.setClassName("com.b00348312.workout","com.b00348312.workout.WorkoutChoice");
    if (meters > 0)
    {
        double number = getIntent().getDoubleExtra("height", meters);
    }
    if (kg > 0)
    {
        double number2 = getIntent().getDoubleExtra("weight", kg);
    }
    startActivity(i); 
}   

当按下设置在XML中的按钮时,它会拉出一个屏幕,强制关闭应用程序。

使用xml中内置的“andoid:onClick =''”语句设置按钮。 getText()的语句也在这个类中使用,并且工作正常

    public void onCreate(Bundle savedInstanceState) 
    {
    // add a click listener to the button
    mPickDate.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });
    // get the current date
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    // display the current date (this method is below)
    updateDisplay();

    mButton = (Button)findViewById(R.id.get);
    weight   = (EditText)findViewById(R.id.weight);
    height   = (EditText)findViewById(R.id.height);
    t = (TextView)findViewById(R.id.out);

    mButton.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
               s = WorkoutChoice.this.weight.getText().toString();
               s2 = WorkoutChoice.this.height.getText().toString();
               int w = Integer.parseInt(s);
               double h = Double.parseDouble(s2);
               double BMI = (w/h)/h;
               t.setText(""+BMI);
            }
        });

}

有没有人知道为什么声明似乎在一个类而不是另一个类中起作用:

这是来自logcat的东西:

03-05 22:22:34.637: E/global(2987): Deprecated Thread methods are not supported.
03-05 22:22:34.637: E/global(2987): java.lang.UnsupportedOperationException
03-05 22:22:34.637: E/global(2987):     at java.lang.VMThread.stop(VMThread.java:85)
03-05 22:22:34.637: E/global(2987):     at java.lang.Thread.stop(Thread.java:1391)
03-05 22:22:34.637: E/global(2987):     at java.lang.Thread.stop(Thread.java:1356)
03-05 22:22:34.637: E/global(2987):     at com.b00348312.workout.Splashscreen$1.run(Splashscreen.java:42)
03-05 22:22:54.597: D/dalvikvm(2987): GC_FOR_MALLOC freed 6061 objects / 319632 bytes in 63ms
03-05 22:22:54.607: D/AndroidRuntime(2987): Shutting down VM
03-05 22:22:54.607: W/dalvikvm(2987): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
03-05 22:22:54.627: E/AndroidRuntime(2987): FATAL EXCEPTION: main
03-05 22:22:54.627: E/AndroidRuntime(2987): java.lang.IllegalStateException: Could not execute method of the activity
03-05 22:22:54.627: E/AndroidRuntime(2987):     at   android.view.View$1.onClick(View.java:2072)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at android.view.View.performClick(View.java:2408)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at android.view.View$PerformClick.run(View.java:8817)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at android.os.Handler.handleCallback(Handler.java:587)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at android.os.Looper.loop(Looper.java:144)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at android.app.ActivityThread.main(ActivityThread.java:4937)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at java.lang.reflect.Method.invokeNative(Native Method)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at java.lang.reflect.Method.invoke(Method.java:521)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at dalvik.system.NativeStart.main(Native Method)
03-05 22:22:54.627: E/AndroidRuntime(2987): Caused by: java.lang.reflect.InvocationTargetException
03-05 22:22:54.627: E/AndroidRuntime(2987):     at com.b00348312.workout.convert.convertw(convert.java:44)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at java.lang.reflect.Method.invokeNative(Native Method)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at java.lang.reflect.Method.invoke(Method.java:521)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at android.view.View$1.onClick(View.java:2067)
03-05 22:22:54.627: E/AndroidRuntime(2987):     ... 11 more
03-05 22:22:54.627: E/AndroidRuntime(2987): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
03-05 22:22:54.627: E/AndroidRuntime(2987):     at java.lang.Integer.parseInt(Integer.java:412)
03-05 22:22:54.627: E/AndroidRuntime(2987):     at java.lang.Integer.parseInt(Integer.java:382)
03-05 22:22:54.627: E/AndroidRuntime(2987):     ... 15 more

2 个答案:

答案 0 :(得分:1)

从您以后的活动代码中...问题在于您在使用findViewById()来查找按钮,尽管您没有为该特定活动设置任何布局。

答案 1 :(得分:-1)

尝试执行项目 - >清理并重建。如果之前有效,并且自上次成功运行后您还没有编辑它,那么清理应该有所帮助。