java – Button在Android应用程序中不起作用

前端之家收集整理的这篇文章主要介绍了java – Button在Android应用程序中不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我是Android的新手,并试图实现一个简单的按钮,它应该在Toast中显示一条消息(最终它将用于创建一个帐户).但是当我按下按钮没有任何反应时,logcat上没有显示任何错误,所以我必须错过一些明显的东西,但我找不到它!

AndroidManifest.xml中

registration.xml

RegistrationActivity.java

 package com.example.laptop.whatsfordinner;


    import android.app.Activity;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.http.NameValuePair;
    import org.apache.http.message.BasicNameValuePair;
    import org.json.JSONException;
    import org.json.JSONObject;

    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;



    public class RegistrationActivity extends Activity {


    // Progress Dialog
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();
    EditText inputName;
    EditText inputUsername;
    EditText inputEmail;
    EditText inputPassword;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration);

        // Create button
       Button btnRegister1 = (Button) findViewById(R.id.btnRegister);

        // button click event
        btnRegister1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // creating new account in background thread
                Toast.makeText(RegistrationActivity.this,"Example action.",Toast.LENGTH_SHORT).show();

            }
        });
    }

Registration.java

package com.example.laptop.whatsfordinner;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;

public class Registration extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static Registration newInstance(int sectionNumber) {
        Registration fragment = new Registration();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER,sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public Registration() {
    }

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

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainPage) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

我究竟做错了什么?

最佳答案
您正在片段和活动中展开布局.

如果要在Fragment中处理该Button,请执行以下操作:

@Override
    public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_main,false);

        Button regButton1 = (Button) rootView.findViewById(R.id.btnRegister);

        regButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(),"Example action",Toast.LENGTH_SHORT).show();
            }
        });

        return rootView;
    }

您的Activity中的代码看起来不错,但是您为两者充气了相同的布局.

看到:

setContentView(R.layout.registration);

和:

View rootView = inflater.inflate(R.layout.registration,false);

此外,按CTRL Alt O删除未使用的导入,最后,它应该是这样(你的片段的导入):

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

就像我猜测的那样,问题是因为你在一个片段和一个Activity中膨胀了一个布局.我已经尝试过它并且它与该Activity一起工作.

(顺便说一下好问题).

解:
设置自己的片段布局和活动,但两者的布局不同.

然后,你准备好了.

原文链接:https://www.f2er.com/android/429934.html

猜你在找的Android相关文章