2017-09-26 80 views
1

我使用Volley,php,MYSQL和Shared首選項創建了我的登錄應用程序。 登錄和註冊活動成功,但登錄成功後頁面不會轉到其他活動。這是一個意圖還是另一個問題?請幫忙!使用volley,php,mysql在登錄成功後登錄活動意圖不會去其他活動

登錄活動:

 
//if everything is fine 
     StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.URL_LOGIN, 
       new Response.Listener() { 
        @Override 
        public void onResponse(String response) { 
         progressBar.setVisibility(View.GONE); 

         try { 
          //converting response to json object 
          JSONObject obj = new JSONObject(response); 

          //if no error in response 
          if (!obj.getBoolean("error")) { 
           Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show(); 
           //getting the user from the response 
           JSONObject userJson = obj.getJSONObject("user"); 

           //creating a new user object 
           User user = new User(
             userJson.getInt("id"), 
             userJson.getString("fullname"), 
             userJson.getString("phone") 
           ); 

           //storing the user in shared preferences 
           SharedPrefManager.getInstance(getApplicationContext()).userLogin(user); 

           //starting the profile activity 
           finish(); 
           startActivity(new Intent(getApplicationContext(), ProfileActivity.class)); 

          } else { 

           // when error 
           Toast.makeText(getApplicationContext(), "Ops! "+obj.getString("message"), Toast.LENGTH_SHORT).show(); 
          } 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       } 
+0

你可以嘗試添加一個標誌的意圖。 Intent intent = new Intent(LoginActivity.this,ProfileActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish(); –

+0

好吧,先生..我已經做到了這一點,但仍然一樣..只是吐司消息顯示.. – agazslee

+0

嘗試記錄一個消息,只是在startActivity之後,以確保執行代碼行。也看看是否有任何例外的LogCat –

回答

0

更改爲

//starting the profile activity 

    finish(); 
    startActivity(new Intent(getApplicationContext(), ProfileActivity.class)); 

startActivity(new Intent(LoginActivity.this, ProfileActivity.class)); 
finish(); 
+0

我試試這個,但同樣的錯誤..謝謝.. – agazslee

+0

我只是顯示吐司「登錄成功」..不改變其他活動.. – agazslee

+0

你想要後去profileactivity登錄成功的權利? –

0

這是LoginActivity.java

import android.content.DialogInterface; 
import android.content.Intent; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.AppCompatButton; 
import android.support.v7.widget.AppCompatEditText; 
import android.support.v7.widget.AppCompatTextView; 
import android.text.TextUtils; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ProgressBar; 
import android.widget.Toast; 

import com.android.volley.AuthFailureError; 
import com.android.volley.Request; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.toolbox.StringRequest; 

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

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

public class LoginActivity extends AppCompatActivity { 
    AppCompatEditText etPhone, etPassword; 
    ProgressBar progressBar; 
    AppCompatTextView tvRegister; 
    AppCompatButton buttonLogin; 


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

      if (SharedPrefManager.getInstance(this).isLoggedIn()) { 
       finish(); 
       startActivity(new Intent(this, ProfileActivity.class)); 
      } 

      progressBar = (ProgressBar) findViewById(R.id.progressBar); 
      etPhone = (AppCompatEditText) findViewById(R.id.etPhone); 
      etPassword = (AppCompatEditText) findViewById(R.id.etPassword); 
      buttonLogin = (AppCompatButton) findViewById(R.id.buttonLogin); 

      //if user presses on login 
      //calling the method login 


      buttonLogin.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        userLogin(); 
       } 
      }); 

      //if user presses on not registered 
      tvRegister = (AppCompatTextView) findViewById(R.id.tvRegister); 
      tvRegister.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        //open register screen 
        finish(); 
        startActivity(new Intent(getApplicationContext(), MainActivity.class)); 
       } 
      }); 
     } 


    private void userLogin() { 
     //first getting the values 
     final String phone = etPhone.getText().toString(); 
     final String password = etPassword.getText().toString(); 

     //validating inputs 
     if (TextUtils.isEmpty(phone)) { 
      etPhone.setError("Please enter your username"); 
      etPhone.requestFocus(); 
      return; 
     } 

     if (TextUtils.isEmpty(password)) { 
      etPassword.setError("Please enter your password"); 
      etPassword.requestFocus(); 
      return; 
     } 

     //if everything is fine 
     StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.URL_LOGIN, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 

         progressBar.setVisibility(View.GONE); 

         try { 
          //converting response to json object 
          JSONObject obj = new JSONObject(response); 

          //if no error in response 
          if (!obj.getBoolean("error")) { 
           Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show(); 

           //getting the user from the response 
           JSONObject userJson = obj.getJSONObject("user"); 

           //creating a new user object 
           User user = new User(
             userJson.getInt("id"), 
             userJson.getString("phone"), 
             userJson.getString("fullname") 
           ); 

           //storing the user in shared preferences 
           SharedPrefManager.getInstance(getApplicationContext()).userLogin(user); 

           //starting the profile activity 
           finish(); 
           startActivity(new Intent(getApplicationContext(), ProfileActivity.class)); 
          } else { 
           Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show(); 
          } 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); 
        } 
       }) { 
      @Override 
      protected Map<String, String> getParams() throws AuthFailureError { 
       Map<String, String> params = new HashMap<>(); 
       params.put("phone", phone); 
       params.put("password", password); 
       return params; 
      } 
     }; 

     VolleySingleton.getInstance(this).addToRequestQueue(stringRequest); 
    } 

    /** 
    * This method to EXIT COMPLETELY application! 
    */ 
    @Override 
    public void onBackPressed() { 
     new AlertDialog.Builder(this).setIcon(null).setTitle("Exit") 
       .setMessage("Sure to exit?") 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

         Intent intent = new Intent(Intent.ACTION_MAIN); 
         intent.addCategory(Intent.CATEGORY_HOME); 
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         startActivity(intent); 
         finish(); 
        } 
       }).setNegativeButton("No", null).show(); 
    } 
} 

