我创建了将数据发送到数据库的注册活动,按钮必须打开另一个活动,即MainActivity。代码工作正常,但有一个问题。单击该按钮时,它会响应并将信息发送到数据库,但活动不会更改。
如何更改代码以打开其他活动?
RegisterRequest.java
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://loginacti.comli.com/Register.php";
private Map<String, String> params;
public RegisterRequest(String Expect, String Prize, String Name, String Email, Response.Listener<String> listener) {
super(Method.POST, REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("Expect", Expect);
params.put("Prize", Prize);
params.put("Name", Name);
params.put("Email", Email);
}
@Override
public Map<String, String> getParams() {
return params;
}
RegisterActivity.java
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText etUknown = (EditText) findViewById(R.id.etUknown);
final EditText etPrize = (EditText) findViewById(R.id.etPrize);
final EditText etName = (EditText) findViewById(R.id.etName);
final EditText etEmail = (EditText) findViewById(R.id.etEmail);
final Button bRegister = (Button) findViewById(R.id.bRegister);
bRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String Expect = etUknown.getText().toString();
final String Prize = etPrize.getText().toString();
final String Name = etName.getText().toString();
final String Email = etEmail.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>(){
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success){
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
RegisterActivity.this.startActivity(intent);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(Expect, Prize, Name, Email, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView registerLink = (TextView) findViewById(R.id.eRegister);
registerLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent registerIntent = new Intent(MainActivity.this, RegisterActivity.class);
MainActivity.this.startActivity(registerIntent);
}
});
}
清单
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterActivity"></activity>
</application>
activity_register.xml
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etUknown"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="60dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Numbers" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etPrize"
android:hint="Secret CD"
android:layout_below="@+id/etUknown"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/etUknown"
android:layout_alignEnd="@+id/etUknown" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/etName"
android:hint="Full Name"
android:layout_below="@+id/etPrize"
android:layout_alignRight="@+id/etPrize"
android:layout_alignEnd="@+id/etPrize"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/etEmail"
android:hint="New Email"
android:layout_below="@+id/etName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/etName"
android:layout_alignEnd="@+id/etName" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:id="@+id/bRegister"
android:layout_marginTop="46dp"
android:layout_below="@+id/etEmail"
android:layout_centerHorizontal="true" />
答案 0 :(得分:2)
在onclick中你不能使用.finish(),它通常会退出当前活动。但是,您可以创建一个方法exitActivity(),然后从您的onclick中调用它。因此,在写入数据库之后,在onClick结束时,使用以下命令创建一个try块:
try {
exitActivity();
}
catch (Exception e){
e.printStackTrace();
}
然后在您的RegisterActivity中创建一个exitActivity(),如下所示:
private void exitActivity(){
this.finish();
}
你想这样做,因为如果你这样做,你会在尝试注册更多项目时制作多个主要活动实例。这可能会导致您的应用最终耗尽内存。
答案 1 :(得分:1)
再添加一项权限ACCESS_NETWORK_STATE
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterActivity"></activity>
</application>
答案 2 :(得分:1)
它会抛出错误还是只是无所事事?
你也可以尝试:
Intent registerIntent = new Intent(this, MainActivity.class);
startActivity(registerIntent);
而不是
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
RegisterActivity.this.startActivity(intent);
答案 3 :(得分:1)
在&#39; onClick&#39;中尝试这一行代码方法:
startActivity(new Intent(MainActivity.this, RegisterActivity.class));