2017-09-04 1 views
-5

몇 가지 활동으로 안드로이드 응용 프로그램을 만들었습니다. 문제는 내 로그인 활동에 "새 사용자, reg 여기"라는 텍스트가 있습니다. 클릭하면 Reg.java 페이지로 이동합니다. 그러나 대신, 그것은 같은 페이지를 다시로드하는 것입니다.
내 매니페스트를 확인했습니다. 괜찮습니다. 또한,이 텍스트 뷰를 다른 액티비티와 연결하려고했는데 제대로 작동합니다. 하지만 Reg.java에서는 작동하지 않습니다.안드로이드 onclick 청취자가 원하는 활동을 열지 못합니다.

코드 :

LoginActivity.java 
package com.example.mi.mikpiadmin; 

import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import org.json.JSONException; 

import java.io.IOException; 
import java.util.HashMap; 

public class LoginActivity extends AppCompatActivity { 

    private EditText etusername, etid; 
    Button btnlogin; 
    TextView tvreg; 
    private ParseContent parseContent; 
    private final int LoginTask = 1; 
    private PreferenceHelper preferenceHelper; 

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

     parseContent = new ParseContent(this); 
     preferenceHelper = new PreferenceHelper(this); 

     etusername = (EditText) findViewById(R.id.etusername); 
     etid = (EditText) findViewById(R.id.etpassword); 

     btnlogin = (Button) findViewById(R.id.btn); 
     tvreg = (TextView) findViewById(R.id.tvreg); 

     tvreg.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       **Intent intent = new Intent(LoginActivity.this, Reg.class); 
       startActivity(intent);** 
      } 
     }); 

     btnlogin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       try { 
        login(); 
       } catch (IOException |JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    private void login() throws IOException, JSONException { 

     if (!AndyUtils.isNetworkAvailable(LoginActivity.this)) { 
      Toast.makeText(LoginActivity.this, "Internet is required!", Toast.LENGTH_SHORT).show(); 
      return; 
     } 
     AndyUtils.showSimpleProgressDialog(LoginActivity.this); 
     final HashMap<String, String> map = new HashMap<>(); 
     map.put(AndyConstants.Params.NAME, etusername.getText().toString()); 
     map.put(AndyConstants.Params.MI_ID, etid.getText().toString()); 
     new AsyncTask<Void, Void, String>(){ 
      protected String doInBackground(Void[] params) { 
       String response=""; 
       try { 
        HttpRequest req = new HttpRequest(AndyConstants.ServiceType.LOGIN); 
        response = req.prepare(HttpRequest.Method.POST).withData(map).sendAndReadString(); 
       } catch (Exception e) { 
        response=e.getMessage(); 
       } 
       return response; 
      } 
      protected void onPostExecute(String result) { 
       //do something with response 
       Log.d("newwwss", result); 
       onTaskCompleted(result,LoginTask); 
      } 
     }.execute(); 
    } 

    private void onTaskCompleted(String response, int task) { 
     Log.d("responsejson", response.toString()); 
     AndyUtils.removeSimpleProgressDialog(); //will remove progress dialog 
     switch (task) { 
      case LoginTask: 
       if (parseContent.isSuccess(response)) { 
        parseContent.saveInfo(response); 
        Toast.makeText(LoginActivity.this, "Login Successfully!", Toast.LENGTH_SHORT).show(); 
        Intent intent = new Intent(LoginActivity.this, Home.class); 
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(intent); 
        this.finish(); 
       }else { 
        Toast.makeText(LoginActivity.this, parseContent.getErrorMessage(response), Toast.LENGTH_SHORT).show(); 
       } 
     } 
    } 
} 

