2013-12-17 124 views
0

好吧,在我解釋之前,我還不是Android編程方面的專家,所以請原諒我,如果我做出愚蠢的問題。片段按鈕問題Android

我試圖在我的Java類中實現的代碼是處理Buttons和Fragments。我想在一個片段類中放一個按鈕,以便進入一個網站。即(Facebook的Instagram),但我不知道爲什麼我在活動=這個錯誤;我希望有人幫助我並向我解釋(如果可能的話)爲什麼會這樣,以及我將來可以做些什麼來防止再次發生此錯誤。

這裏是Java代碼:

package info.androidhive.slidingmenu; 

import android.app.Activity; 
import android.app.Fragment; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageButton; 


public class NetworksActivity extends Fragment implements OnClickListener{ 

ImageButton imgButtonInstagram; 
ImageButton imgButtonFacebook; 
Activity activity; 

public void onCreate (Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.networks_layout); 
} 
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, 
      Bundle saedInstanceState) { 
     View rootView = inflater.inflate(R.layout.networks_layout, parent, false); 

    activity = this; 
    imgButtonInstagram = (ImageButton)rootView.findViewById(R.id.imgBtnInsta); 
    imgButtonFacebook = (ImageButton)rootView.findViewById(R.id.imgBtnFace); 
    imgButtonFacebook.setOnClickListener(listener); 
    imgButtonInstagram.setOnClickListener(listener); 

    return rootView; 
    } 

private View.OnClickListener listener = new View.OnClickListener() { 
    public void onClick(View v) { 

     switch (v.getId()){ 
      case (R.id.imgBtnInsta): 
       startActivity(new Intent(Intent.ACTION_VIEW, 
         Uri.parse("http://instagram.com/m4lvip"))); 

       break ; // add here 

      case (R.id.imgBtnFace): 
       startActivity(new Intent(Intent.ACTION_VIEW, 
         Uri.parse("https://www.facebook.com/"))); 

       break ;// add here 

     } 


    } 
}; 

@Override 
public void onClick(DialogInterface arg0, int arg1) { 
    // TODO Auto-generated method stub 

} 
} 

回答

2

activity = this這並不是指活動情境堂妹NetworksActivity extends Fragment

使用

activity = getActivity()

getActivity()

返回該片段當前與相關聯的活動。

而且你的進口不正確

刪除此

import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 

使用

import android.view.View 
import android.view.View.OnClickListener; 

還可以使用@覆蓋批註

@Override 
public void onClick(View v) { 

而且刪除此

@Override 
public void onClick(DialogInterface arg0, int arg1) { 
    // TODO Auto-generated method stub 

} 

同時刪除該

implements OnClickListener 
+0

這個工作!謝謝! – Kodi

0

試試這個

activity=getActivity(); 
0

下面是與Raug註釋工作代碼,雖然我沒有使用既不

刪除此 進口android.content.DialogInterface; import android.content.DialogInterface.OnClickListener;使用 import android.view.View import android.view.View.OnClickListener;

import android.app.Fragment; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageButton; 


public class MiscFragment extends Fragment { 
ImageButton imgButtonTwitter; 
ImageButton imgButtonFacebook; 

public void onCreate (Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
} 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.fragment_misc, container, 
      false); 
    imgButtonTwitter = (ImageButton)rootView.findViewById(R.id.iButton1); 
    imgButtonFacebook = (ImageButton)rootView.findViewById(R.id.iButton2); 
    imgButtonFacebook.setOnClickListener(listener); 
    imgButtonTwitter.setOnClickListener(listener); 
    return rootView; 
} 
private View.OnClickListener listener = new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     switch (v.getId()){ 
      case (R.id.iButton1): 
       startActivity(new Intent(Intent.ACTION_VIEW, 
         Uri.parse("http://instagram.com/"))); 

       break ; 

      case (R.id.iButton2): 
       startActivity(new Intent(Intent.ACTION_VIEW, 
         Uri.parse("https://www.facebook.com/"))); 

       break ; 

     } 


    } 
}; 

}