activity_main.xml中按鈕點擊不起作用
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<fragment
android:id="@+id/fragment1"
android:name="sithi.test.fragmenttest.Fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="btnClick1" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
ActivityMain.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Fragment1.java
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Fragment1 extends Fragment
{
TextView tv;
@Override
public void onStart()
{
// TODO Auto-generated method stub
super.onStart();
tv=(TextView)getView().findViewById(R.id.textView1);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
// TODO Auto-generated method stub
//inflater.inflate(resource, root, attachToRoot);
return inflater.inflate(R.layout.fragment1, container, false);
}
public void btnClick1(View view)
{
tv.setText("dsdsdasda");
}
}
我創建的XML文件和類這樣的,但在btnClick1()
Android的碎片也不能正常工作。 當我點擊片段中的按鈕時,它會出現錯誤。我在Fragment類中寫了那個按鈕點擊函數。
提供日誌.. – bakriOnFire 2013-05-08 07:36:35
05-08 12:44:43.156:W/dalvikvm(29123):threadid = 1:線程以未捕獲的異常退出(group = 0x40aa6210) 05-08 12:44:43.436:E/AndroidRuntime(29123):致命異常:main 05-08 12:44:43.436:E/AndroidRuntime(29123):java.lang.IllegalStateException:在活動類sithi.test中找不到方法btnClick1(View)。 fragmenttest.MainActivity爲視圖類android.widget.Button ID爲'button1'的onClick處理程序 – 2013-05-08 07:44:14