2013-02-02 51 views
1

我已經創建了ButtonFragment類,其中我們實現了 按鈕的點擊方法,查看並確定了特定的按鈕,當我們點擊按鈕事件時會起作用,但是當我們嘗試打開 另一個活動,如startActivity方法給出error.How我們可以做到這一點 ,任何人都可以幫助我。以下是我的代碼。按鈕點擊打開課程中的另一個活動擴展片段

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");  
       } 
     } 

回答

1

你可以嘗試如下:

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

,並確保你已經在AndroidManifest.xml

+0

thnks將工作宣告Activity_One。 – user2017942

相關問題