Android Activity Navigation
I'm very new to android development and I'm working on an application where I have 4 activities. Each activity needs to be able to navigate to any of the other 3. So I created 4 buttons at the top of each activity that allow for this. The XML code looks like this:
<Button ... android:onClick="loadProfileLayout"/>
<Button ... android:onClick="loadRulesLayout"/>
<Button ... android:onClick="loadSettingsLayout"/>
<Button ... android:onClick="loadHelpLayout"/>
the manifest has an activity tag for each:
<activity android:name=".Profiler" 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="Rules"></activity>
<activity android:name="Settings"></activity>
<activity android:name="Help"></activity>
And the functions called are these:
public void loadProfileLayout() { startActivity(new Intent(this, Prof开发者_Python百科iler.class)); }
public void loadRulesLayout(View v) { startActivity(new Intent(this, Rules.class)); }
public void loadSettingsLayout(View v) { startActivity(new Intent(this, Settings.class)); }
public void loadHelpLayout(View v) { startActivity(new Intent(this, Help.class)); }
So initially this works. From the main "Profile" activity I am able to navigate to any of the other 3. And from the other 3 I can navigate anywhere but back to the main one. When I press the main activity button the application crashes. I try and debug, but it doesn't even appear to be executing loadProfileLayout(). Eclipse opens a "View.class" file with the contents of basically "Source not found". If I press F8 to continue debugging it loads "ZygoteInit$MethodAndArgsCaller.run()"... again, "Source not found". Pressing F8 again will load the error message in the emulator "Sorry! The application has stopped unexpectedly. Please try again."
Again, I am new to Android and all I know of activities is what I've been reading on the dev website. Am I making a fundamental mistake here I am not aware of?
Thanks,
NateI'm not sure if this was a typo in your question, but loadProfileLayout()
also needs to take a View
as its only parameter:
public void loadProfileLayout(View v)
Edit: The View parameter is the View that caused the onClick
event (in your case, the Button
instance). I haven't looked at the code, but I assume that View is using reflection to find the method to call (specifically one that takes a View as an argument), and since it doesn't find a matching method, it decides to throw an exception.
精彩评论