Calling android widget method of Activity class from another class?

Tags: android
Question!

i wanna call a method of Activity Class to normal public class. but it faced NullPointerException.

My Activty (Lv.class)

public class Lv extends Activity {
        public static EditText et1; 
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listandimage);
            et1 = (EditText) findViewById(R.id.editText1);

My Public class (Data.class)

        public class Data {             
            public String c = "";
            public String d = "";
            private static Socket soc;

            public Data(String var1, String var2) {
                this.c = var1.toLowerCase().trim();
                this.d = var2;
            }

    public final void start() {

            try {
                soc = new Socket(address, port);
                os = soc.getOutputStream();
                is = new DataInputStream(soc.getInputStream());
            } catch (Exception var5) {
            }
    }

        public void processdata(String rcvdata){
String txttype = rcvdata;
        if (txttype.equals("\003")) {
                        String rname="my man";
                        Lv.et1.setText(rname);
    }

MainActivity.class

button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        new RTask().execute();
    }
});
class RTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... Void) {
        try {
            mee = new Data(editText1.getText().toString(), editText2
                    .getText().toString());
            mee.start();
        } catch (Exception e) {
        }
        return null;
    }

Data.class is a public class which sythesize data received from socket connection. Yes.my layout name,content name is ok,when i use that from activity button onclick,it works. Somehow,every command of activity class

(e.g gettext,button on click,Calling adapter notify[made them public static also]) via this public class will remain NullPointerException.

please help me to sort out this problem.



Answers
  1. In your case you shouldnt set EditText as static.
  2. Pass this EditText to your Data class. So you can call it inside Data class.

    class Data {
        //make it field
        private EditText editText;
        //your constructor of Data class
        Data (EditText editText) {
        this.editText = editText;
        }
        //now you can use editText like you are in Activity
        private myMethod() {
        editText.setText("every String you want to see here");
        }
    }
    

Now when you create Data from Activity just pass EditText

Data data = new Data(editText);

Same way in you need to pass Activity.

If you need to change more than one EditText inside your Data class its better to pass activity into it. Then create multiple public methods to change values of views.

First of all you should know that static variable is linked to class, not to object. So for sure its null for you in your case because onCreate method where you initialize EditText is not static.

By : DEADMC


Consider writing the entire query in one SELECT (using bracketing, ANDs, ORs), this removes all the inefficient duplicate checking of UNION.

You can try UNION ALL as Pawel-Dyl suggests, but if you can make that work, you can probably write it all in one SELECT, and this gives the optimizer more options.

By : RobertB


I think you could rewrite this as a single query on the status table, using ORs to test each scenario, like so:

SELECT ss.int_tran_id
FROM   status SS
WHERE  ss.stage in ('ACHPayment_Confirmed', 'HIFV4', 'HIFV5_FTRINF', 'Payment_HIFV5_FTRINF')
OR     (ss.stage = 'PREVDAY'
        AND (EXISTS (SELECT NULL
                     FROM   references rf
                     WHERE  ss.int_tran_id = rf.int_tran_id
                     AND    rf.mid_ref IS NOT NULL)
             OR EXISTS (SELECT NULL
                        FROM   app_data ad
                               INNER JOIN ach aa
                                 ON (ad.app_data_id = aa.ach_id)
                        WHERE  ss.int_tran_id = ad.int_tran_id
                        AND    aa.par_number IS NOT NULL)));
By : Boneist


This video can help you solving your question :)
By: admin