在菜单应用打开应用之前,错误运行应用页面

时间:2019-06-14 14:53:33

标签: java android

我的应用需要帮助

因此,我已经创建了侧边栏菜单,现在我正在使用php和mysql(xampp)进行登录和注册页面,我希望登录页面成为第一页,并且它们进入“应用程序内部”进入菜单和主页。

所以我正在看一个教程,他从一个新项目开始,所以我创建了一个新的布局调用activity_register.xml,我做了一些代码,他也将代码放在了MainActivity.java上,但是我已经在MainActivity中有很多代码了.java,我不确定代码是否正确

应该创建一个新的java类吗?

因此,我希望登录页面首先出现,并在其侧栏上显示fragmens。

activity_register.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingTop="80dp">

    <com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/name"
        android:hint="Name"
        android:inputType="textPersonName"
        android:textColor="@color/colorText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/email"
            android:hint="Email"
            android:inputType="textEmailAddress"
            android:textColor="@color/colorText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       app:passwordToggleEnabled="true">
        <EditText
            android:id="@+id/password"
            android:hint="Password"
            android:inputType="textPassword"
            android:textColor="@color/colorText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleEnabled="true">
        <EditText
            android:id="@+id/c_password"
            android:hint="Confirm Password"
            android:inputType="textPassword"
            android:textColor="@color/colorText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </com.google.android.material.textfield.TextInputLayout>

    <ProgressBar
        android:id="@+id/loading"
        android:layout_marginTop="30dp"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <Buttonbt
        android:id="@+id/btn_regist"
        android:backgroundTint="@color/colorPrimary"
        android:textColor="@android:color/white"
        android:text="Register"
        android:layout_marginTop="30dp"
        android:layout_width="match_parent"
        android:layout_height="55dp" />

</LinearLayout>

MainActivity.java

package com.example.readytogo;


import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;


import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    private DrawerLayout drawer;

    //register

    private EditText name, email, password, c_password;
    private Button btn_regist;
    private ProgressBar loading;
    private static String URL_REGIST = "http://localhost:8012/android_register_login/register.php";
    //register private end


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView =findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        if(savedInstanceState == null){
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new ProfileFragment()).commit();
        navigationView.setCheckedItem(R.id.nav_profile);


        //register
        loading = findViewById(R.id.loading);
        name = findViewById(R.id.name);
        email = findViewById(R.id.email);
        password = findViewById(R.id.password);
        c_password = findViewById(R.id.c_password);
        btn_regist = findViewById(R.id.btn_regist);

        btn_regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Regist();
            }
        });
            //register end

        }
    }
    //register
        private void Regist(){
            loading.setVisibility(View.VISIBLE);
            btn_regist.setVisibility(View.GONE);

            final String name = this.name.getText().toString().trim();
            final String email = this.email.getText().toString().trim();
            final String password = this.password.getText().toString().trim();

            StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_REGIST,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                        try{
                            JSONObject jsonObject = new JSONObject(response);
                            String success = jsonObject.getString("success");

                            if (success.equals(1)) {
                                Toast.makeText(MainActivity.this, "Register Success!", Toast.LENGTH_SHORT).show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(MainActivity.this, "Register Error! " + e.toString(), Toast.LENGTH_SHORT).show();
                            loading.setVisibility(View.GONE);
                            btn_regist.setVisibility(View.VISIBLE);
                        }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(MainActivity.this, "Register Error! " + error.toString(), Toast.LENGTH_SHORT).show();
                            loading.setVisibility(View.GONE);
                            btn_regist.setVisibility(View.VISIBLE);
                        }
                    })

            {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<>();
                    params.put("name", name);
                    params.put("email", email);
                    params.put("password", password);
                    return params;
                }
            };

            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
        }


        //mavigate from fragments
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.nav_profile:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ProfileFragment()).commit();
                break ;
            case R.id.nav_events:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new EventsFragment()).commit();
                break;
            case R.id.nav_aboutus:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new AboutUsFragment()).commit();
                break;
            case R.id.nav_faq:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new FAQFragment()).commit();
                break;
            case R.id.nav_settings:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new SettingsFragment()).commit();
                break;
            case R.id.nav_share:
                Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_logout:
                Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show();
                break;
            }
            drawer.closeDrawer(GravityCompat.START);
        return true;
        }
            //emd of navigate from templates


    @Override
    public void onBackPressed(){
        if (drawer.isDrawerOpen(GravityCompat.START)){
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}

运行时的错误原因

原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)”         在com.example.readytogo.MainActivity.onCreate(MainActivity.java:81)

2 个答案:

答案 0 :(得分:2)

您在xml文件中输入了错误的按钮名称小部件

替换此:

    <Buttonbt
      android:id="@+id/btn_regist"
      android:backgroundTint="@color/colorPrimary"
      android:textColor="@android:color/white"
      android:text="Register"
      android:layout_marginTop="30dp"
      android:layout_width="match_parent"
      android:layout_height="55dp" />

与此

    <Button
      android:id="@+id/btn_regist"
      android:backgroundTint="@color/colorPrimary"
      android:textColor="@android:color/white"
      android:text="Register"
      android:layout_marginTop="30dp"
      android:layout_width="match_parent"
      android:layout_height="55dp" />

答案 1 :(得分:1)

您的btn_regist.setOnClickListener似乎未正确初始化! 您可以尝试以下操作:btn_regist = (Button) findViewById(R.id.btn_regist);

,您应该始终在单独的 Java类中创建一些服务。就您而言,注册可能是一项服务,它将为您的PHP创建发布请求。 URL_REGIST 变量应该在该服务java类中。