从一个活动导航到另一活动时出错

时间:2019-04-02 17:22:39

标签: java android android-layout android-fragments

/ *我已经尝试了很多次,但是当我单击导航抽屉中的项目时,我无法导航到另一个活动,而当我单击该项目时,应用程序崩溃(错误:再次打开应用程序)。* / < / p>

drawer.xml 

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/admin"
        android:title="Admin Profile"
        android:icon="@drawable/ic_dashboard"/>
    <item
        android:id="@+id/createaccount"
        android:title="User Registration"
        android:icon="@drawable/ic_event_black_24dp"/>
    <item
        android:id="@+id/manageuser"
        android:title="Manage User"
        android:icon="@drawable/ic_settings_black_24dp"/>
    <item
        android:id="@+id/us"
        android:title="About us"
        android:icon="@drawable/ic_call_to_action_black_24dp"/>
    <item
        android:id="@+id/logout"
        android:title="Logout"
        android:icon="@drawable/ic_cancel_black_24dp"/>



</menu>

/ activity_navigation_bar.xml /

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingBottom="16dp"
    android:paddingTop="16dp"
    tools:context="com.ereports.navigationBar">
    <android.support.v7.widget.LinearLayoutCompat
        android:background="@drawable/bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.LinearLayoutCompat>


    <android.support.design.widget.NavigationView
        app:headerLayout="@layout/header"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/white"
        app:itemTextColor="@color/darkgray"
        app:itemIconTint="@color/darkgray"
        app:menu="@menu/drawermenu"
        android:layout_gravity="start">

    </android.support.design.widget.NavigationView>


</android.support.v4.widget.DrawerLayout>

/ navigationBar.java /

    package com.ereports;

import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

import static com.ereports.R.id.us;

public class navigationBar extends AppCompatActivity {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_bar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.Open, R.string.Close);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }
 public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement

        if (id == R.id.us) {

            Intent intent = new Intent(this,Registration.class);
            this.startActivity(intent);
            return true;
        }

        if (id == R.id.action_search) {
            Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show();
            return true;
        }

        if (id == R.id.action_settings) {
            Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
}

/ *用于导航的另一项活动:

'Registration.java * /

 package com.ereports;

import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Registration extends Fragment implements OnClickListener {
    private static View view;
    private static EditText fullName, emailId, mobileNumber, location,
            password, confirmPassword;
    private static TextView login;
    private static Button signUpButton1;
    private static CheckBox terms_conditions;

    public Registration() {

    }

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

    // Initialize all views
    private void initViews() {
        fullName = (EditText) view.findViewById(R.id.fullName);
        emailId = (EditText) view.findViewById(R.id.userEmailId);
        mobileNumber = (EditText) view.findViewById(R.id.mobileNumber);
        location = (EditText) view.findViewById(R.id.location);
        password = (EditText) view.findViewById(R.id.password);
        confirmPassword = (EditText) view.findViewById(R.id.confirmPassword);
        signUpButton1 = (Button) view.findViewById(R.id.signUpBtn);
        login = (TextView) view.findViewById(R.id.already_user);
        terms_conditions = (CheckBox) view.findViewById(R.id.terms_conditions);

        // Setting text selector over textviews
        @SuppressLint("ResourceType") XmlResourceParser xrp = getResources().getXml(R.drawable.text_selector);
        try {
            ColorStateList csl = ColorStateList.createFromXml(getResources(),
                    xrp);

            login.setTextColor(csl);
            terms_conditions.setTextColor(csl);
        } catch (Exception e) {
        }
    }

    // Set Listeners
    private void setListeners() {
        signUpButton1.setOnClickListener(this);
        login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.signUpBtn1:

                // Call checkValidation method
                checkValidation();
                break;

        }

    }

    // Check Validation Method
    private void checkValidation() {

        // Get all edittext texts
        String getFullName = fullName.getText().toString();
        String getEmailId = emailId.getText().toString();
        String getMobileNumber = mobileNumber.getText().toString();
        String getLocation = location.getText().toString();
        String getPassword = password.getText().toString();
        String getConfirmPassword = confirmPassword.getText().toString();

        // Pattern match for email id
        Pattern p = Pattern.compile(Utils.regEx);
        Matcher m = p.matcher(getEmailId);

        // Check if all strings are null or not
        if (getFullName.equals("") || getFullName.length() == 0
                || getEmailId.equals("") || getEmailId.length() == 0
                || getMobileNumber.equals("") || getMobileNumber.length() == 0
                || getLocation.equals("") || getLocation.length() == 0
                || getPassword.equals("") || getPassword.length() == 0
                || getConfirmPassword.equals("")
                || getConfirmPassword.length() == 0)

            new CustomToast().Show_Toast(getActivity(), view,
                    "All fields are required.");

            // Check if email id valid or not
        else if (!m.find())
            new CustomToast().Show_Toast(getActivity(), view,
                    "Your Email Id is Invalid.");

            // Check if both password should be equal
        else if (!getConfirmPassword.equals(getPassword))
            new CustomToast().Show_Toast(getActivity(), view,
                    "Both password doesn't match.");

            // Make sure user should check Terms and Conditions checkbox
        else if (!terms_conditions.isChecked())
            new CustomToast().Show_Toast(getActivity(), view,
                    "Please select Terms and Conditions.");

            // Else do signup or do your stuff
        else
            Toast.makeText(getActivity(), "User Registered", Toast.LENGTH_SHORT)
                    .show();

    }
}