和logcat:

09-28 04:42:19.391 15056-15056/com.agazslees.helpmehere E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering 
09-28 04:42:25.213 15056-15056/com.agazslees.helpmehere E/ActivityThread: Activity com.agazslees.helpmehere.SplashFullScreen has leaked IntentReceiver [email protected]f8 that was originally registered here. Are you missing a call to unregisterReceiver()? 
                      android.app.IntentReceiverLeaked: Activity com.agazslees.helpmehere.SplashFullScreen has leaked IntentReceiver [email protected]f8 that was originally registered here. Are you missing a call to unregisterReceiver()? 
                       at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java) 
                       at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java) 
                       at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java) 
                       at android.app.ContextImpl.registerReceiver(ContextImpl.java) 
                       at android.app.ContextImpl.registerReceiver(ContextImpl.java) 
                       at android.content.ContextWrapper.registerReceiver(ContextWrapper.java) 
                       at com.kqstone.immersedstatusbar.injector.ActivityInjector.hookAfterPerformResume(ActivityInjector.java) 
                       at com.kqstone.immersedstatusbar.hook.ActivityHook.performResumeAfter(ActivityHook.java) 
                       at android.app.Activity.performResume(Activity.java) 
                       at android.app.ActivityThread.performResumeActivity(ActivityThread.java) 
                       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java) 
                       at android.app.ActivityThread.access$800(ActivityThread.java) 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java) 
                       at android.os.Handler.dispatchMessage(Handler.java) 
                       at android.os.Looper.loop(Looper.java) 
                       at android.app.ActivityThread.main(ActivityThread.java) 
                       at java.lang.reflect.Method.invokeNative(Native Method) 
                       at java.lang.reflect.Method.invoke(Method.java:515) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:835) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651) 
                       at dalvik.system.NativeStart.main(Native Method) 

有人可以檢查這個。什麼問題?