我是开发人员的新手,我开始按照本教程创建一个消息应用: http://sinch.github.io/android-messaging-tutorial/
添加第二个Activity(ListUsersActivity)及其布局文件后,每次运行应用程序时都会崩溃,不知道为什么会这样。
这是ListUsersActivity:
package com.sinch.messagingtutorialskeleton;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.example.messagingtutorialskeleton.R;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import java.util.ArrayList;
import java.util.List;
public class ListUsersActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstaceState){
super.onCreate(savedInstaceState);
setContentView(com.example.messagingtutorialskeleton.R.layout.activity_list_users);
logoutButton = (Button) findViewById(R.id.logoutButton);
logoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ParseUser.logOut();
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
}
});
setConversationList();
}
private void setConversationList() {
currentUserId = ParseUser.getCurrentUser().getObjectId();
names = new ArrayList<String>();
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereNotEqualTo("objectId", currentUserId);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List <ParseUser> userList, ParseException e) {
if (e == null) {
for (int i=0; i<userList.size(); i++) {
names.add(userList.get(i).getUsername().toString());
}
usersListView = (ListView)findViewById(R.id.usersListView);
namesArrayAdapter =
new ArrayAdapter<String>(getApplicationContext(),
R.layout.user_list_item, names);
usersListView.setAdapter(namesArrayAdapter);
} else {
Toast.makeText(getApplicationContext(),
"Error loading user list",
Toast.LENGTH_LONG).show();
}
}
});
usersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
Toast.makeText(getApplicationContext(),
"You clicked on user: " + i,
Toast.LENGTH_SHORT).show();
//Look up user id and open conversation
}
});
}
private Button logoutButton;
private String currentUserId;
private ArrayAdapter<String> namesArrayAdapter;
private ArrayList<String> names;
private ListView usersListView;
}
现在的MainActivity(LoginActivity):
package com.sinch.messagingtutorialskeleton;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.messagingtutorialskeleton.R;
import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;
public class LoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
//start new activity
Intent intent = new Intent(getApplicationContext(), ListUsersActivity.class);
startActivity(intent);
}
setContentView(com.example.messagingtutorialskeleton.R.layout.activity_login);
Parse.initialize(this, "uUv5HM8nqz9gVQBDWaw1bDLxbD3SFQPQtvDESJG1", "BHVp4MQbiOODY3yGOrM7pgaZ0xTbCPI1v6dfRU6C");
loginButton = (Button) findViewById(R.id.loginButton);
signUpButton = (Button) findViewById(R.id.signupButton);
usernameField = (EditText) findViewById(R.id.loginUsername);
passwordField = (EditText) findViewById(R.id.loginPassword);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Login",
Toast.LENGTH_SHORT).show();
username = usernameField.getText().toString();
password = passwordField.getText().toString();
ParseUser.logInInBackground(username, password, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
//start new activity
Intent intent = new Intent(getApplicationContext(), ListUsersActivity.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(),
"Wrong username/password combo",
Toast.LENGTH_LONG).show();
}
}
});
}
});
signUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Sign Up",
Toast.LENGTH_SHORT).show();
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(ParseException e) {
if (e == null) {
//start new activity
Intent intent = new Intent(getApplicationContext(), ListUsersActivity.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(),
"There was an error signing up."
, Toast.LENGTH_LONG).show();
}
}
});
}
});
}
private Button signUpButton;
private Button loginButton;
private EditText usernameField;
private EditText passwordField;
private String username;
private String password;
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.messagingtutorialskeleton" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar"> >
<activity
android:name="com.sinch.messagingtutorialskeleton.LoginActivity"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.sinch.messagingtutorialskeleton.ListUsersActivity"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>
activity_login.xml(LoginActivity的布局文件):
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="com.sinch.messagingtutorial.app.LoginActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="@string/username"
android:id="@+id/loginUsernameText"
android:layout_gravity="center_horizontal"
android:textColor="@color/sinch_purple"
android:textSize="20sp"/>
<EditText
android:id="@+id/loginUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:inputType="text"
android:textSize="32sp"
android:padding="10dp"
android:textColor="@color/sinch_purple"
android:background="@drawable/inputbox"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">
<requestFocus/>
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="@string/password"
android:id="@+id/loginPasswordText"
android:layout_gravity="center_horizontal"
android:textColor="@color/sinch_purple"
android:textSize="20sp"/>
<EditText
android:id="@+id/loginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="40dp"
android:inputType="textPassword"
android:textSize="32sp"
android:padding="10dp"
android:textColor="@color/sinch_purple"
android:background="@drawable/inputbox"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="40dp"
android:layout_gravity="center">
<Button
android:id="@+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_login"/>
<Button
android:id="@+id/signupButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_signup"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
activity_list_users.xml(UserListActivity布局):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.sinch.messagingtutorial.app.ListUsersActivity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/logout"
android:id="@+id/logoutButton"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_horizontal"
android:background="@color/dark_gray"
android:textSize="24sp"
android:padding="15dp"
android:textColor="@color/sinch_purple" />
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="@color/off_white"
android:id="@+id/usersListView">
</ListView>
</LinearLayout>
将logcat放在这里时出错,所以我把它上传到了pastebin: http://pastebin.com/nzg2YbfZ
答案 0 :(得分:0)
尝试移动:
setContentView(com.example.messagingtutorialskeleton.R.layout.activity_login);
Parse.initialize(this, "uUv5HM8nqz9gVQBDWaw1bDLxbD3SFQPQtvDESJG1","BHVp4MQbiOODY3yGOrM7pgaZ0xTbCPI1v6dfRU6C");
loginButton = (Button) findViewById(R.id.loginButton);
signUpButton = (Button) findViewById(R.id.signupButton);
usernameField = (EditText) findViewById(R.id.loginUsername);
passwordField = (EditText) findViewById(R.id.loginPassword);
之前:
if (currentUser != null) {
//start new activity
Intent intent = new Intent(getApplicationContext(), ListUsersActivity.class);
startActivity(intent);
}
后:
ParseUser currentUser = ParseUser.getCurrentUser();
我只是猜测,当你开始活动时,它就会停在那里,并且没有得到剩下的东西,包括 Parse.initialize();如果它不起作用,那么抱歉!请不要downvote!
修改强>
正如弗雷德里克在下面所说的,它引用了那条线,在它调用活动之前是可疑的,并且它表示在该行的位置应该有Parse.initialize(),或者在你开始之前新活动。
答案 1 :(得分:0)
检查您发布的logcat。
Caused by: java.lang.RuntimeException: You must call Parse.initialize(context, oauthKey, oauthSecret) before using the Parse library.
at com.parse.ParseUser.checkApplicationContext(ParseUser.java:1104)
at com.parse.ParseUser.getCurrentUser(ParseUser.java:904)
at com.sinch.messagingtutorialskeleton.LoginActivity.onCreate(LoginActivity.java:24)
如果您检查代码引用此行(LoginActivity:24)...
ParseUser currentUser = ParseUser.getCurrentUser();
我没有使用过该库,但很明显你必须调用
Parse.initialize(this, "**parse key**","**parse key**");
在 ParseUser.getCurrentUser();
之前**对于ListUserActivity中的第二次崩溃**
将行拆分为两行,这样就可以检查&#34; getCurrentUser()&#34;方法失败了:
...
// Line 65
ParseUser mCurrentUser = ParseUser.getCurrentUser(); // Breakpoint here in Android Studio and go step by step for checking return value
currentUserId = mCurrentUser.getObjectId();
...
有关调试的建议......