由于某种原因,我的用户名和密码变量是红色的。当我将它们定义为字符串时,我会收到错误,说它们无法从编辑文本中转换出来....有没有办法真正解决这个问题?这令人恼火,因为我从一个教程中逐字逐句地工作,他们似乎没有这些问题。我已经尝试过各种各样的事情,例如将密码和用户名转换为字符串。它只是不能正常工作。
这是我迄今为止所尝试的内容。我只想要这个简单的登录/注册代码。 (代码更新)
package myapplication.example.com.cis490_project;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.parse.SignUpCallback;
public class loginactivity extends AppCompatActivity {
Button loginButton;
Button signUpButton;
EditText usernameField;
EditText passwordField;
String username;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loginactivity);
loginButton = (Button) findViewById(R.id.btlogin);
signUpButton = (Button) findViewById(R.id.btsignup);
usernameField = (EditText) findViewById(R.id.tusername);
passwordField = (EditText) findViewById(R.id.tpassword);
// Enable Local Datastore.
Parse.enableLocalDatastore(this);
Parse.initialize(this, "SECRET_KEY", "SECRET_KEY");
ParseObject testObject = new ParseObject("Testing");
testObject.put("foo", "badr");
testObject.saveInBackground();
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
ParseUser.logInInBackground(username, password, new LogInCallback() {
public void done(ParseUser user, com.parse.ParseException e) {
if (user != null) {
//start next activity
//start sinch service
} else {
Toast.makeText(getApplicationContext(),
"There was an error logging in.",
Toast.LENGTH_LONG).show();
}
}
});
}
});
signUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
ParseUser user = new ParseUser();
user.setUsername(username);
user.setPassword(password);
user.signUpInBackground(new SignUpCallback() {
public void done(com.parse.ParseException e) {
if (e == null) {
//start next activity
//start sinch service
} else {
Toast.makeText(getApplicationContext(),
"There was an error signing up."
, Toast.LENGTH_LONG).show();
}
}
});
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_loginactivity, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="myapplication.example.com.cis490_project.loginactivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/btlogin"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Password"
android:id="@+id/tpassword"
android:layout_above="@+id/btlogin"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Username"
android:id="@+id/tName"
android:layout_above="@+id/tpassword"
android:layout_alignLeft="@+id/tpassword"
android:layout_alignStart="@+id/tusername"
android:layout_marginBottom="40dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iprofile"
android:src="@drawable/com_facebook_profile_picture_blank_square"
android:layout_above="@+id/tName"
android:layout_alignRight="@+id/btlogin"
android:layout_alignEnd="@+id/btlogin"
android:layout_marginBottom="56dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup"
android:id="@+id/btsignup"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
答案 0 :(得分:1)
在setContentView
内onCreate
之后定义所有小部件。并将username
和password
定义为String变量。因为您没有在代码中的任何位置定义username
和password
。
public class loginactivity extends AppCompatActivity {
Button loginButton;
Button signUpButton;
EditText usernameField;
EditText passwordField;
String username;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loginactivity);
loginButton = (Button) findViewById(R.id.btlogin);
signUpButton = (Button) findViewById(R.id.btsignup);
usernameField = (EditText) findViewById(R.id.tusername);
passwordField = (EditText) findViewById(R.id.tpassword);
//rest of your code
..............
}