fragment_view1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/viewOneText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="92dp"
android:layout_marginTop="182dp"
android:text="First View"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/viewOneBtn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/viewOneText"
android:layout_below="@+id/viewOneText"
android:layout_marginTop="17dp"
android:text="Click Here" />
<include layout = "@layout/drop_down"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
drop_down.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SlidingDrawer
android:id="@+id/SlidingDrawer"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:content="@+id/contentLayout"
android:handle="@+id/slideButton"
android:orientation="vertical"
android:alpha="0.7">
<LinearLayout
android:id="@+id/slideButton"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:orientation="vertical"
android:clickable="true"
android:gravity="right"
android:background="@android:drawable/bottom_bar"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="^^^"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:id="@+id/contentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000000"
android:padding="10dip" >
<TextView
android:id="@+id/menu_add_inquiry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="AAA"
android:textColor="#ffffff"
android:clickable="true"
android:onClick="onMenuItemClicked"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/menu_add_event"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="BBB"
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/menu_additional_info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="CCC"
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/menu_add_to_contacts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="DDD"
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
FirstView.java (for fragment_view1.xml)
package com.example.fragmenttest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class FirstView extends DropDownMenu
{
private TextView firstText;
private Button btn;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_view1,container,false);
firstText = (TextView)view.findViewById(R.id.viewOneText);
btn = (Button)view.findViewById(R.id.viewOneBtn);
return view;
}
}
DropDownMenu.java (for drop_down.xml)
package com.example.fragmenttest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class DropDownMenu extends Fragment {
private TextView addInquiry, addEvent, additionalInfo, addToContacts;
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.drop_down,container,false);
intialize();
return view;
}
private void intialize()
{
//Intializing instance variables
addInquiry = (TextView)view.findViewById(R.id.menu_add_inquiry);
addEvent = (TextView)view.findViewById(R.id.menu_add_event);
additionalInfo = (TextView)view.findViewById(R.id.menu_additional_info);
addToContacts = (TextView)view.findViewById(R.id.menu_add_to_contacts);
}
public void onMenuItemClicked(View view) {
switch (view.getId()) {
case R.id.menu_add_inquiry:
// Intent intent = new Intent(DropDownMenu.this,NewLead.class);
// startActivity(intent);
break;
default:;
}
}
}
MainActivity.java
package com.example.fragmenttest;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
private ViewPager viewPager;
private MyAdapter pageAdapter;
private static final int ITEMS = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.pager);
pageAdapter = new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(pageAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public int getCount() {
return ITEMS;
}
@Override
public Fragment getItem(int position) {
if(position==0)
{
return new FirstView();
}
else
{
return new SecondView();
}
}
}
public void setCurrentItem (int item, boolean smoothScroll) {
viewPager.setCurrentItem(item, smoothScroll);
}
}
This code generate the following UI. Note that what you see as a "Menu" is built by drop_down.xml
.
The problem is, when I click the TextView
menu_add_inquiry in drop_down.xml, I get the following error.
11-28 14:13:52.537: E/AndroidRuntime(1933): FATAL EXCEPTION: main
11-28 14:13:52.537: E/AndroidRuntime(1933): java.lang.IllegalStateException: Could not find a method onMenuItemClicked(View) in the activity class com.example.fragmenttest.MainActivity for onClick handler on view class android.widget.TextView with id 'menu_add_inquiry'
11-28 14:13:52.537: E/AndroidRuntime(1933): at android.view.View$1.onClick(View.java:3586)
11-28 14:13:52.537: E/AndroidRuntime(1933): at android.view.View.performClick(View.java:4204)
11-28 14:13:52.537: E/AndroidRuntime(1933): at android.view.View$PerformClick.run(View.java:17355)
11-28 14:13:52.537: E/AndroidRuntime(1933): at android.os.Handler.handleCallback(Handler.java:725)
11-28 14:13:52.537: E/AndroidRuntime(1933): at android.os.Handler.dispatchMessage(Handler.java:92)
11-28 14:13:52.537: E/AndroidRuntime(1933): at android.os.Looper.loop(Looper.java:137)
11-28 14:13:52.537: E/AndroidRuntime(1933): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-28 14:13:52.537: E/AndroidRuntime(1933): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 14:13:52.537: E/AndroidRuntime(1933): at java.lang.reflect.Method.invoke(Method.java:511)
11-28 14:13:52.537: E/AndroidRuntime(1933): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-28 14:13:52.537: E/AndroidRuntime(1933): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-28 14:13:52.537: E/AndroidRuntime(1933): at dalvik.system.NativeStart.main(Native Method)
11-28 14:13:52.537: E/AndroidRuntime(1933): Caused by: java.lang.NoSuchMethodException: onMenuItemClicked [class android.view.View]
11-28 14:13:52.537: E/AndroidRuntime(1933): at java.lang.Class.getConstructorOrMethod(Class.java:460)
11-28 14:13:52.537: E/AndroidRuntime(1933): at java.lang.Class.getMethod(Class.java:915)
11-28 14:13:52.537: E/AndroidRuntime(1933): at android.view.View$1.onClick(View.java:3579)
11-28 14:13:52.537: E/AndroidRuntime(1933): ... 11 more
It says that method is missing, but it is there.
UPDATE
I edited the button in drop_down.xml with the full definition com.example.fragmenttest.DropDownMenu.onMenuItemClicked
. Now I have the following issue
11-28 14:49:45.757: E/AndroidRuntime(2604): Caused by: java.lang.NoSuchMethodException: com.example.fragmenttest.DropDownMenu.onMenuItemClicked [class android.view.View]
In your drop_down.xml
layout, you call onMenuItemClicked method, that does not exist in MainActivity class.
In the case of your layout, it is the method of the activity that will be called (and not the one of the fragment).
You may want to replace your method by same method, but in your MainActivity class.
You may read documentation of Android android:onclick :
This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).