【问题标题】:Invoke Method using Reflection使用反射调用方法
【发布时间】:2016-01-26 21:46:10
【问题描述】:

我有两节课。我想在 QuestionPicker.class 中调用“saveq”方法。此方法将一些字符串保存到 SharedPreferences 中。我想从另一个名为 Questions 的类中调用该方法。 我想使用反射调用该方法,但我的代码不起作用.. 我认为问题出在反射的某个地方,但我看不到。 有人可以看到问题吗?谢谢

这里是 QuestionPicker.class

public class QuestionPicker {

    public void saveq() {

        // MyApplication Class calls Context
        Context context = MyApplication.getAppContext();

        // Values to save
        String Q1 = "SomeQuestion";
        String Q2 = "SomeOtherQuestion";
        String Q3 = "AndEvenMoreQuestions";

        // Save the Strings to SharedPreferences
        SharedPreferences.Editor editor = context.getSharedPreferences("QuestionsSaver", 0).edit();

        editor.putString("Q1", Q1);
        editor.putString("Q2", Q2);
        editor.putString("Q3", Q3);

        editor.apply();

    }
}

这里是 Questions.class

public class Questions extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questions);

        // storing string resources into Array
        String[] subjects = getResources().getStringArray(R.array.subjects);
        ListView lv = (ListView) findViewById(R.id.listView);
        // Binding resources Array to ListAdapter
        ListAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, subjects);
        lv.setAdapter(adapter);


        // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

          // Invoke the saving Method  
                try {
                    Class cls = Class.forName("com.test.QuestionPicker");
                    Object obj = cls.newInstance();
                    Method method = cls.getMethod("saveq");
                    method.invoke(obj);

                } catch(Exception ex){
                    ex.printStackTrace();
                }



            }
        });
    }
}

【问题讨论】:

  • 方法“saveq”是公开的,使用反射的原因是什么?
  • 我稍微简化了课程。我需要通过 Questions.class 中的不同变量名调用几种方法
  • “不起作用”是什么意思?

标签: java android methods reflection invoke


【解决方案1】:

我同意@Joni - 什么不起作用?例如,下面的代码确实“工作”。我把它去掉了Android,但概念应该是一样的。

package com.company;

public class QuestionPicker {
    public void saveq() {
        System.out.println( "saveq is called");
    }
}

package com.company;

import java.lang.reflect.Method;

public class Main {

public static void main(String[] args) {
        try {
            Class clazz = Class.forName("com.company.QuestionPicker");
            Object obj = clazz.newInstance();
            Method method = clazz.getMethod("saveq");
            method.invoke(obj);
        } catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-08
    • 2011-06-21
    • 2018-02-18
    相关资源
    最近更新 更多