2012-02-09 46 views
0

我正在使用vogella教程,我幾乎肯定我完全按照說明操作,但是當我嘗試使用該應用程序時,它不起作用,它只顯示xml佈局。Android溫度轉換器無法正常工作?

代碼:

package convert.activity; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.RadioButton; 
import android.widget.Toast; 

public class ConvertActivityActivity extends Activity { 
private EditText text; 

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

    text = (EditText) findViewById(R.id.editText1); 

} 

// This method is called at button click because we assigned the name to the 
// "On Click property" of the button 
public void myClickHandler(View view) { 
    switch (view.getId()) { 
    case R.id.button1: 
     RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0); 
     RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1); 
     if (text.getText().length() == 0) { 
      Toast.makeText(this, "Please enter a valid number", 
        Toast.LENGTH_LONG).show(); 
      return; 
     } 

     float inputValue = Float.parseFloat(text.getText().toString()); 
     if (celsiusButton.isChecked()) { 
      text.setText(String 
        .valueOf(convertFahrenheitToCelsius(inputValue))); 
      celsiusButton.setChecked(false); 
      fahrenheitButton.setChecked(true); 
     } else { 
      text.setText(String 
        .valueOf(convertCelsiusToFahrenheit(inputValue))); 
      fahrenheitButton.setChecked(false); 
      celsiusButton.setChecked(true); 
     } 
     break; 
    } 
} 

// Converts to celsius 
private float convertFahrenheitToCelsius(float fahrenheit) { 
    return ((fahrenheit - 32) * 5/9); 
} 

// Converts to fahrenheit 
private float convertCelsiusToFahrenheit(float celsius) { 
    return ((celsius * 9)/5) + 32; 
} 
} 

究竟什麼可能可能是錯呢? (公共類ConvevrtActivityActivity是它的實際名稱)

編輯:這是我的XML太:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<EditText android:layout_height="wrap_content" 
    android:id="@+id/editText1" 
    android:layout_width="match_parent" ></EditText> 

<RadioGroup android:layout_height="wrap_content" android:id="@+id/radioGroup1" 
    android:layout_width="match_parent"> 

    <RadioButton 
     android:id="@+id/radio0" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:checked="true" 
     android:text="@string/celsius" > 
</RadioButton> 

    <RadioButton 
     android:id="@+id/radio1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/fahrenheit" > 
</RadioButton> 
</RadioGroup> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/calc" > 
</Button> 


</LinearLayout> 

的logcat:

02-08 21:05:39.392: W/ThrottleService(89): unable to find stats for iface rmnet0 
02-08 21:07:18.561: D/dalvikvm(172): GC_CONCURRENT freed 385K, 6% free 9529K/10119K,   paused 9ms+8ms 
02-08 21:12:06.030: A/BatteryStatsImpl(89): problem reading network stats 
02-08 21:12:06.030: A/BatteryStatsImpl(89): java.lang.IllegalStateException: problem parsing idx 1 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.jav a:300) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.jav a:250) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at  com.android.internal.os.BatteryStatsImpl.getNetworkStatsDetailGroupedByUid(BatteryStatsImpl .java:5734) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at com.android.internal.os.BatteryStatsImpl.access$100(BatteryStatsImpl.java:76) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at com.android.internal.os.BatteryStatsImpl$Uid.computeCurrentTcpBytesReceived(BatteryStatsImp l.java:2457) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at com.android.internal.os.BatteryStatsImpl$Uid.getTcpBytesReceived(BatteryStatsImpl.java:2446) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at com.android.internal.os.BatteryStatsImpl.writeSummaryToParcel(BatteryStatsImpl.java:5437) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at com.android.internal.os.BatteryStatsImpl.writeLocked(BatteryStatsImpl.java:4836) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at com.android.internal.os.BatteryStatsImpl.writeAsyncLocked(BatteryStatsImpl.java:4818) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at com.android.server.am.ActivityManagerService.updateCpuStatsNow(ActivityManagerService.java:1649) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at com.android.server.am.ActivityManagerService$3.run(ActivityManagerService.java:1531) 
02-08 21:12:06.030: A/BatteryStatsImpl(89): Caused by: java.io.FileNotFoundException: /proc/net/xt_qtaguid/stats: open failed: ENOENT (No such file or directory) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at libcore.io.IoBridge.open(IoBridge.java:406) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at java.io.FileInputStream.<init> (FileInputStream.java:78) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.jav a:269) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  ... 10 more 
02-08 21:12:06.030: A/BatteryStatsImpl(89): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at libcore.io.Posix.open(Native Method) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  at libcore.io.IoBridge.open(IoBridge.java:390) 
02-08 21:12:06.030: A/BatteryStatsImpl(89):  ... 12 more 
02-08 21:12:06.439: D/dalvikvm(89): GC_CONCURRENT freed 1446K, 16% free 11329K/13383K,  paused 10ms+19ms 
02-08 21:14:45.149: D/dalvikvm(172): GC_CONCURRENT freed 383K, 6% free 9529K/10119K, paused 8ms+7ms 
02-08 21:15:39.493: W/ThrottleService(89): unable to find stats for iface rmnet0 
+0

張貼您的XML也。並且在你的點擊處理程序回調中添加一些日誌語句,以便你可以判斷你是否獲得了任何回調。 – FoamyGuy 2012-02-09 03:15:57

+0

我不確定日誌中有什麼問題,但是如果有幫助,我將使用真正的手機作爲模擬器。 – Mia 2012-02-09 03:24:05

回答

1

你還沒有 「捆綁」 點擊監聽方法你的按鈕視圖。更改佈局的部分看起來像這樣:

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/calc" 
    android:onClick="myClickHandler"> 
</Button> 

如果不從認識到任何東西的記錄很可能是無關的應用程序。我的意思是把日誌語句放到你的java中,基本上可以打印到日誌貓。這樣,您可以使用它們來確定在構建應用程序時破壞了哪些應用程序。這樣的事情:

Log.d("TAG","I'm inside the click callback!"); 
+0

非常感謝!我完全忘記了這一點。 – Mia 2012-02-09 03:36:14