activity_registration.java

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="10dp"
        android:text="@string/signUp"
        android:textColor="@color/white_greyish"
        android:textSize="25sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/fullName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent"
        android:drawableLeft="@drawable/user"
        android:drawablePadding="8dp"
        android:gravity="center_vertical"
        android:hint="@string/fullName"
        android:inputType="textCapWords"
        android:padding="10dp"
        android:singleLine="true"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:textSize="16sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="@color/white_greyish" />

    <EditText
        android:id="@+id/userEmailId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@android:color/transparent"
        android:drawableLeft="@drawable/email"
        android:drawablePadding="8dp"
        android:gravity="center_vertical"
        android:hint="@string/email"
        android:inputType="textEmailAddress"
        android:padding="10dp"
        android:singleLine="true"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:textSize="16sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="@color/white_greyish" />

    <EditText
        android:id="@+id/mobileNumber"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@android:color/transparent"
        android:drawableLeft="@drawable/phone"
        android:drawablePadding="8dp"
        android:gravity="center_vertical"
        android:hint="@string/mobileNumber"
        android:inputType="phone"
        android:padding="10dp"
        android:singleLine="true"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:textSize="16sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="@color/white_greyish" />

    <EditText
        android:id="@+id/location"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@android:color/transparent"
        android:drawableLeft="@drawable/location"
        android:drawablePadding="8dp"
        android:gravity="center_vertical"
        android:hint="@string/location"
        android:inputType="textCapWords"
        android:padding="10dp"
        android:singleLine="true"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:textSize="16sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="@color/white_greyish" />

    <EditText
        android:id="@+id/password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@android:color/transparent"
        android:drawableLeft="@drawable/password"
        android:drawablePadding="8dp"
        android:gravity="center_vertical"
        android:hint="@string/passowrd"
        android:inputType="textPassword"
        android:padding="10dp"
        android:singleLine="true"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:textSize="16sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="@color/white_greyish" />

    <EditText
        android:id="@+id/confirmPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@android:color/transparent"
        android:drawableLeft="@drawable/confirm_password"
        android:drawablePadding="8dp"
        android:gravity="center_vertical"
        android:hint="@string/confirmPassword"
        android:inputType="textPassword"
        android:padding="10dp"
        android:singleLine="true"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:textSize="16sp" />

    <CheckBox
        android:id="@+id/terms_conditions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="7dp"
        android:text="@string/terms_conditions"
        android:textColor="@color/white"
        android:textSize="14sp" />

    <Button
        android:id="@+id/signUpBtn1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:background="@drawable/loginbutton_selector"
        android:padding="3dp"
        android:text="Register"
        android:textColor="@color/background_color"
        android:textSize="17sp"
        android:textStyle="bold" />

</LinearLayout>

/ *应用程序崩溃

预期结果:应导航到Registration.java 实际结果:应用程序崩溃* /

1 个答案:

答案 0 :(得分:0)

使用片段在活动之间进行切换 [https://www.youtube.com/watch?v=J8GB_b8qyK8]

这涵盖了很好的解释。我自己尝试过