1

我正在从在 ui 线程上运行的 onPostExecute 更新我的片段 ui。我的onPostExecute()asynctask 方法中有一个 nullpointerException。

这是我的方法中的内容:

// dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {



                 //TODO fill front-end
                //all
                //get view for each fragment.
                for(int i = 0; i< 3; i++){
                    Fragment fragment = getSupportFragmentManager().findFragmentByTag("android:switcher:" + mViewPager.getId() + ":" + i);
                    View view = fragment.getView();

                    TableLayout layout;
                    switch(i){

                    /* Update parsed JSON data into tablelayouts
                     */

                    case 0:
                        //allcontent
                        layout = (TableLayout) view.findViewById(R.id.AllContentLayout);

                        for(JsonInObject item: allItems){
                            if(item.getType().equals("text")){
                                addText(layout, item);
                            }else{
                                addImage(layout, item);
                            }
                        }

                        break;

                    case 1:    //Error take place within this from this for loop.
                        //text
                        layout = (TableLayout) view.findViewById(R.id.TextContentLayout);

                        for(Textobj item: textBlocks){
                            addText(layout, item);
                        }

                        break;

                    case 2:
                        //images
                        layout = (TableLayout) view.findViewById(R.id.ImageContentLayout);

                        for(Imageobj item: images){
                            addImage(layout, item);
                        }

                        break;

                    default:
                        break;
                    }
                }

            }
        });

这是错误中所述的 addText 方法:

    private void addText(TableLayout textLayout, JsonInObject item){
    //build the rows and add them to the fragment view
    //TableRow newrow = (TableRow) findViewById(R.id.tableTextRow);
    TableRow newrow = new TableRow(ContentActivity.this);
    newrow.setLayoutParams(new TableLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

    TextView nameText = new TextView(ContentActivity.this);
    nameText.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

    //TextView nametext = (TextView) findViewById(R.id.name);

    if(item.setIsComplete() != false){
        try{
            nameText.setText(item.getData());
        } catch(NullPointerException e){
            Log.println(1, "NULLPOINTER", "Either the tablerow, Texview or tablelayout is null... for some reason.");
            e.printStackTrace();
            return;
        }

            newrow.addView(nameText);

        textLayout.addView(newrow);
    }
}

这是我的错误:

10-19 11:39:36.706: E/AndroidRuntime(32637): FATAL EXCEPTION: main
10-19 11:39:36.706: E/AndroidRuntime(32637): java.lang.NullPointerException
10-19 11:39:36.706: E/AndroidRuntime(32637):    at com.eddiecubed44.Jsonapptest.ContentActivity.addText(ContentActivity.java:448)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at com.eddiecubed44.Jsonapptest.ContentActivity.access$3(ContentActivity.java:426)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at com.eddiecubed44.Jsonapptest.ContentActivity$LoadTextnImgs$1.run(FuzzContentActivity.java:355)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at android.app.Activity.runOnUiThread(Activity.java:4631)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at com.eddiecubed44.Jsonapptest.ContentActivity$LoadTextnImgs.onPostExecute(ContentActivity.java:318)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at com.eddiecubed44.Jsonapptest.ContentActivity$LoadTextnImgs.onPostExecute(ContentActivity.java:1)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at android.os.AsyncTask.finish(AsyncTask.java:631)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at android.os.Looper.loop(Looper.java:137)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at android.app.ActivityThread.main(ActivityThread.java:4918)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at java.lang.reflect.Method.invokeNative(Native Method)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at java.lang.reflect.Method.invoke(Method.java:511)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
10-19 11:39:36.706: E/AndroidRuntime(32637):    at dalvik.system.NativeStart.main(Native Method)

空指针与我用来添加表格行的布局相关联。我不知道为什么这会变成空。发生错误的行号在 之内case 1:如您所见,我之前使用过布局,case 0:并且我假设行已正确添加。

帮帮我obi wan kenobi 你是我唯一的希望。

4

0 回答 0