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