2016-12-15 17 views
1

私はこのコードで私のフラグメントにボタンとしてImageViewのを作りたい:それは働いていませんのOnClick ImageViewの - アンドロイド

imageView4.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      // Create new fragment and transaction 
      Fragment newFragment = new TermsOfServiceFragemnt(); 
      // consider using Java coding conventions (upper first char class names!!!) 
      FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

      // Replace whatever is in the fragment_container view with this fragment, 
      // and add the transaction to the back stack 
      transaction.replace(R.id.content_frame_container, newFragment); 
      transaction.addToBackStack(null); 

      // Commit the transaction 
      transaction.commit(); 
     } 

    }); 

。何も起こらない。ここに完全な断片コードがあります:

public OneFragment() { 
     // Required empty public constructor 
    } 

    private static TextView creditWallet; 

    private String[] myString; 
    private static final Random rgenerator = new Random(); 
    private TextView firstName, textView13,textView14,textView15,textView16,textView17,textView18; 
    private Animation animShake, wbijam, load1, load2, load3, load4, load5, load6, wbijam1, wbijam2, wbijam3, wbijam4, wbijam5; 
    private ImageView img, imageView9,imageView4,imageView5,imageView6,imageView7,imageView8; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    public static void onUpdateView(Context aiContext) { 
     // TODO Auto-generated method stub 
     if (aiContext != null && creditWallet != null) 
      creditWallet.setText(PreferenceConnector.readInteger(aiContext, PreferenceConnector.WALLETPOINTS, 0) + ""); 

    } 

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

     super.onViewCreated(view, savedInstanceState); 
     img = (ImageView)view.findViewById(R.id.imgRandom); 
     imageView9 = (ImageView)view.findViewById(R.id.imageView3); 
     imageView4 = (ImageView)view.findViewById(R.id.imageView4); 
     imageView5 = (ImageView)view.findViewById(R.id.imageView5); 
     imageView6 = (ImageView)view.findViewById(R.id.imageView6); 
     imageView7 = (ImageView)view.findViewById(R.id.imageView7); 
     imageView8 = (ImageView)view.findViewById(R.id.imageView8); 

     firstName = (TextView)view.findViewById(R.id.imie); 


     textView13 = (TextView)view.findViewById(R.id.textView13); 
     textView14 = (TextView)view.findViewById(R.id.textView15); 
     textView15 = (TextView)view.findViewById(R.id.textView16); 
     textView16 = (TextView)view.findViewById(R.id.textView17); 
     textView17 = (TextView)view.findViewById(R.id.textView18); 
     textView18 = (TextView)view.findViewById(R.id.textView19); 

     final Animation wbijam = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam); 
     textView13.startAnimation(wbijam); 
     final Animation wbijam1 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam); 
     textView14.startAnimation(wbijam1); 
     final Animation wbijam2 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam); 
     textView15.startAnimation(wbijam2); 
     final Animation wbijam3 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam); 
     textView16.startAnimation(wbijam3); 
     final Animation wbijam4 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam); 
     textView17.startAnimation(wbijam4); 
     final Animation wbijam5 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam); 
     textView18.startAnimation(wbijam5); 






     final Animation animShake = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.shak); 
     img.startAnimation(animShake); 


     final Animation load1 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load1); 
     imageView9.startAnimation(load1); 
     final Animation load2 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load2); 
     imageView4.startAnimation(load2); 
     final Animation load3 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load3); 
     imageView5.startAnimation(load3); 
     final Animation load4 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load4); 
     imageView6.startAnimation(load4); 
     final Animation load5 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load5); 
     imageView7.startAnimation(load5); 
     final Animation load6 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load6); 
     imageView8.startAnimation(load6); 


     firstName.setText("Hey,"+" " +PreferenceConnector.readString(getActivity().getApplicationContext(), PreferenceConnector.FIRST_NAME, "")); 

     // Inflate the layout for this fragment 

     final ImageView img = (ImageView) view.findViewById(R.id.imgRandom); 
