0

I'm new in android programming . I'm writing an android program which has two tabs: detailstab in which user fill a form and hit save button. listtab in which display what user insert in edittexts control in details tab. to implement this i made this classes:

detail java class :

import android.widget.RadioButton;

import android.widget.TextView;

public class detail {

public   TextView  tvName;
public   TextView  tvAdr;
public   TextView  tvNote;
public  RadioButton rb_0;
public  RadioButton rb_1;
public  RadioButton rb_2;
public int radioValue;
//public  final TextView  tvRadioValue=null;




}

details activity class in which i make an arraylist and add objects from detail:

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class details extends Activity {

public  ArrayList<detail> detaillist=new ArrayList<detail>();
public EditText etName;
public EditText etAdr;
public  EditText etNote;
public  RadioGroup rg;
public  RadioButton rb_0;
public  RadioButton rb_1;
public  RadioButton rb_2;

 static details detailsActivity;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    detailsActivity=this;
    setContentView(R.layout.details_tab);



    Button btnSave=(Button) findViewById(R.id.button1);
    //define an arraylist to add detail object to it 

    etName=(EditText) findViewById(R.id.editText1);
    etAdr=(EditText) findViewById(R.id.editText2);
    etNote=(EditText) findViewById(R.id.editText3);
     rg=(RadioGroup) findViewById(R.id.radioGroup1);
     rb_0=(RadioButton) findViewById(R.id.radio0);
     rb_1=(RadioButton) findViewById(R.id.radio1);
     rb_2=(RadioButton) findViewById(R.id.radio2);

    btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            final detail d=new detail();
            makeDetailList(d);






            //clear textboxes
            clear(etName);
            clear(etAdr);
            clear(etNote);
            rg.check(R.id.radio0);




        }
    });

}

public void clear(View v) {
     EditText edittext=new EditText(null);
     edittext.setText("");

 } 
 public ArrayList<detail> makeDetailList(detail d){



        d.tvName.setText(etName.getText());
        d.tvAdr.setText(etAdr.getText());
        //d.tvNote.setText(etNote.getText());

        if(rb_0.isSelected())
        {
            d.radioValue=0;
            //d.tvRadioValue.setText(rb_0.getText());
        }
        else if (rb_1.isSelected())
        {
            d.radioValue=1;
            //d.tvRadioValue.setText(rb_1.getText());
        }
        else if (rb_1.isSelected())
        {
            d.radioValue=2;
            //d.tvRadioValue.setText(rb_2.getText());
        }


        detaillist.add(d);

    return detaillist;

 }


 public static details getInstance(){
       return   detailsActivity;
     }



}

and finally lists activity class this class contains a listview to display data from details tab:

 public class list extends Activity {

//details details=new details();
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_tab);

    ListView lv=(ListView) findViewById(R.id.listView1);
    //details details=new details();
    //ArrayList mylist=details.getInstance().detaillist;
    MyAdapter adapter=new MyAdapter(this,android.R.layout.simple_list_item_1 ,
            R.id.textView1,
            details.getInstance().detaillist);

    lv.setAdapter(adapter);

//  ArrayAdapter<details> adapter=new ArrayAdapter<details>(this,R.layout.my_list,R.id.textView1,R.id.textView2,));
}
 private class MyAdapter extends ArrayAdapter<String>{

        public MyAdapter(Context context, int resource, int textViewResourceId,
                ArrayList<detail> detaillist) {
            super(context, resource, textViewResourceId);
            // TODO Auto-generated constructor stub
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row=inflater.inflate(R.layout.my_list, parent,false);
            ArrayList<detail> items=details.getInstance().detaillist;

            ImageView ivR=(ImageView) row.findViewById(R.id.imageView1);
            TextView tvN=(TextView) row.findViewById(R.id.textView1);
            TextView tvA=(TextView) row.findViewById(R.id.textView2);

            tvN.setText(items.get(position).tvName.getText());
            tvA.setText(items.get(position).tvAdr.getText());

            if(items.get(position) != null)
            {
                if(items.get(position).radioValue==0)
                {
                    ivR.setImageResource(R.drawable.t);
                }
                else if(items.get(position).radioValue==1)
                {
                    ivR.setImageResource(R.drawable.t);
                }
                else if(items.get(position).radioValue==2)
                {
                    ivR.setImageResource(R.drawable.t);
                }
            }



            return row;
        }
    }
}

I receive a NullPointException error onmakeDetailList(d) method . where is the problem? I really appreciate your help best regards

3 Answers 3

1

You have not initilized the variables in the details calss..

d.tvName.setText(etName.getText());
d.tvAdr.setText(etAdr.getText());

In the above lines tvName,tvAdr are not initialized..ininitlize it and then set text.

Sign up to request clarification or add additional context in comments.

Comments

1

d.tvName.setText(etName.getText());

You may not have set tvName to an object

Comments

0
if ( d.tvName == null )  d.tvName = new TextView(this);
if ( d.tvAdr == null ) d.tvAdr = new TextView(this);

and after that you could set the text like:

d.tvName.setText(etName.getText());
d.tvAdr.setText(etAdr.getText());

2 Comments

FATAL EXCEPTION: main java.lang.NullPointerException at android.view.ViewConfiguration.get(ViewConfiguration.java:331) at android.view.View.<init>(View.java:2745) at android.widget.TextView.<init>(TextView.java:560) at android.widget.EditText.<init>(EditText.java:66) at android.widget.EditText.<init>(EditText.java:62) at android.widget.EditText.<init>(EditText.java:58) at com.elmira.farahani.tejarattest.details.clear(details.java:113)
this line : EditText edittext=new EditText(null);. Change it with this one: EditText edittext=new EditText(this);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.