Reputation: 403
I'm following a tutorial, and I face an error; according to the tutorial the code below must work and invoke the activity
StartingPoint
. However, it doesn't.
I simplified the code and just invoke the activity
no matter what, and it's working, so I just copy these two. Would you please check them?
Not working :
public class Menu extends ListActivity{
String classes[] = { "StartingPoint", "example1", "example2"
, "example3", "example4", "example5", "example6"};
@Override
public 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.thenewboston.travis." + cheese);
Intent ourIntent = new Intent(Menu.this,ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
This code works fine, so other parts of the project is just fine:
public class Menu extends ListActivity{
String classes[] = { "StartingPoint", "example1", "example2"
, "example3", "example4", "example5", "example6"};
@Override
public 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);
Intent ourIntent = new Intent("com.thenewboston.travis.STARTINGPOINT");
startActivity(ourIntent);
}
}
I have a P.S. Here too :)
In the second code I have used capital 'STARTINGPOINT' to invoke the activity
, however, in the original tutorial he used 'StartingPoint', and append this to the name of the class, how does it work?
AndroidManifest.xml
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StartingPoint"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.thenewboston.travis.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.thenewboston.travis.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Thanks. :)
Upvotes: 0
Views: 610
Reputation: 403
I found out what was the problem. I used a different package name.
in the beginning of the project I chose <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thenewboston"
android:versionCode="1"
android:versionName="1.0" >
until this point non of the Intent
caused no error, but I guess when I was using Class ourClass = Class.forName("com.thenewboston.travis." + cheese);
This would go and search in the package, but my package name was com.example.thenewboston
, so I guess it is searching inside the package for a class.
Anyway, after changing everything and moving files to a new package, everything worked!
However, I still don't know why I need not to use capital STARTINGPOINT and StartingPoint is still working. I have a guess, I think it is not looking into the manifest, but it's looking java files, and the name of the file is important. Anyway, I'm a newbie, I'm just guessing. :)
Upvotes: 0
Reputation: 157467
the first example uses reflection, to retrieve the class object which represents the Activity
you want to start. In the second you are using an action to start the next activity. Open your AndroidManifest.xml
and look for com.thenewboston.travis.STARTINGPOINT
, you will find an action associated with an Activity
Edit
"example1", "example2"
, "example3", "example4", "example5", "example6"
have to be Activitiy
subclass and have to be declared on the AndroidManifest.xml file
Upvotes: 1
Reputation: 24848
Try to set class name string using Intent
SetClass() :
Intent intent = new Intent();
intent.setClassName(Menu.this, "com.thenewboston.travis." + cheese);
startActivity(intent);
Upvotes: 0