【问题标题】:Android activity refreshes itself instead of going to intended activityAndroid 活动会自行刷新,而不是进入预期活动
【发布时间】:2014-03-24 23:33:29
【问题描述】:

我在一个活动中有 2 个按钮。一个是注销按钮,可以正常工作,另一个是相机按钮,单击该按钮会导致另一个活动。但是,当我单击相机按钮转到下一个活动(相机功能)时,当前活动只会自行刷新,每次单击它都是额外的时间,我必须单击注销按钮才能注销!(即,如果我单击相机按钮 5 次我必须单击注销按钮 5 次才能注销)。这可能是非常愚蠢的事情,但我是编程新手,所以请耐心等待!这里是相关的代码sn-ps。如果有人可以提供帮助,我将不胜感激。

欢迎课

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 布局文件中的按钮代码

<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" />

清单

<?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 eclipse camera android-activity


    【解决方案1】:

    按钮

    <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" />
    

    android:onClick="sendMessage" 行表示当单击按钮时,应调用 sendMessage 方法。除非您有 sendMessage 方法,否则我会从您的代码中删除这一行。

    意图

    /** 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);
    
    }
    

    new Intent(Welcome.this, Camera.class) 是一种可接受的开始新活动的方式。通过使用Welcome.this 而不是this,您可以正确地获取上下文。

    【讨论】:

    • 我已删除 android:onClick="sendMessage" 并将我的意图更改为 "Intent intent = new Intent(getApplicationContext(), Camera.class); startActivity(intent)" 以及调整我的清单(如上图)但我还是有同样的问题!!
    • @Phillip - 我已经添加了带有 ParseUser 详细信息的整个 loginsignup 类
    • 我似乎没有 ParseUser.logOut()
    • 你在注销按钮监听器的onClick方法中使用它。
    • 找到了,我已经把它放在问题的底部了!
    【解决方案2】:

    解决了,我在相机类中扩展了 MainActivity 而不是扩展了活动!!

    【讨论】:

      【解决方案3】:

      我必须将此作为答案,因为我需要更多的声誉点来发表评论。 一个按钮基本上有两个 onClick 事件,因此您可能需要解决此问题。

      android:onClick="sendMessage"
      
       // Camera Button Click Listener
      cameraButton.setOnClickListener(new View.OnClickListener()
      

      而且有时使用 getApplicationContext() 会更好

      Intent intent = new Intent(getApplicationContext(), Camera.class);
      startActivity(intent)
      

      尝试将您的 onClick 方法修改为以下

      // Add a click listener to perform action when button is clicked
               cameraButton.setOnClickListener(
                  new View.OnClickListener()
                  {
                      @Override
                      public void onClick( View view )
                      {
                      }
                  });
      

      【讨论】:

      • 我已删除 android:onClick="sendMessage" 并将我的意图更改为 "Intent intent = new Intent(getApplicationContext(), Camera.class); startActivity(intent)" 以及调整我的清单(如上图)但我还是有同样的问题!!
      • 你能发布退出按钮的xml吗?
      • 看看上面。我将 onClickListener() 更改为 View.OnClickListener()
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      相关资源
      最近更新 更多