按钮单击事件未在ANDROID </include>中的<include>标记中正确触发

时间:2014-09-19 10:52:32

标签: android

我有三个XML文件

1)标题 2)FirstActivity 3)SecondActivity。

header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button 
android:text="Button 1" 
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:onClick="first1"
>
</Button>

<Button 
android:text="Button 2" 
android:id="@+id/button2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:onClick="second"
>
</Button>
</LinearLayout>

firstactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout      xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<include layout="@layout/headerfile"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="100dip"
>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Detail"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/simid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/imeino"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/phoneno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />  
</LinearLayout>
</RelativeLayout>

HeaderActivity.java

public class HeaderActivity extends Activity {
Button b1,b2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.headerfile);


b1 = (Button)findViewById(R.id.button1) ;
b2 = (Button)findViewById(R.id.button2) ;

} //onCreate over

public void first1(View v)
{
Intent i =new Intent(this,firstactivity.class);
startActivity(i);
finish();       
}

public void second(View v)
{
Intent i1 =new Intent(this, secondactivity.class);
startActivity(i1);  
finish();
}
}

但我每次都得到错误......

Log-cat:

ERROR/AndroidRuntime(328): java.lang.IllegalStateException: Could not find a method first1
(View) in the activity class com.contactDetails.ContactDetailsActivity for onClick handler on view class 
android.widget.Button with id 'button1'

2 个答案:

答案 0 :(得分:1)

在ContactDetailsActivity.java类中,您应该添加以下方法

public void first1(View v)
{
// onlick code 
}

答案 1 :(得分:0)

<include>标记包含根布局中的所有元素。因此,您始终可以使用findViewById上的Activity访问它们。

您可以使用其他方式处理onClickListener的<{1}} ..

您无需单独编写Button来处理包含布局视图....

您可以直接从使用过视图的活动(即firstactivity.xml)访问这些按钮。

试试这个..

Activity

并从Button b1 = (Button) findViewById(R.id.button1); Button b2 = (Button) findViewById(R.id.button2); b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i =new Intent(this,firstactivity.class); startActivity(i); finish(); } }); b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i1 =new Intent(this,secondactivity.class); startActivity(i1); finish(); } });

删除onClick属性