在Onclick属性上找不到方法

时间:2015-07-28 00:19:44

标签: java android methods onclick

我正在尝试调用postannonce方法,但它没有显示在Onclick方法列表中。这是我的SecondeFrament.java代码:

  import android.app.Fragment;
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.os.Bundle;
  import android.os.Environment;
  import android.support.annotation.Nullable;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.webkit.WebSettings;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ImageView;
  import android.widget.ProgressBar;
  import android.widget.TextView;
  import android.widget.Toast;

  import java.io.File;


  public class SecondeFragment extends Fragment {
      public static final String TAG = "seconde";


      ProgressBar prog;
      Button post;
      EditText titre;
      EditText text;





      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle   savedInstanceState) {


          View view =  inflater.inflate(R.layout.secondefragment, container, false);

          post = (Button) view.findViewById(R.id.ressayer);
          titre = (EditText) view.findViewById(R.id.titreannonce);
          text = (EditText) view.findViewById(R.id.textannonce);
          prog = (ProgressBar) view.findViewById(R.id.progressBar2);


           return view;


      }


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

}

public void postannonce(View view){

    prog.setVisibility(View.VISIBLE);


            String titreannonce = titre.getText().toString();
            String textannonce= text.getText().toString();

            if((!titreannonce.trim().equals(""))&&(!textannonce.trim().equals(""))){
                new PostActivity(getActivity(),prog).execute(titreannonce, textannonce);
            }
            else{
                Toast.makeText(getActivity(), "Veuillez remplir tout les champs svp", Toast.LENGTH_SHORT).show();

                prog.setVisibility(View.INVISIBLE);

            }

        }

}

secondeFragment.xml

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
      android:id="@+id/lay1">

   <ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar2"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:indeterminate="false"
    android:visibility="invisible" />

      <EditText
          android:layout_width="260dp"
          android:layout_height="wrap_content"
          android:ems="10"
          android:id="@+id/titreannonce"
          android:textColor="#000000"
          android:maxLength="30"
          android:background="@drawable/edittxt"
          android:layout_marginBottom="70dp"
          android:textColorHint="#ffb5b5b5"
          android:hint="Donnez un titre a votre annonce .."
          android:layout_above="@+id/progressBar2"
          android:layout_centerHorizontal="true" />

  <EditText
    android:layout_width="260dp"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine"
    android:ems="10"
    android:id="@+id/textannonce"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:paddingTop="15dp"
    android:paddingBottom="15dp"
    android:textColor="#000000"
    android:background="@drawable/textarea"
    android:gravity="left|top"

    android:maxLength="150"
    android:hint="Votre annonce ici .."
    android:textColorHint="#ffb5b5b5"
    android:layout_alignBottom="@+id/post"
    android:layout_alignLeft="@+id/titreannonce"
    android:layout_alignStart="@+id/titreannonce"
    android:layout_below="@+id/titreannonce"
    android:layout_marginBottom="70dp"/>

  <ImageView
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:id="@+id/post"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_margin="16dp"
    android:background="@drawable/floating_button"
    android:src="@mipmap/ic_check"
    android:padding="16dp"
    android:onClick="postannonce"
    android:scaleType="center" />

  </RelativeLayout>

我还是Android新手,如果有人能告诉我代码中缺少什么,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

您的postannonce方法需要在SecondeFragment课程内。改变这一部分:

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

}

public void postannonce(View view){

    prog.setVisibility(View.VISIBLE);


            String titreannonce = titre.getText().toString();
            String textannonce= text.getText().toString();

            if((!titreannonce.trim().equals(""))&&(!textannonce.trim().equals(""))){
                new PostActivity(getActivity(),prog).execute(titreannonce, textannonce);
            }
            else{
                Toast.makeText(getActivity(), "Veuillez remplir tout les champs svp", Toast.LENGTH_SHORT).show();

                prog.setVisibility(View.INVISIBLE);

            }

        }

}

看起来像这样:

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

    public void postannonce(View view){

        prog.setVisibility(View.VISIBLE);


                String titreannonce = titre.getText().toString();
                String textannonce= text.getText().toString();

                if((!titreannonce.trim().equals(""))&&(!textannonce.trim().equals(""))){
                    new PostActivity(getActivity(),prog).execute(titreannonce, textannonce);
                }
                else{
                    Toast.makeText(getActivity(), "Veuillez remplir tout les champs svp", Toast.LENGTH_SHORT).show();

                    prog.setVisibility(View.INVISIBLE);

                }

            }

    }
}