过去3天我一直在努力解决这个问题,但无法理解。我不太确定我错过了什么,我希望得到一些澄清。
基本上,我正在构建这个应用程序,我完成了如何发送和接收文本,同样适用于图片和视频。但是,我决定为用户提供一个选项,用于创建调查问卷并将其发送到用户的收件箱。到目前为止,除问卷调查部分外,一切都取得了成功。我使用parse.com作为后端。我能够在解析中保存数据。因此,用户能够创建问卷,但是当他/她完成创建调查问卷并单击按钮以查看要将其发送到的收件人列表时,应用程序崩溃。我尝试检索收件人活动中的数据并在投票活动中切换意图但没有成功,它继续崩溃。我在下面发布了代码的投票活动和收件人活动中出现问题。我将非常感谢您的指导或指导。
public class VoteActivity extends Activity {
private Vote vote;
private EditText mainText;
private EditText mTextOne;
private EditText mTextTwo;
private EditText mTextThree;
private String postmaintText;
private String posttextOne;
private String posttextTwo;
private String posttextThree;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vote);
Intent intent = this.getIntent();
mainText = (EditText) findViewById(R.id.questionVote);
mTextOne = (EditText) findViewById(R.id.choiceOne);
mTextTwo = (EditText) findViewById(R.id.choiceTwo);
mTextThree = (EditText) findViewById(R.id.choiceThree);
mButton =(Button) findViewById(R.id.createQ);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
postmaintText = mainText.getText().toString();
posttextOne = mTextOne.getText().toString();
posttextTwo = mTextTwo.getText().toString();
posttextThree = mTextThree.getText().toString();
postmaintText.trim();
posttextOne.trim();
posttextTwo.trim();
posttextThree.trim();
if (postmaintText.isEmpty() || posttextOne.isEmpty() || posttextTwo.isEmpty()|| posttextThree.isEmpty())
{
AlertDialog.Builder builder = new AlertDialog.Builder(VoteActivity.this);
builder.setMessage(R.string.voteMessage)
.setTitle(R.string.vote_error_title)
.setPositiveButton(android.R.string.ok,null);
AlertDialog dialog = builder.create();
dialog.show();
}
else
{
final ParseObject post = new ParseObject("Messages");
post.put("questionare", postmaintText);
post.put("optionOne", posttextOne);
post.put("optionTwo", posttextTwo);
post.put("optionThree", posttextThree);
post.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e==null) {
// questioner posted successfully
vote = new Vote (post.getObjectId(), postmaintText, posttextOne, posttextTwo,posttextThree);
Toast.makeText(getApplication(), "Sent", Toast.LENGTH_SHORT).show();
***// below is the intent where the list of recipents will be displayed for the user, that is where the app crashes***
Intent intent = new Intent(VoteActivity.this, recipientsActivity.class);
startActivity(intent);
}
else {
Toast.makeText(getApplicationContext(), "Failed to post", Toast.LENGTH_SHORT).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_vote, 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);
}
}
以下是收件人活动
public class recipientsActivity extends ListActivity {
public static final String TAG = recipientsActivity.class.getSimpleName();
protected List<ParseUser> mFriends;
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
protected MenuItem mSendMenuItem;
protected Uri mMediaUri;
protected String mFileType;
protected String mMyMessage;
protected String mMainquestionare;
protected String mOptionone;
protected String mOptiontwo;
protected String mOptionthree;
private List<Vote> mVotes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_recipients);
// Show the Up button in the action bar.
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mMediaUri = getIntent().getData();
mFileType = getIntent().getExtras().getString(parseConstants.KEY_FILE_TYPE);
//***************
mMyMessage = getIntent().getExtras().getString("themessage");
//***************
mMainquestionare = getIntent().getExtras().getString("questionare");
mOptionone = getIntent().getExtras().getString("optionOne");
mOptiontwo = getIntent().getExtras().getString("optionTwo");
mOptionthree = getIntent().getExtras().getString("optionThree");
}
@Override
public void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser.getRelation(parseConstants.KEY_FRIENDS_RELATION);
setProgressBarIndeterminate(true);
ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
query.addAscendingOrder(parseConstants.KEY_USERNAME);
query.findInBackground(new FindCallback<ParseUser>()
{
// friends
@Override
public void done(List<ParseUser> friends, ParseException e) {
setProgressBarIndeterminate(false);
if (e == null) {
mFriends = friends;
String[] usernames = new String[mFriends.size()];
int i = 0;
for (ParseUser user : mFriends) {
usernames[i] = user.getUsername();
i++;
}
// check list
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(getListView().getContext(),
android.R.layout.simple_list_item_checked,
usernames);
setListAdapter(adapter);
} else {
Log.e(TAG, e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(recipientsActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.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_recipients, menu);
mSendMenuItem = menu.getItem(0);
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.
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_send:
ParseObject message = createMessage();
//send(message);
if (message == null) {
// error
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.error_selecting_file)
.setTitle(R.string.error_selecting_file_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
} else {
send(message);
finish();
}
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// show the number of recipents that are checked
if (l.getCheckedItemCount() > 0) {
mSendMenuItem.setVisible(true);
} else {
mSendMenuItem.setVisible(false);
}
}
protected ParseObject createMessage() {
ParseObject message = new ParseObject(parseConstants.CLASS_MESSAGES);
ParseQuery<ParseObject> query = ParseQuery.getQuery(parseConstants.CLASS_MESSAGES);
query.whereEqualTo("senderName", ParseUser.getCurrentUser().getUsername());
message.put(parseConstants.KEY_SENDER_ID, ParseUser.getCurrentUser().getObjectId());
message.put(parseConstants.KEY_SENDER_NAME, ParseUser.getCurrentUser().getUsername());
message.put(parseConstants.KEY_RECIPIENT_IDS, getRecipientIds());
message.put(parseConstants.KEY_FILE_TYPE, mFileType);
//*************
if (mFileType.equals(parseConstants.TYPE_TEXT)) {
message.put("themessage", mMyMessage);
message.put(parseConstants.KEY_FILE_TYPE, "message");
return message;
} else if (mFileType.equals(parseConstants.TYPE_QUESTIONARE)) {
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if (e == null) {
String myQuestonare = mMainquestionare.toString();
String myOptionOne = mOptionone.toString();
String myOptionTwo = mOptiontwo.toString();
String myOptionThree = mOptionthree.toString();
Log.d("Brand", "Retrieved " + myQuestonare + " question" + myOptionOne + myOptionTwo + myOptionThree);
// mVotes.clear();
// for (ParseObject vote :parseObjects ) {
//Vote vote1 = new Vote (vote.getObjectId("questionare"));
//mVotes.add(vote1);
}
else
{
Log.d(getClass().getSimpleName(), "Error: " + e.getMessage());
}
}
});
} else {
//***************
byte[] fileBytes = FileHelper.getByteArrayFromFile(this, mMediaUri);
if (fileBytes == null) {
return null;
} else {
if (mFileType.equals(parseConstants.TYPE_IMAGE)) {
fileBytes = FileHelper.reduceImageForUpload(fileBytes);
}
String fileName = FileHelper.getFileName(this, mMediaUri, mFileType);
ParseFile file = new ParseFile(fileName, fileBytes);
message.put(parseConstants.KEY_FILE, file);
return message;
}
//***********
}
//***********
return message;
}
protected ArrayList<String> getRecipientIds() {
ArrayList<String> recipientIds = new ArrayList<String>();
for (int i = 0; i < getListView().getCount(); i++) {
if (getListView().isItemChecked(i)) {
recipientIds.add(mFriends.get(i).getObjectId());
}
}
return recipientIds;
}
protected void createQ()
{
}
protected void send (ParseObject message) {
message.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
// sucessful
Toast.makeText(recipientsActivity.this, R.string.success_message, Toast.LENGTH_LONG).show();
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(recipientsActivity.this);
builder.setMessage(R.string.error_sending_message)
.setTitle(R.string.error_selecting_file_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}
messageAdapter
public class messageAdapter extends ArrayAdapter <ParseObject> {
protected Context mContext;
protected List<ParseObject> mMessages;
public messageAdapter(Context context, List<ParseObject> messages) {
super(context, R.layout.message_item, messages);
mContext = context;
mMessages = messages;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.message_item, null);
holder = new ViewHolder();
holder.iconImageView = (ImageView) convertView.findViewById(R.id.messageIcon);
holder.nameLabel = (TextView) convertView.findViewById(R.id.senderLabel);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
ParseObject message = mMessages.get(position);
if (message.getString(parseConstants.KEY_FILE_TYPE).equals(parseConstants.TYPE_IMAGE)) {
holder.iconImageView.setImageResource(R.drawable.ic_action_new_picture);
}
else if (message.getString(parseConstants.KEY_FILE_TYPE).equals(parseConstants.TYPE_VIDEO)){
holder.iconImageView.setImageResource(R.drawable.ic_action_video);
}else {
holder.iconImageView.setImageResource(R.drawable.ic_action_edit);
}
holder.nameLabel.setText(message.getString(parseConstants.KEY_SENDER_NAME));
return convertView;
}
private static class ViewHolder {
ImageView iconImageView;
TextView nameLabel;
}
}
inboxFragment
public class InboxFragment extends ListFragment {
protected List<ParseObject> mMessages;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_inbox, container, false);
return rootView;
}
// we are going to query the new message class that we just created in parse
@Override
public void onResume() {
super.onResume();
getActivity().setProgressBarIndeterminate(true);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(parseConstants.CLASS_MESSAGES);
query.whereEqualTo(parseConstants.KEY_RECIPIENT_IDS, ParseUser.getCurrentUser().getObjectId());
query.addDescendingOrder(parseConstants.KEY_CREATED_AT);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> messages, ParseException e) {
getActivity().setProgressBarIndeterminate(false);
if(e == null) {
//we found messages!
mMessages = messages;
String[] usernames = new String[mMessages.size()];
int i = 0;
for (ParseObject message : mMessages) {
usernames[i] = message.getString(parseConstants.KEY_SENDER_NAME);
i++;
}
// check list
messageAdapter adapter = new messageAdapter(
getListView().getContext(),
mMessages);
setListAdapter(adapter);
}
}
});
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ParseObject message = mMessages.get(position);
String messageType = message.getString(parseConstants.KEY_FILE_TYPE);
String senderName = message.getString(parseConstants.KEY_SENDER_NAME);
String displayMessage = message.getString(parseConstants.KEY_MESSAGE);
String createdDate = message.getString(parseConstants.KEY_CREATED_AT);
ParseFile file = message.getParseFile(parseConstants.KEY_FILE);
if (messageType.equals(parseConstants.TYPE_IMAGE)) {
//view the image
Uri fileUri = Uri.parse(file.getUrl());
Intent intent = new Intent(getActivity(), ViewImageActivity.class);
intent.setData(fileUri);
startActivity(intent);
}else if (messageType.equals(parseConstants.TYPE_VIDEO)) {
Uri fileUri = Uri.parse(file.getUrl());
//view the video
Intent intent = new Intent(Intent.ACTION_VIEW, fileUri);
intent.setDataAndType(fileUri, "video/*");
startActivity(intent);
}
else if (messageType.equals(parseConstants.TYPE_QUESTIONARE)) {
Intent intent = new Intent();
startActivity(intent);
}else if (messageType.equals(parseConstants.TYPE_TEXT)){
//Toast.makeText(getActivity(), displayMessage, Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Message from: " + senderName + ".");
builder.setMessage(displayMessage);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
//delete the message
List<String> ids = message.getList(parseConstants.KEY_RECIPIENT_IDS);
if(ids.size() == 1) {
//last recipient, delete the message
message.deleteInBackground();
}else {
//remove recipients name
ids.remove(ParseUser.getCurrentUser().getObjectId());
ArrayList<String> idsToRemove = new ArrayList<String>();
idsToRemove.add(ParseUser.getCurrentUser().getObjectId());
message.removeAll(parseConstants.KEY_RECIPIENT_IDS, idsToRemove);
message.saveInBackground();
}
}
}
记录猫
09-28 17:26:44.645 593-593/com.abdul.uniapp D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
09-28 17:26:44.645 593-593/com.abdul.uniapp D/AbsListView﹕ unregisterIRListener() is called
09-28 17:26:44.645 593-593/com.abdul.uniapp D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
09-28 17:26:44.645 593-593/com.abdul.uniapp D/AbsListView﹕ unregisterIRListener() is called
09-28 17:26:53.595 593-2102/com.abdul.uniapp I/System.out﹕ ParseRequest.NETWORK_EXECUTOR-thread-5 calls detatch()
09-28 17:26:53.615 593-593/com.abdul.uniapp D/skia﹕ GFXPNG PNG bitmap created width:90 height:90 bitmap id is 288
09-28 17:26:53.695 593-593/com.abdul.uniapp I/PersonaManager﹕ getPersonaService() name persona_policy
09-28 17:26:53.705 593-593/com.abdul.uniapp D/skia﹕ GFXPNG PNG bitmap created width:48 height:48 bitmap id is 289
09-28 17:26:53.715 593-593/com.abdul.uniapp E/MoreInfoHPW_ViewGroup﹕ Parent view is not a TextView
09-28 17:26:53.715 593-593/com.abdul.uniapp D/skia﹕ GFXPNG PNG bitmap created width:72 height:72 bitmap id is 290
09-28 17:26:53.725 593-593/com.abdul.uniapp D/skia﹕ GFXPNG PNG bitmap created width:144 height:144 bitmap id is 291
09-28 17:26:53.725 593-593/com.abdul.uniapp I/PersonaManager﹕ getPersonaService() name persona_policy
09-28 17:26:53.735 593-593/com.abdul.uniapp D/AbsListView﹕ Get MotionRecognitionManager
09-28 17:26:53.745 593-593/com.abdul.uniapp D/AbsListView﹕ onVisibilityChanged() is called, visibility : 8
09-28 17:26:53.745 593-593/com.abdul.uniapp D/AbsListView﹕ unregisterIRListener() is called
09-28 17:26:53.745 593-593/com.abdul.uniapp D/skia﹕ GFXPNG PNG bitmap created width:72 height:72 bitmap id is 292
09-28 17:26:53.745 593-593/com.abdul.uniapp D/AndroidRuntime﹕ Shutting down VM
09-28 17:26:53.745 593-593/com.abdul.uniapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x417e0da0)
09-28 17:26:53.745 593-593/com.abdul.uniapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.abdul.uniapp, PID: 593
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abdul.uniapp/com.abdul.uniapp.recipientsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
at android.app.ActivityThread.access$900(ActivityThread.java:173)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5579)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.abdul.uniapp.recipientsActivity.onCreate(recipientsActivity.java:58)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
at android.app.ActivityThread.access$900(ActivityThread.java:173)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5579)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
09-28 17:26:55.505 593-593/com.abdul.uniapp I/Process﹕ Sending signal. PID: 593 SIG: 9
09-28 17:26:55.625 2130-2130/com.abdul.uniapp I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
09-28 17:26:55.645 2130-2130/com.abdul.uniapp I/SELinux﹕ Function: selinux_android_load_priority , spota verifySig and checkHash pass. priority version is VE=SEPF_SM-G900T_4.4.2_0034
09-28 17:26:55.645 2130-2130/com.abdul.uniapp I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
09-28 17:26:55.645 2130-2130/com.abdul.uniapp E/dalvikvm﹕ >>>>> Normal User
09-28 17:26:55.645 2130-2130/com.abdul.uniapp E/dalvikvm﹕ >>>>> com.abdul.uniapp [ userId:0 | appId:10242 ]