现在我知道这些是相当常见的问题,但我使用的文本视图无法解决.我已经清理了项目,导入android.widget.TextView,但它继续标记为未解析.
这是我正在使用的代码:
public void onStart() {
super.onStart();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://1.php");
HttpResponse response = client.execute(request);
// Get the response
BufferedReader rd = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
TextView.append(line);
}
}
Run Code Online (Sandbox Code Playgroud)
activity_main中的TextView:
<TextView
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10">
</TextView>
Run Code Online (Sandbox Code Playgroud)
对此有什么想法吗?
编辑:
TextView textView = (TextView) findViewById(R.id.TextView1);
String line = "";
while ((line = rd.readLine()) != null) {
TextView1.append(line);
}
Run Code Online (Sandbox Code Playgroud)
以TextView1开头的结束行提供错误,指出无法解析.只是,TextView1而不是整条线.
编辑28/10/2012:
好的,所以TextView现在正在工作,但反过来,代码的其他部分却没有.这是截图显示的确切内容.我可以在try和catch语句中包装其中的每一个,但然后app force在我的Android设备上关闭.
http://southwestdesign.org.uk/Code.jpg
有关于此的任何想法吗?
您需要获取特定的TextView:
TextView textView = (TextView) findViewById(R.id.TextView);
String line = "";
while ((line = rd.readLine()) != null) {
textView.append(line);
}
Run Code Online (Sandbox Code Playgroud)
(你真的应该给TextView一个独特的,更具描述性的id,就像这样android:id="@+id/webContentTV".当你使用10-20 TextView时,你可以很容易地跟踪它们.)
除了
在id你的XML:
<!-- Notice I gave this TextView a more descriptive id. -->
<TextView
android:id="@+id/webContentTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
Run Code Online (Sandbox Code Playgroud)
必须匹配您传递给的确切ID findViewById():
TextView textView = (TextView) findViewById(R.id.webContentTV);
String line = "";
while ((line = rd.readLine()) != null) {
textView.append(line);
}
Run Code Online (Sandbox Code Playgroud)
目前你的身份不匹配:android:id="TextView"不是findViewById(R.id.TextView1)
| 归档时间: |
|
| 查看次数: |
28516 次 |
| 最近记录: |