我正在使用vogella教程,我几乎肯定我完全按照说明操作,但是当我尝试使用该应用程序时,它不起作用,它只显示xml布局。
代码:
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 :(得分: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 cat。这样,您可以使用它们来确定应用程序在构建应用程序时的位置。像这样的东西:
Log.d("TAG","I'm inside the click callback!");