Question

I have a problem. I don't figure out that. I want to add dynamic row to tableLayout. My main acivity class :

public class DutyRoster extends Activity {
     /** Called when the activity is first created. */
        ArrayList<String> updateList = new ArrayList<String>();
        ArrayList<String> totalDate, totalExam = new ArrayList<String>();
        String[] temp = null; 
        webServiceCall wsc = new webServiceCall();
        TableLayout tablo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_duty_roster);


        init();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.duty_roster, menu);
        return true;
    }

    private void init() {

        tablo = (TableLayout)DutyRoster.this.findViewById(R.id.tablo);
        final  AlertDialog ad = new AlertDialog.Builder(this).create();

        Button buton1=(Button)findViewById(R.id.remind);

        buton1.setOnClickListener(new OnClickListener() {  

            @Override
            public void onClick(View v) {
                if (updateList.size() == 0) {

                    ad.setMessage("None selected!");
                } else {

                    startAnotherActivity();
                }
            }
        });


        try {

            String resp = wsc.Call();
            new SoapCall().execute(resp);

        }catch (Exception e) {

            ad.setTitle("Error!");
            ad.setMessage(e.toString());
            ad.show();
        }

            createDynamicTable(temp);

    }

    public void startAnotherActivity () {

        Intent i = new Intent(this, ScheduleActivity.class);
        Bundle b = new Bundle();
        b.putStringArrayList("updateList", updateList);
        i.putExtra("rowList", b);
        startActivity(i);

    }



   //add dynamic rows
    public void createDynamicTable(String[] tableContentList){


        for (int i = 0; i < tableContentList.length; i++){

            TableRow newLine = new TableRow(this);
            newLine.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

            TextView rowDate = new TextView(this);
            TextView rowName = new TextView(this);
            TextView rowPeriod = new TextView(this);

            CheckBox checkBox = new CheckBox(this);
            checkBox.setOnClickListener(getOnClickDoSomething(checkBox));

            rowDate.setText(tableContentList[i]);
            rowName.setText(tableContentList[i + 1]);
            rowPeriod.setText(tableContentList[i + 2]);

            totalDate.add(tableContentList[i]);
            totalExam.add(tableContentList[i + 1]);

            newLine.addView(checkBox);
            newLine.addView(rowDate);
            newLine.addView(rowName);
            newLine.addView(rowPeriod);


            tablo.addView(newLine, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
            i += 3;

        }

    }

    View.OnClickListener getOnClickDoSomething(final Button button) {
        return new View.OnClickListener() {
            public void onClick(View v) { 
                int location = button.getId();
                updateList.add(totalDate.get(location) + "/" + totalExam.get(location));
            } 
        };
    }


//creating parallel process via AsyncTask structure
    private class SoapCall extends AsyncTask<String, String[], String[]> {

        private ProgressDialog dialog = new ProgressDialog(DutyRoster.this); 


        @Override
        protected void onPreExecute() {
            dialog.setMessage("Loading..."); 
            dialog.show();
        }

        @Override
        protected void onPostExecute(String[] temp) {

            dialog.dismiss();

        }

        @Override
        protected String[] doInBackground(String... responses) {
            String rsp = responses[0]; 

            String delimiter = "#"; 

            try{

                temp = rsp.split(delimiter); 

            }catch(Exception ex)
            {

                ex.printStackTrace();
            }   
            return temp; 
        }


    }
}

I'm getting the message "The application Tickler(process omu.ceng.tickler) has stopped unexpectedly.Please try again" while I'm running my code.

My layout file :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

            <TableLayout
                android:id="@+id/tablo"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="*" >

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" >

                    <CheckBox
                        android:id="@+id/allCheckBox"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        />

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/date" />

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/name_of_exam" />

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/period_of_exam" />
                </TableRow>

            </TableLayout>
    </ScrollView>

    <Button
        android:id="@+id/remind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/remind" />

</LinearLayout>

So also I have some logs.

01-18 22:48:26.645: E/AndroidRuntime(717): FATAL EXCEPTION: main
01-18 22:48:26.645: E/AndroidRuntime(717): **java.lang.RuntimeException: Unable to start activity ComponentInfo{omu.ceng.tickler/omu.ceng.tickler.DutyRoster}: java.lang.NullPointerException**
01-18 22:48:26.645: E/AndroidRuntime(717):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-18 22:48:26.645: E/AndroidRuntime(717):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-18 22:48:26.645: E/AndroidRuntime(717):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-18 22:48:26.645: E/AndroidRuntime(717):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-18 22:48:26.645: E/AndroidRuntime(717):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 22:48:26.645: E/AndroidRuntime(717):  at android.os.Looper.loop(Looper.java:123)
01-18 22:48:26.645: E/AndroidRuntime(717):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-18 22:48:26.645: E/AndroidRuntime(717):  at java.lang.reflect.Method.invokeNative(Native Method)
01-18 22:48:26.645: E/AndroidRuntime(717):  at java.lang.reflect.Method.invoke(Method.java:507)
01-18 22:48:26.645: E/AndroidRuntime(717):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-18 22:48:26.645: E/AndroidRuntime(717):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-18 22:48:26.645: E/AndroidRuntime(717):  at dalvik.system.NativeStart.main(Native Method)
01-18 22:48:26.645: E/AndroidRuntime(717): **Caused by: java.lang.NullPointerException**
01-18 22:48:26.645: E/AndroidRuntime(717):  at omu.ceng.tickler.DutyRoster.createDynamicTable(DutyRoster.java:109)
01-18 22:48:26.645: E/AndroidRuntime(717):  at omu.ceng.tickler.DutyRoster.init(DutyRoster.java:85)
01-18 22:48:26.645: E/AndroidRuntime(717):  at omu.ceng.tickler.DutyRoster.onCreate(DutyRoster.java:38)

I think reason of error at the line => tablo = (TableLayout)DutyRoster.this.findViewById(R.id.tablo); But I didn't find a solution. Please, help me. I'm waiting for your suggestion.

Was it helpful?

Solution

The field temp is null when you pass it in in createDynamicTable(temp), and this blows up in the body of the method when you access tableContentList.length. The fact is, AsyncTask spawns another thread in the background that executes doInBackground(). Here you initialise the field temp but you cannot count on this happening before createDynamicTable(temp).

Try calling createDynamicTable(temp) in onPostExecute() and remove line 85

@Override
protected void onPostExecute(String[] temp) {
    dialog.dismiss();
    DutyRoster.this.temp = temp;
    createDynamicTable(temp);
}

By the way, watch out the leaks!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top