1

I'm trying to make my first list adapter with android, and it comes up with the list, but when I click the items, nothing happens. My only guess is the comment I have after the line that defines Class cvar.

package scouting.form;

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[] = {"SCOUTING","LOGIN","MAIN"};

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        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)
    {
        super.onListItemClick(l, v, position, id);
        String cheese= "com.cody.graham."+classes[position];
        try{
            Class cvar=Class.forName(cheese);     //Eclipse has a warning on this line "Class is a raw type. References to generic type Class<T> should be parameterized". I don't really know what that means.
            Intent intention=new Intent(Menu.this,cvar);
            startActivity(intention);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}

Edit:

Part of the AndroidManifest:

<activity android:name=".splash_screen" android:label="@string/title_activity_splash" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name=".Login" android:label="@string/title_activity_login" android:exported="false">
    <intent-filter>
        <action android:name="com.cody.graham.LOGIN" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

<activity android:name=".Scouting" android:label="@string/title_activity_scouting" android:exported="false">
    <intent-filter>
        <action android:name="com.cody.graham.SCOUTING" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
bluebl1
  • 176
  • 1
  • 4
  • 13
  • Logcat output? My hunch is that the classes can't be found with those names. Do you know you can create a list of the actual classes? – dmon Nov 08 '12 at 23:46

2 Answers2

3
  String classes[] = {"SCOUTING","LOGIN","MAIN"};

Try naming the classes how it appears. like Scouting

Also trying changing your manifest:

<activity android:name=".Scouting" />
This is what you only need in this case.

Eclipse has a warning on this line "Class is a raw type. References to generic type Class should be parameterized". This is a warning, it will not give you an error in this case. What is means is Class , T is a class type of what you are getting. ex:

Class<? extends Activity> cvar=Class.forName(cheese);

For more information : link

Community
  • 1
  • 1
wtsang02
  • 18,603
  • 10
  • 49
  • 67
  • 1
    As an additional note, All things in Java are case-sensitive. Class lookup is no exception - you could easily have one class named Scouting and a completely different one named SCouTing – JRaymond Nov 08 '12 at 23:28
  • I know. Before I got this post, I tested with this because I have the class named "Scouting", the manifest sets its name to "com.cody.graham.SCOUTING", and somewhere else I use "scouting". I made the list {"SCOUTING","Scouting","scouting"} as a test, and none of the 3 buttons did anything. – bluebl1 Nov 09 '12 at 00:10
  • I don't see any thing wrong with java code. Can you double check your package name? – wtsang02 Nov 09 '12 at 00:17
  • The code runs, and the menu appears, but when I click the menu, nothing happens. PS the Scouting class does work because it worked before I added the menu and I haven't changed it at all. – bluebl1 Nov 09 '12 at 00:24
  • I'm pretty sure that it has to do with the warning "Class is a raw type. References to generic type Class should be parameterized". The problem is I don't know what that means or how to 'parameterize' the class. – bluebl1 Nov 09 '12 at 00:27
1

Firstly, remove the <intent-filter> sections from your manifest for both .Scouting and .Login (leave the one for .splash_screen). Unless you need any of your Activity classes to be started from 3rd-party (external) apps, it is not necessary to declare action-based <intent-filter> sections - it's also not good practice.

Secondly, do as wtsang02 suggests and change the classes string array to...

String classes[] = {"Scouting","Login","splash_screen"};

Also, assuming the other Activity classes (such as Scouting etc) are in the same package as Menu, change this line...

String cheese= "com.cody.graham."+classes[position];

to be...

String cheese= "scouting.form." +classes[position];

...and to get rid of the eclipse warning, this line...

Class cvar=Class.forName(cheese);

...to...

Class<?> cvar=Class.forName(cheese);
Squonk
  • 48,735
  • 19
  • 103
  • 135
  • It still doesn't do anything :( – bluebl1 Nov 09 '12 at 00:56
  • @bluebl1 : I edited just before you posted your comment. Re-check my code change suggestions and make sure you're using the correct package name for the `cheese` string. – Squonk Nov 09 '12 at 01:08
  • I was wondering how you could remove the intent filter and still use "com.cody.graham." But I still have the intent filter because I do use that a little. – bluebl1 Nov 09 '12 at 01:13
  • @bluebl1 : To use the `` actions, you create the `Intent` in a different way and don't specify the `Context` or `Class>` parts. Instead you simply pass a string to the `Intent` constructor to specify the action. Example - `Intent intention=new Intent("com.cody.graham.SCOUTING");` would work but this is called an "implicit intent". When specifying the `Class>` parameter to be used, it is an "explicit intent". The strings for actions in the `` section can be anything - the `Class>` for explicit intents must, however, have a fully qualified package name etc. – Squonk Nov 09 '12 at 01:20