简体   繁体   中英

Type Class is a raw type. References to generic type Class<T> should be parameterized

Class activityItems work fine but i get warning "Type Class is a raw type. References to generic type Class should be parameterized". How to fix it ?

Here is my code:

public class MainActivity extends ListActivity {

    String classes[] = { "AccountActivity", "CountsActivity", "" };
    String listItems[] = { "Accounts", "Counts", "temp" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View layout = findViewById(R.id.container);
        Drawable background = layout.getBackground();
        background.setAlpha(110);

        setListAdapter(new ArrayAdapter<String>(this, 
                android.R.layout.simple_list_item_1, listItems));

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        String item = classes[position];
        try {
            Class activityItems = Class.forName("com.arbaz.hk." + item);
            Intent intent = new Intent(this, activityItems);
            startActivity(intent);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

I think it may just be a case of changing this:

Class activityItems = Class.forName("com.arbaz.hk." + item);

to:

Class<?> activityItems = Class.forName("com.arbaz.hk." + item);

That way you're still not saying you know what the T in Class<T> is, but you're demonstrating that you're at least aware that it is a generic type, so the compiler won't erase all trace of generics from the signatures etc as it would otherwise.

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