我在活動中有2個按鈕。一個是註銷按鈕,工作正常,另一個是相機按鈕,點擊時應導致另一個活動。但是,當我點擊相機按鈕進入下一個活動(相機功能),當前活動只是刷新自己,每次我點擊它是一個額外的時間,我必須點擊註銷按鈕註銷!(即如果我點擊相機按鈕5次我必須點擊註銷按鈕5次才能註銷)。這可能真的很愚蠢,但我是編程新手,所以請耐心等待!以下是相關的代碼片段。如果任何人都可以幫助,我會非常感激。在歡迎XML佈局文件Android活動刷新本身而不是打算進行預期活動
<Button
android:id="@+id/cameraButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/logout"
android:layout_centerHorizontal="true"
android:onClick="sendMessage"
android:text="@string/CameraBtn" />
清單
歡迎類
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.parse.ParseUser;
public class Welcome extends Activity {
// Declare Variable
Button logout;
Button cameraButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from singleitemview.xml
setContentView(R.layout.welcome);
// Retrieve current user from Parse.com
ParseUser currentUser = ParseUser.getCurrentUser();
// Convert currentUser into String
String struser = currentUser.getUsername().toString();
// Locate TextView in welcome.xml
TextView txtuser = (TextView) findViewById(R.id.txtuser);
// Set the currentUser String into TextView
txtuser.setText("You are logged in as " + struser);
// Locate Button in welcome.xml
logout = (Button) findViewById(R.id.logout);
// Logout Button Click Listener
logout.setOnClickListener(new OnClickListener() {
/** Called when the user clicks the Logout button */
public void onClick(View arg0) {
// Logout current user
ParseUser.logOut();
finish();
}
});
// Locate Button in welcome.xml
cameraButton = (Button) findViewById(R.id.cameraButton);
// Camera Button Click Listener
cameraButton.setOnClickListener(new OnClickListener() {
/** Called when the user clicks the Camera button */
public void onClick(View view) {
// Send user to Camera.class
Intent intent = new Intent(Welcome.this, Camera.class);
startActivity(intent);
}
});
}
}
按鈕代碼
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name="ParseApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:allowBackup="true"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.myapp.LoginSignupActivity" >
</activity>
<activity android:name="com.example.myapp.Welcome" >
</activity>
<activity
android:name="com.example.myapp.Camera">
</activity>
</application>
</manifest>
個字符串xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">myapp</string>
<string name="app_name">myapp</string>
<string name="Username">Username</string>
<string name="Password">Password</string>
<string name="LoginBtn">Login</string>
<string name="SignupBtn">Sign Up</string>
<string name="LogoutBtn">Log Out</string>
<string name="CameraBtn">Camera</string>
<string name="Welcome">Welcome!</string>
<string name="tap">Tap the image to open the camera!!</string>
<string name="title_camera">My Message</string>
</resources>
LoginSignup類
public class LoginSignupActivity extends Activity {
// Declare Variables
Button loginbutton;
Button signup;
String usernametxt;
String passwordtxt;
EditText password;
EditText username;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from main.xml
setContentView(R.layout.loginsignup);
// Locate EditTexts in main.xml
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
// Locate Buttons in main.xml
loginbutton = (Button) findViewById(R.id.login);
signup = (Button) findViewById(R.id.signup);
// Login Button Click Listener
loginbutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Retrieve the text entered from the EditText
usernametxt = username.getText().toString();
passwordtxt = password.getText().toString();
// Send data to Parse.com for verification
ParseUser.logInInBackground(usernametxt, passwordtxt,
new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
// If user exist and authenticated, send user to Welcome.class
Intent intent = new Intent(
LoginSignupActivity.this,
Welcome.class);
startActivity(intent);
Toast.makeText(getApplicationContext(),
"Successfully Logged in",
Toast.LENGTH_LONG).show();
finish();
} else {
Toast.makeText(
getApplicationContext(),
"No such user exist, please signup",
Toast.LENGTH_LONG).show();
}
}
});
}
});
// Sign up Button Click Listener
signup.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Retrieve the text entered from the EditText
usernametxt = username.getText().toString();
passwordtxt = password.getText().toString();
// Force user to fill up the form
if (usernametxt.equals("") && passwordtxt.equals("")) {
Toast.makeText(getApplicationContext(),
"Please complete the sign up form",
Toast.LENGTH_LONG).show();
} else {
// Save new user data into Parse.com Data Storage
ParseUser user = new ParseUser();
user.setUsername(usernametxt);
user.setPassword(passwordtxt);
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Show a simple Toast message upon successful registration
Toast.makeText(getApplicationContext(),
"Successfully Signed up, please log in.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Sign up Error", Toast.LENGTH_LONG)
.show();
}
}
});
}
}
});
}
}
ParseUser.logout
// Locate Button in welcome.xml
logout = (Button) findViewById(R.id.logout);
// Logout Button Click Listener
logout.setOnClickListener(new OnClickListener() {
/** Called when the user clicks the Logout button */
public void onClick(View arg0) {
// Logout current user
ParseUser.logOut();
finish();
}
});
我已經刪除了android:onClick =「sendMessage」,並將我的意圖更改爲「Intent intent = new Intent(getApplicationContext(),Camera.class); startActivity(intent)」以及調整我的清單(如上所示)但我仍然有同樣的問題! – Stewy
請發佈您的'ParseUser.logOut()'代碼。 – Philip
@ Phillip - 我已經用ParseUser詳細信息添加了整個loginsignup類 – Stewy