2013-05-08 130 views
2

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類中寫了那個按鈕點擊函數。

+0

提供日誌.. – bakriOnFire 2013-05-08 07:36:35

+0

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

回答

2

您需要在片段代碼中分配OnClickListener才能使其工作。請參閱Snicolas對「爲什麼」的回答。

+1

我重寫了onStart()方法並在onStart()方法中分配了OnClickListener。它工作得很好。 – 2013-05-08 08:00:44

+0

@ user2361252'onCreateView'或'onViewCreated'可能是更好的選擇。 – 2013-05-08 08:24:49

9

XML onClick的實現方式是針對活動而不是片段。該活動應擁有btnClick1方法,而不是片段。

+0

所以我們如何才能實現onclickListener的片段? – Saad 2013-05-08 07:39:09

+0

我可以知道如何從活動中獲取該btnClick1事件。有沒有辦法做到這一點? – 2013-05-08 07:41:39

+3

最受歡迎的方式是以編程方式使用偵聽器。 xml方式僅用於玩具應用程序。 myButton.setOnClickListener(new OnClickListener(){void onClicked(View v){// yourcode here}}); – Snicolas 2013-05-08 07:42:04