【问题标题】:Code not working in app代码在应用程序中不起作用
【发布时间】:2014-03-26 20:16:38
【问题描述】:

第 24 行 Class is a Raw type.References to generic Type Class<t> should be parametrised 中显示错误。第 24 行是从 try in block 开始的行。之前它工作正常,但现在我出现了这个错误,因此我无法运行它。

package com.example.testing;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends ListActivity {

    String classes[]={"MainActivity","TextPlay","example2","example3","example4","example5"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(Menu.this,android.R.layout.simple_list_item_1,classes));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String cheese=classes[position];
        try {
             Class ourClass = Class.forName("com.example.testing."+ cheese);
             Intent ourIntent=new Intent(Menu.this , ourClass);
             startActivity(ourIntent);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

错误是:

 `Class is a Raw type.References to generic Type Class<t> should be parametrised`

【问题讨论】:

  • 你能告诉我第 24 行的语句吗?
  • Class ourClass = Class.forName("com.example.testing."+ cheese);

标签: java android


【解决方案1】:

我认为这不是错误,应该是警告。

如果您看到Class javadoc file,您会看到forName方法返回Class&lt;?&gt;

因此您可以使用 @SuppressWarning 注释忽略警告,或者您可以提供泛型的类型:

Class<?> ourClass = Class.forName("com.example.testing."+ cheese);

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 2018-11-05
    • 2018-11-22
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多