How to access Activity UI from my class?

前端 未结 7 1158
情歌与酒
情歌与酒 2020-12-01 14:53

I have an activity which creates an object instance of my class:

file MyActivity.java:
public class MyActivity extends Activity {
    TextView myView = (Text         


        
7条回答
  •  再見小時候
    2020-12-01 15:11

    see you post, i've edited it , to fix the problem

    hope it helps :=)

    here is the Edit :

    file MyActivity.java:
        public class MyActivity extends Activity {
        TextView myView ;
        protected void onCreate(android.os.Bundle savedInstanceState) {
            myView = (TextView)findViewById(R.id.myView);
                Points myPoints = new Points(this);
                myPoints.displayMsg("Hello World !!!");
        }  
        }
    
    --------------------------------------------------------------
    
    file Points.java:
    private class Points {
        protected MyActivity context;
        //add a constructor with the Context of your activity
        public Points(MyActivity _context){
            context = _context;
        }
    
        public void displayMsg( final String msg){
            context.runOnUiThread(new Runnable() {
    
                @Override
                public void run() {
                    context.myView.setText(msg);    
                }
            });
        }
    }
    

提交回复
热议问题