-1

伙计们,你能检查一下为什么我在这一行得到一个空指针异常,

cl = Class.forName(myClass);

这是代码:

private void addBookmark(String[] values_array) {       
    LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = vi.inflate(R.layout.single_bookmark, null);

    TextView text = (TextView) v.findViewById(R.id.bookmark_text);
    Button button = (Button) v.findViewById(R.id.bookmark_button);

    text.setText(values_array[1]);

    final String myClass = values_array[0];
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Class<?> cl = null;
            try {
            cl = Class.forName(myClass); // <--- this is where i am getting a null pointer exception
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
        }
            Intent myIntent = new Intent(mContext, cl);
            startActivity(myIntent);
        }
    });

我应该如何解决这个问题?或者有什么我需要更换的吗?

4

3 回答 3

0

OP 要求将我的评论作为答案发布>

真的不应该使用单个String[]来将语义上不同类型的数据保存在一起!

第一个元素是名,第二个是标签语义:你应该为这个方法签名,让它们分开!单个数组是不可读的、非结构化的、通常不可维护的!

(我还在这里添加了一个条件来检查 className 中的 null ......我假设 className 为 null 是一个致命错误。)

private void addBookmark(String labelText, String className) {      
      //check className not to be null
      if(className==null) {
          //handle error somehow, log error, etc
          ...
          //no point in doing anything the listener, it would not do any good, just return
          return;
      }

    LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = vi.inflate(R.layout.single_bookmark, null);

    TextView text = (TextView) v.findViewById(R.id.bookmark_text);
    Button button = (Button) v.findViewById(R.id.bookmark_button);

    text.setText(labelValue); // variable "labelValue"

    final String myClass = className; //variable className
      //checking for null

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Class<?> cl = null;
            try {
            cl = Class.forName(myClass); 
              } catch (ClassNotFoundException e) {
                e.printStackTrace();
                //this case should be handled better.
            }
            Intent myIntent = new Intent(mContext, cl);
            startActivity(myIntent);
        }
    });

当然,当你调用这个方法的时候,你也应该把代码从:

addBookmark(anArrayOfStrings);

addBookmark(myLabelValueString, myClassNameString);

其中 myLabelValueString、myClassNameString 是包含相应值的字符串变量。

于 2013-09-12T09:04:14.867 回答
0

Java Docs 说什么 Class.forName(); 如果找不到该类,将返回 ClassNotFoundException 所以我会说它永远不会返回 null。

查看您的日志。可能是java无法通过名称找到类

String myClass = values_array[0];
于 2013-09-12T08:10:02.090 回答
0
    public void onClick(View v) {
        Class<?> cl = null;
        try {
            Log.e("onClick", "myClass="+myClass);
        cl = Class.forName(myClass); // <--- this is where i am getting a null pointer exception
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
    }

确保“myClass”不为空;

于 2013-09-12T08:39:39.240 回答