0

我创建了一个名为 commentasync 的新类。我在 doInBackground 部分中放置了一个 findViewById() ,它给了我一个错误。这基本上是我的代码。findViewById 不起作用,它不会让我验证新视图。textView 或按钮也会作为错误出现。

    public class commentAsync extends AsyncTask<String, String, String> {


  UserFunctions userFunctions;


    // JSON Response node name
    private static String KEY_SUCCESS = "success";
    private static String KEY_ERROR_MSG = "error_msg";
    private static String KEY_STATE = "state";
    private static String KEY_USERNAME = "username";
    private static String KEY_COMMENT = "comment";
    private static String KEY_COMMENT_ID = "commentID";
    private static String KEY_NUMBER_OF_COMMENTS = "numberOfComments";
    private static String KEY_POSTED_AT = "posted_at";
    private static String KEY_VERIFY = "verify";
    private static String KEY_OFFSET_NUMBER = "offsetNumber";


    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    String usernameforcomments = db.getUserDetails();



     registerErrorMsg = (TextView) findViewById(R.id.collectComment_error);
     int verify = 1;
     int verify2 = 1;
     String offsetNumber = "null";
     LinearLayout linear = (LinearLayout) this.findViewById(R.id.commentSection);





    @Override
    protected void onPreExecute() {
    super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(String... values) {
    super.onProgressUpdate(values);

    }

    @Override
    protected String doInBackground(String... params) {
return null;






 UserFunctions CollectComments = new UserFunctions();
 JSONObject json = CollectComments.collectComments(usernameforcomments, offsetNumber);




     int commentCycle = 1;

      // check for comments  
      try {  
     if (json.getString(KEY_SUCCESS) != null) { 
         registerErrorMsg.setText("");
         String res2 = json.getString(KEY_SUCCESS);
         if(Integer.parseInt(res2) == 1){ 

             String numberOfComments = json.getString(KEY_NUMBER_OF_COMMENTS);


             String offsetNumberDb = db.getOffsetNumber();


            int numberOfComments2 = Integer.parseInt(numberOfComments) - Integer.parseInt(offsetNumberDb);
            offsetNumber = offsetNumberDb;

             //if comment number is less than 15 or equal to 15
             if(numberOfComments2 <= 15){




             while (commentCycle <= numberOfComments2){

                 JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber);


                LinearLayout commentBox = new LinearLayout(this);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                 commentBox.setBackgroundResource(R.drawable.comment_box_bg);
                layoutParams.setMargins(0, 10, 0, 10);
                commentBox.setPadding(0,0,0,10);
                 commentBox.setOrientation(LinearLayout.VERTICAL);
                 linear.addView(commentBox, layoutParams);

                 RelativeLayout commentBoxHeader = new RelativeLayout(this);
                commentBoxHeader.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                 commentBoxHeader.setBackgroundResource(R.drawable.comment_box_bg);
                commentBoxHeader.setBackgroundResource(R.drawable.comment_box_header);
                commentBox.addView(commentBoxHeader);

                 TextView plateView = new TextView(this);
                plateView.setText(json2.getString(KEY_PLATENUMBER));
                RelativeLayout.LayoutParams plateViewParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                plateViewParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                 plateView.setTextColor(getResources().getColor(R.color.white)); 
                 plateView.setTextSize(15);
                 plateView.setPadding(10, 5, 5, 5);
                 plateView.setTypeface(Typeface.DEFAULT_BOLD);
                commentBoxHeader.addView(plateView, plateViewParam);

                TextView usernameView = new TextView(this);
                usernameView.setText("Posted by: " + json2.getString(KEY_USERNAME));
                RelativeLayout.LayoutParams usernameViewParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                usernameViewParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                 usernameView.setTextColor(getResources().getColor(R.color.white));
                 usernameView.setPadding(5, 5, 10, 5);
                 usernameView.setTypeface(Typeface.DEFAULT_BOLD);
                commentBoxHeader.addView(usernameView, usernameViewParam);


                TextView commentView = new TextView(this);
                commentView.setText(json2.getString(KEY_COMMENT));
                LinearLayout.LayoutParams commentViewParams = new LinearLayout.LayoutParams(
                 LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                commentViewParams.setMargins(20, 10, 20, 20);
                commentView.setBackgroundResource(R.drawable.comment_bg);
                 commentView.setTextColor(getResources().getColor(R.color.black)); 
                commentBox.addView(commentView, commentViewParams);

                TextView descriptionView = new TextView(this);
                descriptionView.setText(json2.getString(KEY_COMMENT));
                descriptionView.setPadding(20, 10, 20, 20);
                descriptionView.setLayoutParams(new LinearLayout.LayoutParams(
                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                descriptionView.setTextColor(getResources().getColor(R.color.black)); 
                commentBox.addView(descriptionView);

                RelativeLayout commentBoxButtons = new RelativeLayout(this);
                RelativeLayout.LayoutParams commentBoxButtonsParam = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                 commentBox.addView(commentBoxButtons, commentBoxButtonsParam);

                Button btnTag1 = new Button(this);
                RelativeLayout.LayoutParams btnTag1Param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                btnTag1Param.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                 btnTag1.setText("Go to " + json2.getString(KEY_PLATENUMBER));
                 btnTag1Param.setMargins(10, 10, 10, 10);
                 btnTag1.setBackgroundResource(R.drawable.dashboard_post);
                 btnTag1.setTextColor(getResources().getColor(R.color.white));
                 btnTag1.setTypeface(Typeface.DEFAULT_BOLD);
                 btnTag1.setPadding(8, 5, 8, 5);
                 btnTag1.setId(verify);
                 commentBoxButtons.addView(btnTag1, btnTag1Param);

                 Button btnTag2 = new Button(this);
                 RelativeLayout.LayoutParams btnTag2Param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                btnTag2Param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                 btnTag2.setText("Go to " + json2.getString(KEY_USERNAME));
                 btnTag2Param.setMargins(10, 10, 10, 10);
                 btnTag2.setTypeface(Typeface.DEFAULT_BOLD);
                 btnTag2.setBackgroundResource(R.drawable.dashboard_post);
                 btnTag2.setTextColor(getResources().getColor(R.color.white));
                 btnTag2.setPadding(8, 5, 8, 5);
                 btnTag2.setId(verify2); 
                 commentBoxButtons.addView(btnTag2, btnTag2Param);

                verify2 = verify2 + 1;
                verify = verify + 1; 
                offsetNumber = json2.getString(KEY_OFFSET_NUMBER);
                commentCycle = commentCycle + 1;

         }//end while
             }//end if comment number is less than or equal to 15


             //if comment number is greater than 15
             if(numberOfComments2 > 15){
                 int commentCycle2 = 0;
             while(commentCycle2 <= 15){
                 commentCycle2 = commentCycle2 + 1; 
                 JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber);


                LinearLayout commentBox = new LinearLayout(this);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                  commentBox.setBackgroundResource(R.drawable.comment_box_bg);
                layoutParams.setMargins(0, 10, 0, 10);
                commentBox.setPadding(0,0,0,10);
                  commentBox.setOrientation(LinearLayout.VERTICAL);
                  linear.addView(commentBox, layoutParams);

                  RelativeLayout commentBoxHeader = new RelativeLayout(this);
                 commentBoxHeader.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                  commentBoxHeader.setBackgroundResource(R.drawable.comment_box_bg);
                 commentBoxHeader.setBackgroundResource(R.drawable.comment_box_header);
                 commentBox.addView(commentBoxHeader);



                TextView usernameView = new TextView(this);
                usernameView.setText("Posted by: " + json2.getString(KEY_USERNAME));
                RelativeLayout.LayoutParams usernameViewParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                usernameViewParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                  usernameView.setTextColor(getResources().getColor(R.color.white));
                  usernameView.setPadding(5, 5, 10, 5);
                  usernameView.setTypeface(Typeface.DEFAULT_BOLD);
                commentBoxHeader.addView(usernameView, usernameViewParam);


                TextView commentView = new TextView(this);
                commentView.setText(json2.getString(KEY_COMMENT));
                LinearLayout.LayoutParams commentViewParams = new LinearLayout.LayoutParams(
                  LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                commentViewParams.setMargins(20, 10, 20, 20);
                commentView.setBackgroundResource(R.drawable.comment_bg);
                  commentView.setTextColor(getResources().getColor(R.color.black)); 
                commentBox.addView(commentView, commentViewParams);

                TextView descriptionView = new TextView(this);
                descriptionView.setText(json2.getString(KEY_COMMENT));
                descriptionView.setPadding(20, 10, 20, 20);
                descriptionView.setLayoutParams(new LinearLayout.LayoutParams(
                  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                descriptionView.setTextColor(getResources().getColor(R.color.black)); 
                commentBox.addView(descriptionView);

                RelativeLayout commentBoxButtons = new RelativeLayout(this);
                RelativeLayout.LayoutParams commentBoxButtonsParam = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                  commentBox.addView(commentBoxButtons, commentBoxButtonsParam);

                Button btnTag1 = new Button(this);
                RelativeLayout.LayoutParams btnTag1Param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                btnTag1Param.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                  btnTag1.setText("Go to " + json2.getString(KEY_PLATENUMBER));
                  btnTag1Param.setMargins(10, 10, 10, 10);
                  btnTag1.setBackgroundResource(R.drawable.dashboard_post);
                  btnTag1.setTextColor(getResources().getColor(R.color.white));
                  btnTag1.setTypeface(Typeface.DEFAULT_BOLD);
                  btnTag1.setPadding(8, 5, 8, 5);
                  btnTag1.setId(verify);
                  commentBoxButtons.addView(btnTag1, btnTag1Param);

                  Button btnTag2 = new Button(this);
                  RelativeLayout.LayoutParams btnTag2Param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                btnTag2Param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                  btnTag2.setText("Go to " + json2.getString(KEY_USERNAME));
                  btnTag2Param.setMargins(10, 10, 10, 10);
                  btnTag2.setTypeface(Typeface.DEFAULT_BOLD);
                  btnTag2.setBackgroundResource(R.drawable.dashboard_post);
                  btnTag2.setTextColor(getResources().getColor(R.color.white));
                  btnTag2.setPadding(8, 5, 8, 5);
                  btnTag2.setId(verify2); 
                  commentBoxButtons.addView(btnTag2, btnTag2Param);

                verify2 = verify2 + 1;
                verify = verify + 1; 
                offsetNumber = json2.getString(KEY_OFFSET_NUMBER);
                commentCycle = commentCycle + 1;




             }//end while cycle <= 15

             db.resetOffsetTable();
             db.addOffset(Integer.parseInt(offsetNumber));

             }//end if comment number is greater than 15













            }//end if key is == 1
         else{
             // Error in registration
             registerErrorMsg.setText(json.getString(KEY_ERROR_MSG));
         }//end else
     }//end if
 } //end try

 catch (JSONException e) { 
     e.printStackTrace();
 }//end catch   










    }

    @Override
    protected void onPostExecute(String result) {
super.onPostExecute(result);
   }
   }
4

2 回答 2

0

你遇到了什么错误?您的 AsyncTask 课程是否在您的 Activity 课程之外?

如果是,正如 Voicu 所说,您应该将 AsyncTask 创建为 Activity 中的内部类。之后,您可以使用 findViewById() 方法。看看下面的例子:

public class MyActivity extends Activity
{
    //Some code

    public class commentAsync extends AsyncTask<String, String, String> 
    {
        //Hurray! Can use findViewById() here!
    }

    //Some more code
}

如果没有,我正在等待更多信息。


更新:

回答您的第一条评论,这对我很有用:

public class A extends Activity {

    public class B
    {

    }

    class C
    {

    }
}

我不知道为什么它不适合你,但我记得我以前遇到过类似的问题......

于 2013-07-04T02:35:10.860 回答
0

AsyncTask没有findViewById定义方法。检查参考页面以查看您可以使用哪些方法。Activity但是类会有这个,所以你可能想要创建AsyncTask内部Activity或将视图传递给它。

如果您要创建一个AsyncTask内部类,那么您可以拥有:

registerErrorMsg = (TextView) YourActivity.this.findViewById(R.id.collectComment_error);
于 2013-07-04T02:22:28.500 回答