2015-09-20 50 views
0

我无法让按钮在我正在处理的片段中工作。下面是代码:在Android应用程序中单击按钮时没有任何反应

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 


//create button listener for player match 
    View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

    Button mButton = (Button) rootView.findViewById(R.id.MatchButton); 
    Log.v("LOADING MATCH", "TEST"); 

    mButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      Log.v("LOADING MATCH", "TEST2"); 
      Intent intent = new Intent(getActivity(), com.example.android.soccerstar.PlayerDisplay.class); 
      String message = "test"; 
      intent.putExtra(intent.EXTRA_TEXT, message); 
      startActivity(intent); 
     } 
    }); 
    return inflater.inflate(R.layout.fragment_main, container, false); 
} 

MatchButton在片段XML定义如下:

<Button 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:textColor="#ffffff" 
     android:textSize="20dp" 
     android:textStyle="bold" 
     android:text="Match to Player" 
     android:id="@+id/MatchButton"/> 

该应用程序加载罚款,但是当我按一下按钮,没有任何反应。有任何想法吗?我对此很新,所以请耐心等待。

谢谢!

回答

2

onCreateView来改变返回:

return rootView; 

问题发生,因为设置点击监听器按钮,并从onCreateView都返回视图对象是不同的对象。

+0

非常感谢!这解决了这个问题......完全忘了在定义rootview之后改变返回值。 – NBC

相关问题