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 (对于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 (适用于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);
}
}
此代码生成以下UI。请注意,您看到的“菜单”是由drop_down.xml
构建的。
问题是,当我点击 drop_down.xml 中的TextView
menu_add_inquiry 时,我收到以下错误。
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
它说方法丢失了,但它就在那里。
更新
我使用完整定义com.example.fragmenttest.DropDownMenu.onMenuItemClicked
编辑了 drop_down.xml 中的按钮。现在我有以下问题
11-28 14:49:45.757:E / AndroidRuntime(2604):引起:java.lang.NoSuchMethodException:com.example.fragmenttest.DropDownMenu.onMenuItemClicked [class android.view.View]
答案 0 :(得分:4)
在drop_down.xml
布局中,调用MainActivity类中不存在的onMenuItemClicked方法。
对于布局,它是将被调用的活动的方法(而不是片段之一)。
您可能希望使用相同的方法替换您的方法,但是在MainActivity类中。
您可以阅读Android android:onclick的文档:
此名称必须对应于只接受View类型的一个参数的公共方法。例如,如果指定android:onClick =“sayHello”,则必须声明上下文的公共void sayHello(View v)方法(通常是您的活动)。