【问题标题】:Button click open the another activity in class extends Fragment按钮单击打开类中的另一个活动扩展片段
【发布时间】:2013-02-02 11:12:23
【问题描述】:

我创建了 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");     
                }
         }

【问题讨论】:

    标签: android android-intent android-widget


    【解决方案1】:

    你可以试试:

    startActivity(new Intent(v.getContext(),Activity_One.class));
    

    并确保您在AndroidManifest.xml 中声明了Activity_One

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      • 2015-02-25
      • 2014-07-28
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多