简体   繁体   中英

Accessing private attributes from MainActivity class using java reflection - Field

I am trying to use reflection to use a private attribute from MainActivity Class, but I do not know what I am doing wrong. Could someone give a help. Here is my code : MainActivity class and This is the error I am getting from the exception : java.lang.IllegalArgumentException: object is not an instance of the class

public class MainActivity extends Activity {
/** Called when the activity is first created. */

private  Controller scheduleFor = new Controller();
private TextView display ;
private TextView title ;
   OnClickListener monListener = new OnClickListener(){

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub      
        scheduleFor.handleRequest("monday");}};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    display = (TextView)findViewById(R.id.scheduleView);
    title = (TextView)findViewById(R.id.day);

    ImageButton monButton = (ImageButton)findViewById(R.id.monbutton);
            monButton.setOnClickListener(monListener);
}

public void onStart(){
    super.onStart();
    display.setText("");
    title.setText("Get your schedule");
}
}



public class MonHandler implements Handler {

private static final String TAG = "Scheduler";

@Override
public void handleIt(Object... args){
    // TODO Auto-generated method stub

    try{                                                             
        Class<?> mainActivityClass =  Class.forName("freesoft.nex.ec.MainActivity");


        Field fDisplay = mainActivityClass.getDeclaredField("display");
        Field fTitle = mainActivityClass.getDeclaredField("title");

        fDisplay.setAccessible(true);
        fTitle.setAccessible(true);

        TextView dSchedule = (TextView) fDisplay.get(mainActivityClass);
        TextView dTitle    = (TextView)fTitle.get(mainActivityClass);

        String schedule =   
                " 07h00  Breakfast\n" +
                        " 08h00  Department Metting\n" +
                        " 09h00  Class CIT 310\n"   +       
                        " 10h015 Class CIT 356\n"   +       
                        " 11h30  Class CIT 499\n"   +       
                        " 12h45  Lunch\n"   +       
                        " 14h00  Labs\n"    +
                        " 17h00  Back home\n" +
                        " 21h00  F.H.E - ??\n";

        String sDay = "Monday";


        dSchedule.setText(schedule);
        dTitle.setText("Schedule for " + sDay); 

    }catch (ClassNotFoundException e) {
        e.printStackTrace();        
        Log.d(TAG, "-->" + e);
    }catch(NoSuchFieldException x){
        x.printStackTrace();
        Log.d(TAG, "-->" + x);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d(TAG, "--> " + e.getMessage());
        Log.d(TAG, "--> " + e.getCause());
        Log.d(TAG, "-->" + e.getLocalizedMessage());
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d(TAG, "-->" + e);
    }

}
}

The problem appears to be that you misunderstood the usage of the .get(...) method in the Field class.

TextView dSchedule = (TextView) fDisplay.get(mainActivityClass);
TextView dTitle    = (TextView) fTitle.get(mainActivityClass);

You are passing in a reference to a class, but rather, you should be supplying an running instance/object of that type, as per the documentation and the exception you're seeing. So, what you're really after is passing in an object of freesoft.nex.ec.MainActivity . Now, assuming that is actually identical to the MainActivity class in which your code resides, all you probably need to do is change above statements to:

TextView dSchedule = (TextView) fDisplay.get(MainActivity.this);
TextView dTitle    = (TextView) fTitle.get(MainActivity.this);

Note that you cannot just supply this , since that will refer to the instance of the MonHandler inner class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM