2017-08-19 6 views
0

私のアプリでは、いくつかのボタンを備えたいくつかのフラグメントを使って、さまざまなコンテンツをナビゲートするのに役立ちます。 私が作ったレイアウトは完璧ですが、今はボタンのリンクを作りたいと思っています。どこに私のフラグメントにonClickListenerを置くことができますか?

MY BUTTONのIDは次のとおりです。Lista_Smartphone:私は名前の新しいアクティビティを開くために私のコードスニペットでonClickListenerを追加することができますbuttonSP

FragmentWithOneImage.java onCreateView()方法で

public class FragmentWithOneImage extends Fragment { 
private String title; 
private int image; 


public static FragmentWithOneImage newInstance(String title, int resImage) { 
    FragmentWithOneImage fragment = new FragmentWithOneImage(); 
    Bundle args = new Bundle(); 
    args.putInt("image", resImage); 
    args.putString("title", title); 
    fragment.setArguments(args); 
    return fragment; 
} 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    image = getArguments().getInt("image", 0); 
    title = getArguments().getString("title"); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_one_img, container, false); 
    TextView tvLabel = (TextView) view.findViewById(R.id.txtMain); 
    tvLabel.setText(title); 

    ImageView imageView = (ImageView) view.findViewById(R.id.imgMain); 
    imageView.setImageResource(image); 
    return view; 
} 
} 

答えて

0

はonCreateView()メソッドの上に置いたコードの下に追加します。アクティビティクラスのonCreate()と等価です

0

Context con = getActivity(); 
Button myButton = (Button) view.findViewById(R.id.buttonSP); 

myButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       //Move to Lista_Smartphone from Current fragment 
       startActivity(new Intent(con, Lista_Smartphone.class)); 
       finish(); 
      } 
     }); 
+0

変数「詐欺」は内部クラス内からアクセスされ、 – localsixosix

+0

は「詐欺」というエラーがあったを解決し、最終的に宣言する必要がありますR.id.buttonSP); – localsixosix

0

onActivityCreated()メソッドを使用してください。時にはonCreateView()のビューに時間がかかり、そのような場合にはnullポインタ例外が発生することがあります。この行のunreacheable声明::ボタンbuttonSP =(ボタン)view.findViewById(

public class FragmentWithOneImage extends Fragment { 


    TextView tvLabel; 
    ImageView imageView; 
    Button button; 

    public static FragmentWithOneImage newInstance(String title, int resImage) { 
     FragmentWithOneImage fragment = new FragmentWithOneImage(); 
     Bundle args = new Bundle(); 
     args.putInt("image", resImage); 
     args.putString("title", title); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_one_img, container, false); 
     return view; 
    } 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     tvLabel = (TextView) view.findViewById(R.id.txtMain); 
     imageView = (ImageView) view.findViewById(R.id.imgMain); 
     button=(Button)view.findViewById(R.id.button); 

    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     tvLabel.setText(title); 
     imageView.setImageResource(image); 

     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Intent intent=new Intent(getActivity(),Lista_Smartphone.class); 
       startActivity(intent); 
      } 
     }); 


    } 
} 
関連する問題