// return view; 
// ^^^^ error remove it 
     Resources res = getResources(); 

     myString = res.getStringArray(R.array.myArray); 

     String q = myString[rgenerator.nextInt(myString.length)]; 

     TextView tv = (TextView) view.findViewById(R.id.tv); 
     tv.setText(q); 

     imageView4.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       // Create new fragment and transaction 
       Fragment newFragment = new TermsOfServiceFragemnt(); 
       // consider using Java coding conventions (upper first char class names!!!) 
       FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

       // Replace whatever is in the fragment_container view with this fragment, 
       // and add the transaction to the back stack 
       transaction.replace(R.id.content_frame_container, newFragment); 
       transaction.addToBackStack(null); 

       // Commit the transaction 
       transaction.commit(); 
      } 

     }); 
      // I have 3 images named img_0 to img_2, so... 
     final String str = "img_" + rnd.nextInt(9); 
     img.setImageDrawable 
       (
         getResources().getDrawable(getResourceID(str, "drawable", 
           getActivity().getApplicationContext()) 
       )); 
     return view; 
     // ^^^ move it here 
    } 

     // I have 3 images named img_0 to img_2, so... 



    protected final static int getResourceID 
      (final String resName, final String resType, final Context ctx) { 
     final int ResourceID = 
       ctx.getResources().getIdentifier(resName, resType, 
         ctx.getApplicationInfo().packageName); 
     if (ResourceID == 0) { 
      throw new IllegalArgumentException 
        (
          "No resource string found with name " + resName 
        ); 
     } else { 
      return ResourceID; 
     } 

    } 

} 

私を助けてください!私はこのカップルの時間と戦っている。ありがとうスタックコミューニティ!

EDIT

さて、私は

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

    super.onViewCreated(view, savedInstanceState); 


    imageView19 = (ImageView)view.findViewById(R.id.imageView3); 

    imageView9.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      // Create new fragment and transaction 
      Fragment newFragment = new TermsOfServiceFragemnt(); 
      // consider using Java coding conventions (upper first char class names!!!) 
      FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

      // Replace whatever is in the fragment_container view with this fragment, 
      // and add the transaction to the back stack 
      transaction.replace(R.id.content_frame_container, newFragment); 
      transaction.addToBackStack(null); 

      // Commit the transaction 
      transaction.commit(); 
     } 

    }); 

持っていると何も変わっていません。など、エラーが作業していないではない

+0

レイアウトXMLを投稿することはできますか? –

+0

デバッガを使用して、リスナー内のコードが実装されているかどうかを確認できますか? –

+0

try catchブロックをリスナーに配置して、エラーの原因を確認してください。例外がスローされますか?それは何ですか? –

答えて

0

解決方法1:下記に示すonActivityCreated()にコードを追加すると

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onActivityCreated(savedInstanceState); 
    // get the button view 
    fashionImg = (ImageView) getView().findViewById(R.id.selectone); 
    // set a onclick listener for when the button gets clicked 
    fashionImg.setOnClickListener(new OnClickListener() { 
     // Start new list activity 
     public void onClick(View v) { 
      // Toast Message 
      // New Activity.. 
     } 
    }); 
} 

解決方法2:画像を表示クリックリスナーのためのサンプルコードの下 使用、

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.fragment_one, container, false); 

    // Here it is 
    ImageButton camBt = (ImageButton)v.findViewById(R.id.button1); 
    camBt.setOnClickListener(listener); 


    return v; 
} 
0

"onViewCreatead"メソッドの中にonClickListenerを置くことができます。

@Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     imageView4 = (ImageView)view.findViewById(R.id.imageView4); 

     imageView4.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       // Create new fragment and transaction 
       Fragment newFragment = new TermsOfServiceFragemnt(); 
       // consider using Java coding conventions (upper first char class names!!!) 
       FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

       // Replace whatever is in the fragment_container view with this fragment, 
       // and add the transaction to the back stack 
       transaction.replace(R.id.content_frame_container, newFragment); 
       transaction.addToBackStack(null); 

       // Commit the transaction 
       transaction.commit(); 
      } 

     }); 

     } 
+0

が機能しません。何も変わらない –

+0

行を削除するとどうなりますか: "super.onViewCreated(view、savedInstanceState);" –

+0

nullオブジェクトリファレンスで仮想メソッド 'void android.view.View.setOnClickListener(android.view.View $ OnClickListener)'を呼び出そうとしています –

関連する問題