gpt4 book ai didi

java - 在菜单应用程序打开应用程序之前运行应用程序页面时出错

转载 作者:行者123 更新时间:2023-12-01 19:38:16 26 4
gpt4 key购买 nike

我的应用需要一些帮助

所以我制作了一个侧边栏菜单,现在我正在使用 php 和 mysql (xampp) 制作一个登录和注册页面,我希望登录页面成为第一页,然后它们进入“应用程序内部”到菜单和主页。

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

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

所以我希望登录页面应该首先出现,然后是带有 fragment 的侧栏。

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)

最佳答案

您在 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" />

关于java - 在菜单应用程序打开应用程序之前运行应用程序页面时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56600618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com