简体   繁体   English

为什么我的应用程序不从 Fragment 打开新的 Activity?

[英]Why doesn't my application open new Activity from fragment?

Can someone please tell me what I am doing wrong.有人可以告诉我我做错了什么。 I have looked everywhere and tried changing my code many ways and I still can't figure it out.我到处寻找并尝试以多种方式更改我的代码,但我仍然无法弄清楚。 So I have a "LoginActivity.java" file that has a activity_login.xml file.所以我有一个“LoginActivity.java”文件,它有一个activity_login.xml 文件。 Inside this activity, I have 2 fragments, login_fragment and register_fragment.在这个活动中,我有 2 个片段,login_fragment 和 register_fragment。 They each have their own java files.他们每个人都有自己的java文件。 Everything works fine but on my LoginFragment.java file, when the user is verified successfully, it is supposed to open MainActivity, but instead of opening the MainActivity, it is just opening a new instance of my LoginActivity, even though I am specifically telling telling it to open MainActivity.一切正常,但在我的 LoginFragment.java 文件上,当用户验证成功时,它应该打开 MainActivity,但不是打开 MainActivity,它只是打开我的 LoginActivity 的一个新实例,即使我特别告诉它打开MainActivity。 Here is the code in my LoginFragment.java file.这是我的 LoginFragment.java 文件中的代码。

package com.example.myapplication;

import android.content.Intent;
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.EditText;
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 org.json.JSONException;
import org.json.JSONObject;

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

public class LoginFragment extends Fragment {

    private EditText username, password;
    private RequestQueue requestQueue;
    private static final String URL = "http://example.com/login.php";
    private StringRequest request;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.login_fragment, container, false);

        username = (EditText) view.findViewById(R.id.lUsername);
        password = (EditText) view.findViewById(R.id.lPassword);

        Button signin = (Button) view.findViewById(R.id.login);

        requestQueue = Volley.newRequestQueue(getActivity());

        signin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if (jsonObject.names().get(0).equals("success")) {
                                Toast.makeText(getActivity(), jsonObject.getString("success"), Toast.LENGTH_SHORT).show();
                                getActivity().startActivity(new Intent(getActivity(), MainActivity.class));
                            } else {
                                Toast.makeText(getActivity(), jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        HashMap<String, String> hashMap = new HashMap<>();
                        hashMap.put("username", username.getText().toString());
                        hashMap.put("password", password.getText().toString());
                        return hashMap;
                    }
                };
                requestQueue.add(request);
            }
        });

        return view;
    }

}

I found the problem and will answer just in case anyone has an issue like mine in the future.我发现了问题,并会回答,以防将来有人遇到像我这样的问题。 So when I start my project, it loads the MainActivity.所以当我开始我的项目时,它会加载 MainActivity。 My MainActivity checks the Shared Preferences on create to check a boolean that states if a user is logged in or not.我的 MainActivity 在创建时检查共享首选项以检查一个布尔值,该布尔值表明用户是否已登录。 If no, it opens the LoginActivity, if yes, it continues to launch MainActivity.如果不是,则打开 LoginActivity,如果是,则继续启动 MainActivity。 On the app's first launch, it automatically sets the boolean to false.在应用程序首次启动时,它会自动将布尔值设置为 false。 When I log in, the boolean in the Shared Preferences is supposed to be set to true.当我登录时,共享首选项中的布尔值应该设置为 true。 Since I forgot to do that, when I try to launch the MainAcitvity, MainActivity looks at the Shared Preferences and sees that userLoggedIn, is set to false, so it launches LoginActivity once again.由于我忘记这样做,当我尝试启动 MainAcitvity 时,MainActivity 查看共享首选项并看到 userLoggedIn 设置为 false,因此它再次启动 LoginActivity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM