我制作了一个带有一些活动的Android应用程序。问题出在我的登录活动中,有一个名为“new user,reg here”的文本。当我点击它时,它应该转到Reg.java页面。但相反,它只是重新加载同一页面 我检查了我的清单,没关系。此外,我试图将此textView链接到另一个活动,它工作正常。但它不适用于Reg.java。
代码:
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>
答案 0 :(得分:1)
使用日志并在输入Reg活动时检查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();
}