坚持实施按钮

时间:2011-12-19 03:58:03

标签: android

我是java的新手(主要是仅与嵌入式决斗)

我正在编写一个简单的android程序,通过单击按钮将editText输入值传递给另一个editText。但是每次按下按钮我的程序都会崩溃。我想任何评论:)

这是我的主要内容:

package Mushroom.Pickers.Note;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
//import android.widget.Button;
//import android.widget.TableLayout;

public class Mushy extends Activity {
//private TableLayout layout;
//private Button button;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);      
}   

public void Compute_data(View view) {//Function to perform on click
     //Perform calculation      
     int Hours;
     EditText start_time = (EditText)findViewById(R.id.editText26) ;
     Hours = Integer.parseInt(start_time.getText().toString());     

     EditText test_time = (EditText)findViewById(R.id.editText43);
     test_time.setText(Hours);

 }  
}

和我的xml:

<Button
        android:id="@+id/Button1"
        android:layout_width="134px"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:textSize="15sp"
        android:text="Calculate"
        android:onClick="Compute_data" 
        android:textColor="#1e90ff" />
</TableRow>
<EditText
        android:id="@+id/editText26"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:inputType="number"
        android:width="350px" />

<EditText
        android:id="@+id/editText43"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:inputType="number"
        android:width="350px" />

布局显得很好,只需点击按钮时崩溃:(

P / S:这是logcat:

12-19 03:03:02.106: W/ResourceType(667): No package identifier when getting value for resource number 0x00000004
12-19 03:03:02.106: D/AndroidRuntime(667): Shutting down VM
12-19 03:03:02.106: W/dalvikvm(667): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-19 03:03:02.275: E/AndroidRuntime(667): FATAL EXCEPTION: main
12-19 03:03:02.275: E/AndroidRuntime(667): java.lang.IllegalStateException: Could not execute method of the activity
12-19 03:03:02.275: E/AndroidRuntime(667):  at android.view.View$1.onClick(View.java:3039)
12-19 03:03:02.275: E/AndroidRuntime(667):  at android.view.View.performClick(View.java:3480)
12-19 03:03:02.275: E/AndroidRuntime(667):  at android.view.View$PerformClick.run(View.java:13983)
12-19 03:03:02.275: E/AndroidRuntime(667):  at android.os.Handler.handleCallback(Handler.java:605)
12-19 03:03:02.275: E/AndroidRuntime(667):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-19 03:03:02.275: E/AndroidRuntime(667):  at android.os.Looper.loop(Looper.java:137)
12-19 03:03:02.275: E/AndroidRuntime(667):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-19 03:03:02.275: E/AndroidRuntime(667):  at java.lang.reflect.Method.invokeNative(Native Method)
12-19 03:03:02.275: E/AndroidRuntime(667):  at java.lang.reflect.Method.invoke(Method.java:511)
12-19 03:03:02.275: E/AndroidRuntime(667):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-19 03:03:02.275: E/AndroidRuntime(667):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-19 03:03:02.275: E/AndroidRuntime(667):  at dalvik.system.NativeStart.main(Native Method)
12-19 03:03:02.275: E/AndroidRuntime(667): Caused by: java.lang.reflect.InvocationTargetException
12-19 03:03:02.275: E/AndroidRuntime(667):  at java.lang.reflect.Method.invokeNative(Native Method)
12-19 03:03:02.275: E/AndroidRuntime(667):  at java.lang.reflect.Method.invoke(Method.java:511)
12-19 03:03:02.275: E/AndroidRuntime(667):  at android.view.View$1.onClick(View.java:3034)
12-19 03:03:02.275: E/AndroidRuntime(667):  ... 11 more
12-19 03:03:02.275: E/AndroidRuntime(667): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x4
12-19 03:03:02.275: E/AndroidRuntime(667):  at android.content.res.Resources.getText(Resources.java:247)
12-19 03:03:02.275: E/AndroidRuntime(667):  at android.widget.TextView.setText(TextView.java:3428)
12-19 03:03:02.275: E/AndroidRuntime(667):  at Mushroom.Pickers.Note.Mushy.Compute_data(Mushy.java:29)
12-19 03:03:02.275: E/AndroidRuntime(667):  ... 14 more
12-19 03:03:02.475: D/dalvikvm(667): GC_CONCURRENT freed 230K, 4% free 10426K/10759K, paused 6ms+14ms

2 个答案:

答案 0 :(得分:3)

<强>问题:

test_time.setText(value)已覆盖intString参数的方法。

  • int参数表示您将传递String资源ID。 (即 R.string.any_string)
  • String参数表示您将通过String

你把小时作为int而不是String传递。

<强>解决方案:

您将把int转换为String以设置为任何视图的文本(即ButtonTextViewEditText) 修改您的代码如下:

public void Compute_data(View view) {
     //....
     //here is rest of your code
     EditText test_time = (EditText)findViewById(R.id.editText43);
     test_time.setText(String.valueOf(Hours));//<<<------consider this change
}

答案 1 :(得分:0)

你没有在onCreate()的任何地方调用方法compute_data(),即使你要调用它,它也不会受到按钮点击的限制。当你将视图传递给它时它会给你错误在那里按下按钮。试试这个:

public class Mushy extends Activity {

   Button button;
   EditText start_time;
   EditText test_time;

   @Override
   public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      button=(Button)findViewById(R.id.Button1); 
      start_time = (EditText)findViewById(R.id.editText26) ;
      test_time = (EditText)findViewById(R.id.editText43);

      button.setOnClickListener(new OnClickListener(){

          @Override
          public void onClick(View arg0){

                compute_data();
          }
      });    
  }   

  public void compute_data() {     

     //Perform calculation      

      test_time.setText(start_time.getText().toString()); // puts value of 1st EditText into 2nd
  }  
}