获取java Android中的其他变量类
我试图让textview显示一个数字,但它不会 我的活动代码:获取java Android中的其他变量类,java,android,html,variables,Java,Android,Html,Variables,我试图让textview显示一个数字,但它不会 我的活动代码: package com.example.gotteron; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class Classement extends Activity{ TextView textview; public void onCreate(Bundle s
package com.example.gotteron;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Classement extends Activity{
TextView textview;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.classement);
GetCode getCode = new GetCode();
textview = (TextView)findViewById(R.id.textView1);
try {
textview.setText(getCode.test());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
类以获取HTMl:
//Package
package com.example.gotteron;
//Import
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class GetCode{
public String test() throws Exception{
//Recupérer le code HTML de la page
URL oracle = new URL("http://www.nationalleague.ch/NL/fr/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
String s1 = "";
while ((inputLine = in.readLine()) != null)
s1 = s1 + inputLine;
in.close();
int Berne = s1.indexOf(">SC Bern</td>");
String s3 = String.valueOf(Berne);
return s3;
}
}
//包
包com.example.gotteron;
//进口
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.net.URL;
导入java.net.URLConnection;
公共类GetCode{
公共字符串test()引发异常{
//重复代码HTML de la页面
URL oracle=新URL(“http://www.nationalleague.ch/NL/fr/");
URLConnection yc=oracle.openConnection();
BufferedReader in=新的BufferedReader(新的InputStreamReader(
yc.getInputStream());
字符串输入线;
字符串s1=“”;
而((inputLine=in.readLine())!=null)
s1=s1+输入线;
in.close();
int-Berne=s1.indexOf(“>SC-Bern”);
字符串s3=字符串.valueOf(伯尔尼);
返回s3;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/firstPosition" />
</LinearLayout>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
显示
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gotteron"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.gotteron.Principal"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Classement" android:label="@string/app_name"></activity>
<activity android:name="Calendrier" android:label="@string/app_name"></activity>
<activity android:name="Live" android:label="@string/app_name"></activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
当应用程序崩溃时您无法在ui线程中建立网络连接(HTTP连接),请尝试将调用移动到getCode.test()
进入另一个线程,或者更好地进入..您将面临NetworkOnMainThread
异常,当您在UI线程中编写与网络相关的代码时,这在API级别11及以上的模拟器上非常常见。将代码更改为AsyncTask
的doInBackground()
,然后运行它
在您的情况下,当您在test()中抛出异常时,它不会使您的应用程序崩溃,因为异常会被异常捕获
示例代码:
class FetchResultTask extends AsyncTask<Void,Void,String>
{
protected String doInBackground()
{
//your network related code.
}
protected void onPostExecute(String result)
{
super.onPostExecute(result);
//set the result to TextView here.
}
}
类FetchResultTask扩展了异步任务
{
受保护字符串doInBackground()
{
//您的网络相关代码。
}
受保护的void onPostExecute(字符串结果)
{
super.onPostExecute(结果);
//在此处将结果设置为TextView。
}
}
本质上,这就是其他答案的内容。既然你没有检查任何答案,也许这会有帮助
public class MyClass extends Activity {
TextView textview;
public void onCreate(Bundle bundle) {
super.onCreate(savedInstanceState);
setContentView(R.layout.classement);
textview = (TextView)findViewById(R.id.textview);
new NetworkOperation().execute();
}
private class NetworkOperation extends AsyncTask<Void, Void, String> {
protected String doInBackground(Void... params) {
try {
URL oracle = new URL("http://www.nationalleague.ch/NL/fr/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
String s1 = "";
while ((inputLine = in.readLine()) != null)
s1 = s1 + inputLine;
in.close();
int Berne = s1.indexOf(">SC Bern</td>");
String s3 = String.valueOf(Berne);
return s3;
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
textview.setText(result);
}
}
}
公共类MyClass扩展活动{
文本视图文本视图;
创建公共void(Bundle){
super.onCreate(savedInstanceState);
setContentView(R.layout.classement);
textview=(textview)findViewById(R.id.textview);
新建网络操作().execute();
}
私有类NetworkOperation扩展了异步任务{
受保护字符串doInBackground(无效…参数){
试一试{
URL oracle=新URL(“http://www.nationalleague.ch/NL/fr/");
URLConnection yc=oracle.openConnection();
BufferedReader in=新的BufferedReader(新的InputStreamReader(yc.getInputStream());
字符串输入线;
字符串s1=“”;
而((inputLine=in.readLine())!=null)
s1=s1+输入线;
in.close();
int-Berne=s1.indexOf(“>SC-Bern”);
字符串s3=字符串.valueOf(伯尔尼);
返回s3;
}
捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(字符串结果){
setText(结果);
}
}
}
您在哪个模拟器版本上运行上述代码?Android 4.2-API级别17您可以通过黑客攻击达到它;)如果(android.os.Build.VERSION.SDK_INT>9){StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().Build();StrictMode.setThreadPolicy(policy);}我的问题解决了,请看我的答案+注释。我将看到什么是“异步任务”,我会再次告诉你它是否有效。感谢使用findViewById(),我的类必须扩展活动。您确定必须将doInBackground()和onPostExecute放在同一个类中吗?您已将textView声明为全局变量。因此,您可以在Activity类中编写上述asynctask,并可以在onPostExecute()中使用textView。。。我必须创建一个新类吗?@user1965878按照ChristianI给出的答案,我将看到什么是“AsyncTask”,我会再次告诉你它是否有效。Thanks@user1965878我的错。需要设置textview
:findViewById(R.id.textview)代码>应该是textview=(textview)findViewById(R.id.textview)代码>谢谢,它可以工作,但我必须等待1分钟30后才会显示文本视图。我要在手机上测试一下。谢谢你花的时间!我测试了一下,在手机上只加载了10秒钟。谢谢大家@user1965878很高兴知道它是有效的。既然我们解决了您的问题,请检查答案。