Reputation: 5063
In this program I implemented the sliding menu library. The problem that I am having now is setting up the on click listeners for these menus.
The library requires the menu to be a different layout, or atleast that is the way that I have implemented it. What I would like to achieve now is to implement various onClick listeners to these menu options.
The program looks like this(a short part of it):
menu.xml
<RelativeLayout android:id="@+id/ask_a_question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="5dp"
android:onClick="getQuestion">
<TextView android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ask_a_question"
android:textColor="#ffffff"
android:layout_marginLeft="15dp"
android:layout_centerVertical="true"/>
<ImageView android:layout_alignParentRight="true"
android:contentDescription="@string/ask_a_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/question"
android:layout_marginRight="15dp"/>
</RelativeLayout>
and on its MainActivity.java the program looks like this:
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.menu);
getSlidingMenu().setBehindOffset(100);
ask_a_question = (RelativeLayout)findViewById(R.id.ask_a_question);
ask_a_question.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getQuestion();
}
});
login = (Button)findViewById(R.id.log_in_button);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.example.btshome.LOGINACTIVITY"));
}
});
}
public Intent getQuestion()
{
Intent i = new Intent("com.example.btshome.ASKPAGE");
startActivity(i);
return null;
}
which returns the following error:
04-23 19:43:30.030: E/AndroidRuntime(845): FATAL EXCEPTION: main
04-23 19:43:30.030: E/AndroidRuntime(845): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.btshome.ASKPAGE }
04-23 19:43:30.030: E/AndroidRuntime(845): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
04-23 19:43:30.030: E/AndroidRuntime(845): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
04-23 19:43:30.030: E/AndroidRuntime(845): at android.app.Activity.startActivityForResult(Activity.java:3351)
04-23 19:43:30.030: E/AndroidRuntime(845): at android.app.Activity.startActivityForResult(Activity.java:3312)
04-23 19:43:30.030: E/AndroidRuntime(845): at android.app.Activity.startActivity(Activity.java:3522)
04-23 19:43:30.030: E/AndroidRuntime(845): at android.app.Activity.startActivity(Activity.java:3490)
04-23 19:43:30.030: E/AndroidRuntime(845): at com.example.btshome.MainActivity.getQuestion(MainActivity.java:47)
04-23 19:43:30.030: E/AndroidRuntime(845): at com.example.btshome.MainActivity$1.onClick(MainActivity.java:30)
04-23 19:43:30.030: E/AndroidRuntime(845): at android.view.View.performClick(View.java:4084)
04-23 19:43:30.030: E/AndroidRuntime(845): at android.view.View$PerformClick.run(View.java:16966)
04-23 19:43:30.030: E/AndroidRuntime(845): at android.os.Handler.handleCallback(Handler.java:615)
04-23 19:43:30.030: E/AndroidRuntime(845): at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 19:43:30.030: E/AndroidRuntime(845): at android.os.Looper.loop(Looper.java:137)
04-23 19:43:30.030: E/AndroidRuntime(845): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-23 19:43:30.030: E/AndroidRuntime(845): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 19:43:30.030: E/AndroidRuntime(845): at java.lang.reflect.Method.invoke(Method.java:511)
04-23 19:43:30.030: E/AndroidRuntime(845): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-23 19:43:30.030: E/AndroidRuntime(845): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-23 19:43:30.030: E/AndroidRuntime(845): at dalvik.system.NativeStart.main(Native Method)
The manifest page:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.btshome.MainActivity"
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=".LoginActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.example.btshome.LOGINACTIVITY"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".SignUp"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.example.btshome.SIGNUP"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".LoginPage"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.example.btshome.LOGINPAGE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity
android:name=".QuestionAsking"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.btshome.ASKPAGE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Is the approach that I taking to implement this menu item on click good? or is it a bad way to go about it, because I will have to have the sliding menu available on all my pages. Help Please.
Upvotes: 1
Views: 3746
Reputation: 6857
You probably don't have ASKPAGE
Activity in your manifest file.
In AndroidManifest.xml, inside application
tag, add:
<activity android:name=".ASKPAGE">
EDIT:
Your getQuestion()
method should look like this:
public Intent getQuestion()
{
Intent i = new Intent("com.example.btshome.ASKPAGE"); // Set an action in constructor
i.setClass(MainActivity.this, QuestionAsking.class); // Set an Activity class
startActivity(i);
return null;
}
And just a note - I hope you see that you always return null
in this method.
Upvotes: 3
Reputation: 3304
You have to explicitly declare an intent filter in at least one of your activities that will handle your intent. For more information you can check here. As an example you should do something like:
<activity
android:name="com.example.btshome.LoginActivity">
<intent-filter>
<action android:name="com.example.btshome.LOGINACTIVITY" />>
<category android:name="com.example.btshome />
</activity>
Upvotes: 0