Reg.java

 package com.example.mi.mikpiadmin; 

     import android.content.Intent; 
     import android.os.AsyncTask; 
     import android.os.Bundle; 
     import android.support.v7.app.AppCompatActivity; 
     import android.util.Log; 
     import android.view.View; 
     import android.widget.Button; 
     import android.widget.EditText; 
     import android.widget.TextView; 
     import android.widget.Toast; 

     import org.json.JSONException; 

     import java.io.IOException; 
     import java.util.HashMap; 

     public class Reg extends AppCompatActivity { 

      private EditText etid, etname, etemail, etphone; 
      Button btnregister; 
      TextView tvlogin; 
      private ParseContent parseContent; 
      PreferenceHelper preferenceHelper; 
      private final int RegTask = 5; 

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

       preferenceHelper = new PreferenceHelper(this); 
       parseContent = new ParseContent(this); 

       if(preferenceHelper.getIsLogin()){ 
        Intent intent = new Intent(Reg.this,LoginActivity.class); 
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(intent); 
        this.finish(); 
       } 
       etid = (EditText) findViewById(R.id.etmi_id); 
       etname = (EditText) findViewById(R.id.etname); 
       //etstorename = (EditText) findViewById(R.id.etstore_name); 
       etemail = (EditText) findViewById(R.id.etemail); 
       etphone = (EditText) findViewById(R.id.etphone); 
       //etdoj = (EditText) findViewById(R.id.etdoj); 
       btnregister = (Button) findViewById(R.id.btn); 
       tvlogin = (TextView) findViewById(R.id.tvlogin); 

       tvlogin.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         Intent intent = new Intent(Reg.this,LoginActivity.class); 
         startActivity(intent); 
        } 
       }); 

       btnregister.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         try { 
          register(); 
         } catch (IOException | JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }); 

      } 

      private void register() throws IOException, JSONException { 

       if (!AndyUtils.isNetworkAvailable(Reg.this)) { 
        Toast.makeText(Reg.this, "Internet is required!", Toast.LENGTH_SHORT).show(); 
        return; 
       } 
       AndyUtils.showSimpleProgressDialog(Reg.this); 
       final HashMap<String, String> map = new HashMap<>(); 
       map.put(AndyConstants.Params.MI_ID, etid.getText().toString()); 
       map.put(AndyConstants.Params.NAME, etname.getText().toString()); 
       // map.put(AndyConstants.Params.STORE_NAME, etstorename.getText().toString()); 
       map.put(AndyConstants.Params.EMAIL, etemail.getText().toString()); 
       map.put(AndyConstants.Params.PHONE, etphone.getText().toString()); 
       // map.put(AndyConstants.Params.DOJ, etdoj.getText().toString()); 
       new AsyncTask<Void, Void, String>(){ 
        protected String doInBackground(Void[] params) { 
         String response=""; 
         try { 
          HttpRequest req = new HttpRequest(AndyConstants.ServiceType.REGISTER); 
          response = req.prepare(HttpRequest.Method.POST).withData(map).sendAndReadString(); 
         } catch (Exception e) { 
          response=e.getMessage(); 
         } 
         return response; 
        } 
        protected void onPostExecute(String result) { 
         //do something with response 
         Log.d("newwwss", result); 
         onTaskCompleted(result, RegTask); 
        } 
       }.execute(); 
      } 

      private void onTaskCompleted(String response, int task) { 
       Log.d("responsejson", response.toString()); 
       AndyUtils.removeSimpleProgressDialog(); //will remove progress dialog 
       switch (task) { 
        case RegTask: 

         if (parseContent.isSuccess(response)) { 

          parseContent.saveInfo(response); 
          Toast.makeText(Reg.this, "Registered Successfully!", Toast.LENGTH_SHORT).show(); 
          Intent intent = new Intent(Reg.this,Home.class); 
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
          startActivity(intent); 
          this.finish(); 

         }else { 
          Toast.makeText(Reg.this, parseContent.getErrorMessage(response), Toast.LENGTH_SHORT).show(); 
         } 
       } 
      } 
     } 

매니페스트

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.mi.mikpiadmin"> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".LoginActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Home" 
      android:label="KPI" 
      android:theme="@style/AppTheme.NoActionBar"/> 

     <activity android:name=".See_Feedback" 
      android:label="Feedback"/> 

     <activity android:name=".Reg" 
      android:label="Registration" 
      android:theme="@style/AppTheme.NoActionBar"/> 

     <activity 
      android:name=".Adminhome" 
      android:label="@string/title_activity_adminhome" 
      android:theme="@style/AppTheme.NoActionBar"/> 

     <activity android:name=".Admin_store_contacts" 
      android:label="Store Contacts" 
      android:theme="@style/AppTheme.NoActionBar"/> 
    </application> 

</manifest> 

답변

1

사용 로그 및 preferenceHelper.getIsLogin()의 값을 확인하면 등록 활동을 입력 할 때; true이라고 확신하고 코드의 일부로 인해 다시로드됩니다.

if(preferenceHelper.getIsLogin()){ 
       Intent intent = new Intent(Reg.this,LoginActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(intent); 
       this.finish(); 
      } 
관련 문제