android:onClick不起作用

时间:2017-02-04 05:38:53

标签: android onclick

我在这样的活动中使用导航抽屉。

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   //setContentView(R.layout.activity_registration);

    FrameLayout contentFrameLayout=(FrameLayout)findViewById(R.id.contentView);
    getLayoutInflater().inflate(R.layout.activity_registration,contentFrameLayout);
    register=(Button)findViewById(R.id.register);
    rname=(EditText)findViewById(R.id.name);
    rmobile=(EditText)findViewById(R.id.mobile);
    remail=(EditText)findViewById(R.id.email);
    rpassword=(EditText)findViewById(R.id.password);
}

    public void register(View v)
    {
      new DoRegistration().execute();
    }
 public class DoRegistration extends AsyncTask<Void,Void,Void>
{
    JSONObject registrationResult;

    @Override
    protected Void doInBackground(Void... voids)
    {
        List<Pair> param=new ArrayList<>();

        param.add(new Pair("R_name",rname.getText().toString()));
        param.add(new Pair("R_mobile",rmobile.getText().toString()));
        param.add(new Pair("R_email",remail.getText().toString()));
        param.add(new Pair("R_password",rpassword.getText().toString()));

        registrationResult=serviceHelper.Registration(param);

        return null;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();

        register.setEnabled(false);
    }

    @Override
    protected void onPostExecute(Void aVoid)
    {

        super.onPostExecute(aVoid);

        register.setEnabled(true);

        if(registrationResult!=null) {

            try {
                if (registrationResult.getString("status").equals("done")) {

                    Intent LoginIntent = new Intent(Registration.this, Login.class);
                    LoginIntent.putExtra("Msg", "Registered Successfully.. Login Now");
                    startActivity(LoginIntent);

                } else {
                }

            } catch (Exception e) {
            }

        }
    }

}

所以onclick不起作用。如何访问setContentView。 发生注册详细信息错误时无法找到onclick方法。

activity_registration

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="top|center">

           <Button
               android:id="@+id/register"
               android:layout_width="190dp"
               android:layout_height="50dp"
               android:text="@string/reg"
               android:textSize="20sp"
               style="@style/button"
               android:background="@drawable/button"
               android:onClick="register"
               />
        </LinearLayout>

为什么会出现此错误。

1 个答案:

答案 0 :(得分:1)

尝试向注册按钮添加侦听器,并从布局中删除onclick触发器。

 View rootView = inflater.inflate(R.layout.activity_registration,contentFrameLayout);

 register = (Button) rootView.findViewById(R.id.register);


 register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            new DoRegistration().execute();
             }
        });
相关问题