我创建了 ButtonFragment 类,在其中我们实现了视图上的按钮单击方法并识别特定按钮,当我们单击按钮事件时将起作用,但是当我们尝试打开另一个活动时,如 startActivity 方法给出错误。我们怎么能这样做,任何人都可以帮助我。下面是我的代码。
package com.viewpagerex;
import android.content.Context; import android.content.Intent;
import android.os.Bundle; import android.support.v4.app.Fragment;
import android.util.Log; import android.view.LayoutInflater; import
android.view.View; import android.view.View.OnClickListener; import
android.view.ViewGroup; import android.widget.ImageButton;
public class ButtonFragment extends Fragment implements OnClickListener
{
private static final String TAG = null;
protected static final Context context=null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.e(TAG,"HelloHelooooo");
}
public void onActivityCreated(Bundle savedInstanceState)
{ super.onActivityCreated(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.button, container, false);
ImageButton imgBtn = (ImageButton) view.findViewById(R.id.imgButton);
imgBtn.setOnClickListener(this);
return view;
}
public void onClick(View v)
{
if(v.getId()==R.id.imgButton)
{
// TODO Auto-generated method stub
startActivity(new Intent(ButtonFragment.this,Activity_One.class));
Log.e(TAG,"Data show");
}
}