2012-10-24 114 views
2

我的应用程序是输入金额,计算金额并显示金额。Android:按钮点击不起作用

我使用的Android SDK 16.当我进入量,并计算按钮点击我收到

SPAN_EXCLUSIVE_EXCLUSIVE跨度不能有长度为零。

我的动态类

public class ChapterTwo extends Activity { 

public static final String tag = "Chapter 2"; 

static{ 
    StrictMode.ThreadPolicy policy = new  
     StrictMode.ThreadPolicy.Builder().permitAll().build(); 

    StrictMode.setThreadPolicy(policy); 
} 




@SuppressWarnings("unused") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_chapter_two); 

    final EditText mealpricefield = (EditText) findViewById(R.id.mealprice); 
    final TextView answerfield = (TextView) findViewById(R.id.answer); 
    final Button button = (Button) findViewById(R.id.calculate); 

      button.setOnClickListener(new Button.OnClickListener() { 

      public void onClickView(View v) { 
      try { 
       Log.i(tag, "onClick Invoked"); 
       // grab the meal price from UI 
       String mealprice = mealpricefield.getText().toString(); 
       Log.i(tag, "meal price is [" + mealprice + "]"); 
       String answer = ""; 
       // check to see if meal price contains $ 
       if (mealprice.indexOf("$") == -1) { 
        mealprice = "$" + mealprice; 
       } 

       float fmp = 0.0F; 
       // get currency formatter; 
       NumberFormat nf = java.text.NumberFormat 
         .getCurrencyInstance(); 
       // grab the input meal price 
       fmp = nf.parse(mealprice).floatValue(); 
       // let's give a nice tip 
       fmp *= 1.2; 
       Log.i(tag, " Total meal price Unformatted is [" + fmp + "]"); 
       // format our result 
       answer = " Full Price, Including the 20% Tip is " 
         + nf.format(fmp); 
       answerfield.setText(answer); 
       Log.i(tag, " On Click Complete"); 

      } catch (java.text.ParseException e) { 

       Log.i(tag, "Parse Exception caught"); 
       answerfield.setText("Failed to Parse the Exception"); 
      } 

      catch (Exception e) { 
       Log.e(tag, "Failed To calculate the tip" + e.getMessage()); 
       e.printStackTrace(); 
       answerfield.setText(e.getMessage()); 

      } 

     } 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 
}} 

我的布局是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Chapter 2 Android Tip Calculator" 
    tools:ignore="HardcodedText" /> 

<EditText 
    android:id="@+id/mealprice" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="text"> 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/calculate" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Calculate Tip" /> 

<EditText 
    android:id="@+id/answer" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" /> 

我收到以下错误日志猫

10-23 20:19:26.232: E/Trace(699): error opening trace file: No such file or directory (2) 
10-23 20:19:27.092: D/gralloc_goldfish(699): Emulator without GPU emulation detected. 
10-23 20:19:33.262: W/IInputConnectionWrapper(699): showStatusIcon on inactive InputConnection 
10-23 20:24:22.106: I/Choreographer(699): Skipped 30 frames! The application may be doing too much work on its main thread. 
10-23 20:27:52.824: I/Choreographer(699): Skipped 37 frames! The application may be doing too much work on its main thread. 
10-23 20:31:13.323: E/SpannableStringBuilder(699): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
10-23 20:31:13.323: E/SpannableStringBuilder(699): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
10-23 20:31:32.552: I/Choreographer(699): Skipped 35 frames! The application may be doing too much work on its main thread. 
10-23 20:39:44.723: I/Choreographer(699): Skipped 63 frames! The application may be doing too much work on its main thread. 

回答

1

你应该申报所有Ť他认为在主类和的onCreate初始化它们()方法:

public class ChapterTwo extends Activity { 

public static final String tag = "Chapter 2"; 
EditText mealpricefield; 
TextView answerfield; 
Button button; 
. 
. 
. 
} 

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

    mealpricefield = (EditText) findViewById(R.id.mealprice); 
    answerfield = (TextView) findViewById(R.id.answer); 
    button = (Button) findViewById(R.id.calculate); 
. 
. 
. 
} 

而且你已经放置在onClickView(视图V)应该是内部的onClick(视图v),你有下面的代码的代码:

@Override 
public void onClick(View v) { 
     // TODO Auto-generated method stub 

    } 
+0

S:它工作,我把我的所有代码保存在onClick(View V)中。它工作没有任何错误。非常感谢。 – alapanil

+0

谢谢Marcin S – alapanil

+0

没问题!请接受答案。 –

0

一眼望去,这可能是你的问题。

Log.i(tag,“Total meal price Unformatted is [”+ fmp +“]”);

fmp是一个浮点数......你将它添加到一个字符串中。

注释此行,看看它是否工作。

1

除了您发布的代码之外是否还有其他代码?

这里有几件事情,我注意到:

  1. 当你按下按钮,因为你正如上文所指出都没有做在onclick(视图V)方法什么您的代码不会做任何事情。你有一个没有人打电话的onClickView(View v)函数。你应该将你的代码从onClickView()移动到onClick(),然后摆脱onClickView()方法(或者你可以从onClick函数调用onClickView)。

  2. 如果您移动代码如上所述,您的代码将工作,我甚至现在就自己对其进行测试以进行仔细检查。 (您在上面的复制粘贴代码中缺少</LinearLayout >标记,但我认为您已经拥有了它)。

  3. 您发布的错误日志显示来自SpannableStringBuilder的错误。但是,您并没有在代码中的任何位置使用SpannableStringBuilder,因此a)您有其他代码或b)此错误与您的代码无关。

当您按下按钮时会发生什么?应用程序崩溃了吗?当你看到问题时,你使用什么值进行测试?日志中是否有其他日志中有您的标记或应用程序名称?您发布的日志不会显示任何强制关闭日志或任何与您的应用程序相关的错误。

第二个想法,你是说你的问题是,当你按下计算按钮,什么都没有发生?所以你认为你有一个错误,只是认为你在日志中看到的错误是你的代码无法工作的原因?如果是这样的话,您只需将onClickView()中的代码块移入onClick()并摆脱onClickView()即可。错误日志来自与您的代码无关的其他程序。