2014-08-29 22 views
-1

現在我知道人已經問過這個問題之前很多次,我讀了所有這些職位的:按鈕偵聽不會對分段響應

NullPointerException with Fragment Interface Listener

Click listener inside a fragmentactivity with Page Adapter

button inside a fragment doesn't work

https://stackoverflow.com/questions/22286220/my-button-action-is-not-working-in-tabs-view-using-fragments

button inside a fragment doesn't work

findViewById in Fragment

和我還是不知道如何使這個簡單的代碼工作。 所以我創建了一個viewpager 2個片段,在片段一個有一個按鈕,我點擊它每次onClickListener沒有預製的,沒有例外。

片段活性的java

public class Asec extends FragmentActivity { 
    ViewPager vp = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_asec); 
     vp = (ViewPager) findViewById(R.id.pager); 
     vp.setAdapter(new ma(getSupportFragmentManager())); 

    } 
} 

class ma extends FragmentPagerAdapter { 

    public ma(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int arg0) { 
     Fragment a = null; 
     if (arg0 == 0) { 
      a = new A(); 
     } 
     if (arg0 == 1) { 
      a = new b(); 
     } 
     return a; 
    } 

    @Override 
    public int getCount() { 
     return 2; 
    } 
} 

片段活性的xml

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="match_parent" 
    android:id="@+id/pager" 
    android:layout_width="match_parent">   
    </android.support.v4.view.ViewPager> 

片段A的java

public class A extends Fragment{ 
    Button b; 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
      @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View view = (RelativeLayout) inflater.inflate(R.layout.av, container, false);//this is line A 
     b = (Button) view.findViewById(R.id.button1);//this is line B 
     b.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       System.out.println("work");//this is not printed 

      } 
     }); 
     return inflater.inflate(R.layout.av, container, false); 
    } 

} 

我嘗試另一種方法爲線A等getView()。findViewById(。 ..),它仍然不能正常工作

片段A 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:background="#33D692"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="23dp" 
     android:layout_marginTop="28dp" 
     android:text="Fragment A" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/button1" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="53dp" 
     android:text="Button" /> 

</RelativeLayout> 

片段B的Java(從現在開始,並不重要)

public class b extends Fragment{ 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
      @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     return inflater.inflate(R.layout.bv, container, false); 
    } 


} 

片段B 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:background="#F53636" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="23dp" 
     android:layout_marginTop="28dp" 
     android:text="Fragment B" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

我知道有很多的疑問像這樣,我實際上閱讀所有這些,而非答案真的很有幫助。 thanx評論此帖子。

回答

0

解決方案是使用一個活動,並更改一些代碼。 XML文件可能是這樣的:

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="match_parent" 
    android:id="@+id/pager" 
    android:layout_width="match_parent"> 

<fragment 
     android:id="@+id/fragment_a" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:name="logical.address.of.your.Fragment"/> 

</android.support.v4.view.ViewPager> 

你改變你的Activity類以這樣的方式

mActionFragment = (ClassA)getFragmentManager().findFragmentById(R.id.fragment_a); 

我希望它的作品。

0

在你的片段你充氣佈局兩次:

View view = (RelativeLayout) inflater.inflate(R.layout.av, container, false);//this is line A 
//... 
return inflater.inflate(R.layout.av, container, false); 

你設置點擊監聽器上的佈局不是唯一的顯示在屏幕上的相同。

返回您充氣的view並設置點擊偵聽器,並且不會膨脹另一個佈局。

0

我不知道是否有幫助了,但:

@Override 
public void onClick(View v) { 
    System.out.println("work");//this is not printed 

} 

這不會起作用,因爲System.out.println("")不會在Android中使用。相反,請使用Log.d(tag, message)作爲調試消息,或使用Log.e(tag, message)作爲錯誤。