Getting error when using tableview in android

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Apr 21, 2016 6:34:28 AM via Website

Help

Getting error while using tableview in android. Getting below error while running
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TableLayout.addView(android.view.View, android.view.ViewGroup$LayoutParams)' on a null object reference

My code

private class AsyncCallWS extends AsyncTask {

    TableLayout tl = (TableLayout) findViewById(R.id.tl1);

    @Override
    protected void onPreExecute() {
        Log.i(TAG, "onPreExecute");
    }

    @Override
    protected String doInBackground(String... params) {
        Log.i(TAG, "doInBackground");

        String NAMESPACE = "example";
        String URL = "example";

        String METHOD_NAME = "example";

        String SOAP_ACTION = "example";

        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("PortalId", "0");
            request.addProperty("ModuleId", "1884");

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            ks = (KvmSerializable) envelope.bodyIn;

            //  final SoapObject ret = (SoapObject) ks.getProperty(0);

            Log.i(TAG, "Result Login: " + ks);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

        final ListView myListView = (ListView)findViewById(R.id.lv_main);
        LinearLayout ll = (LinearLayout) findViewById(R.id.lin);


        final ArrayList<String> al = new ArrayList<String>();
        final ArrayAdapter<String> ad;
        ad=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,al);
        myListView.setAdapter(ad);

        Log.i(TAG, "onPostExecute");

        final SoapObject ret = (SoapObject) ks.getProperty(0);

        final GlobalClass globalVariable = (GlobalClass) getApplicationContext();

        for (int i = 0; i < ret.getPropertyCount(); i++) {

            tr = new TableRow(MainActivity.this);
            tr.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            SoapObject Filas = (SoapObject) ret.getProperty(i);
            final String id = Filas.getProperty("GroupId") + "";
            String name = Filas.getProperty("GroupName") + "";

            CamposEntity cs = new CamposEntity();
            cs.Id = id;
            cs.Name = name;
            Log.i(TAG, "CamposEntity ");
            Log.i(TAG, "Result Login: " + id);
            Log.i(TAG, "Result Login: " + name);

            final TextView tv2 = new TextView(MainActivity.this);
            tv2.setText(name);
            tv2.setTextColor(Color.GRAY);
            tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            tv2.setPadding(5, 5, 5, 0);
            tv2.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            tr.addView(tv2);
            // ll.addView(tv2);

            final TextView tv1 = new TextView(MainActivity.this);
            //ll.addView(tv1);
            tv1.setTextColor(Color.GRAY);
            tv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
            tv1.setPadding(5, 5, 5, 0);
            tv1.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            tr.addView(tv1);

            final ToggleButton tb1 = new ToggleButton(MainActivity.this);


            // Boolean b = n.add(id);

            //  Log.i(TAG, "add list: " +b );

            tb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked == true) {

                        String str = id;
                        //    Integer cout = myListView.getCount()+ 1;
                        //  String str1 = cout.toString().concat("."+str);
                        al.add(myListView.getCount(), str);


                        //Collections.sort(al);
                        ad.notifyDataSetChanged();


                        Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_SHORT).show();

                        Log.i(TAG, "array list: " + str);
                        Log.i(TAG, "array list: " + al);

                        tv1.setText(id);

                    } else {

                        for (int i = 0; i < al.size(); i++) {
                            if (al.get(i) == id) {
                                al.remove(i);
                                ad.notifyDataSetChanged();
                            }
                        }
                        Log.i(TAG, "after remove: " + al);
                        tv1.setText("none");
                    }

                }
            });

             tr.addView(tb1);
           // ll.addView(tb1);

            tl.addView(tr, new TableLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        }



        final Button button = new Button(MainActivity.this);
        button.setText("Next");
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                StringBuilder builder = new StringBuilder();

                for (String details : al) {
                    builder.append(details + ",");
                }

                globalVariable.setName(builder.toString());

                Log.i(TAG, "Result Login all textview: " + builder.toString());

                Log.i(TAG, "array list all: " + al);
                Intent i = new Intent(MainActivity.this, SecondScreen.class);
                i.putExtra("al", al);
                startActivity(i);

            }
        });
        ll.addView(button);

    }
}

Any suggestions please...

Thanks in advance.