Android 无法使用AsyncTask在TextView中设置文本
我有一个Android 无法使用AsyncTask在TextView中设置文本,android,android-layout,android-studio,android-asynctask,Android,Android Layout,Android Studio,Android Asynctask,我有一个AsyncTask,我想在我的layout.xml文件中设置TextView的文本,但我的应用程序因此错误而崩溃 android.content.res.Resources$NotFoundException:字符串资源ID#0x2 请帮帮我 public class UploadVideo extends Activity { protected void onCreate(Bundle savedInstanceState) { super
AsyncTask
,我想在我的layout.xml文件中设置TextView
的文本,但我的应用程序因此错误而崩溃
android.content.res.Resources$NotFoundException:字符串资源ID#0x2
请帮帮我
public class UploadVideo extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
transferUtility = Util.getTransferUtility(this);
requestWindowFeature(getWindow().FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.my_activity);
context = this;
new get_people_data().execute();
}
public class get_people_data extends AsyncTask<String,String,String>{
final String TAG = "get_people_data";
String API_URL = "http://api.expressionsapp.com/users/auth0_5732cc152781665b7b5dfee6/status/";
int front;
int behind;
@Override
protected String doInBackground(String...arg0)
{
try
{
JsonParser jParser = new JsonParser();
JSONObject json = jParser.getJSONFromUrl(API_URL);
behind= json.getInt("countBack");
front= json.getInt("countFront");
}catch (JSONException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s)
{
TextView ppl_infront;
TextView ppl_behind;
ppl_infront = (TextView) findViewById(R.id.ppl_infront);
ppl_infront.setText(front);
ppl_behind = (TextView) findViewById(R.id.ppl_behind);
ppl_behind.setText(behind);
}
}
}
公共类上传视频扩展活动{
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
transferUtility=Util.getTransferUtility(this);
requestWindowFeature(getWindow().FEATURE\u NO\u TITLE);
requestWindowFeature(Window.FEATURE\u不确定\u进度);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(R.layout.my_活动);
上下文=这个;
新建get_people_data().execute();
}
公共类获取人员数据任务{
最后一个字符串标记=“获取人员数据”;
字符串API_URL=”http://api.expressionsapp.com/users/auth0_5732cc152781665b7b5dfee6/status/";
内锋;
int后面;
@凌驾
受保护的字符串doInBackground(字符串…arg0)
{
尝试
{
JsonParser jParser=新的JsonParser();
JSONObject json=jParser.getJSONFromUrl(API_URL);
behind=json.getInt(“倒计时”);
front=json.getInt(“countFront”);
}捕获(JSONException e)
{
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串s)
{
TextView ppl_infront;
文本视图ppl_后面;
ppl_infront=(TextView)findViewById(R.id.ppl_infront);
ppl_前排座椅(前排);
ppl_behind=(TextView)findViewById(R.id.ppl_behind);
ppl_behind.setText(behind);
}
}
}
完成您的任务
ppl_infront=(TextView)findViewById(R.id.ppl_infront);
代码>
及
ppl_-behind=(TextView)findViewById(R.id.ppl_-behind);
代码>
在setContentView(R.layout.my_活动)下面的onCreate()
方法中;
代码>
如果它不能解决您的问题,请发布您的xml文件。执行您的操作
ppl_infront=(TextView)findViewById(R.id.ppl_infront);
代码>
及
ppl_-behind=(TextView)findViewById(R.id.ppl_-behind);
代码>
在setContentView(R.layout.my_活动)下面的onCreate()
方法中;
代码>
如果它不能解决您的问题,请发布您的xml文件。问题已解决
ppl_infront.setText(front);
ppl_behind.setText(behind);
因为您正在使用
最终作废setText(整数剩余)
以及在具有该int id的R文件中进行的系统搜索(该int id是自动生成的文件)
对此进行更改:
ppl_infront.setText(String.valueOf(front));
ppl_behind.setText(String.vaueOf(behind));
使用
最终作废setText(字符序列文本)
这里是官方问题所在
ppl_infront.setText(front);
ppl_behind.setText(behind);
因为您正在使用
最终作废setText(整数剩余)
以及在具有该int id的R文件中进行的系统搜索(该int id是自动生成的文件)
对此进行更改:
ppl_infront.setText(String.valueOf(front));
ppl_behind.setText(String.vaueOf(behind));
使用
最终作废setText(字符序列文本)
这里是官方的快速解决方案:
如果您不想弄乱String.value(front)
,您可以如下设置文本
ppl_behind.setText(front + "");
快速解决方案:
如果您不想弄乱String.value(front)
,您可以如下设置文本
ppl_behind.setText(front + "");
首先声明TextView
Global部分显示xml请在onCreate中初始化TextView您应该在onCreate()中初始化小部件
并全局声明。首次声明TextView
Global部分的可能重复显示您的xml请在onCreate中初始化TextView。您应该在onCreate()
中初始化小部件并全局声明它。如果他使用此选项,则TextView必须是实例变量。为什么需要这样做?这不是为了让它全球化。如果他在onPostExecute()
中执行,那么findViewById()
的上下文将是异步任务上下文。因此它可能会崩溃。我怀疑那个人。因为,findViewById
仅在活动
上下文中运行。AsyncTask
中没有findViewById
,因此它无法获取上下文。如果你能看到他的onPostExecute()
方法,它包含文本视图的findViewById()
。我是说Android中的onPostExecute()
方法没有findViewById()
。因此,上面的代码只在活动上下文中运行,而活动上下文中没有该方法,因为它可以正确编译和运行。为什么需要这样做?这不是为了让它全球化。如果他在onPostExecute()
中执行,那么findViewById()
的上下文将是异步任务上下文。因此它可能会崩溃。我怀疑那个人。因为,findViewById
仅在活动
上下文中运行。AsyncTask
中没有findViewById
,因此它无法获取上下文。如果你能看到他的onPostExecute()
方法,它包含文本视图的findViewById()
。我是说Android中的onPostExecute()
方法没有findViewById()
。所以,上面的代码是ru