gpt4 book ai didi

java - ProgressDialog 上的 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 07:50:56 25 4
gpt4 key购买 nike

我在使用进度对话框时收到 NullPointerException。

我的代码如下:

package org.text.me.actionBar.edit.fragments;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.quickblox.core.QBCallback;
import com.quickblox.core.QBCallbackImpl;
import com.quickblox.core.result.Result;
import com.quickblox.module.content.QBContent;
import com.quickblox.module.content.model.QBFile;
import com.quickblox.module.content.result.QBFileUploadTaskResult;
import com.quickblox.module.users.QBUsers;
import com.quickblox.module.users.model.QBUser;
import org.text.me.R;
import org.text.me.actionBar.definitions.Queries;
import org.text.me.actionBar.edit.activity.ChangeNameActivity;
import org.text.me.actionBar.edit.activity.ChangePasswordActivity;
import org.text.me.actionBar.gallery.activity.GalleryActivity;
import org.text.me.actionBar.holder.DataHolder;
import org.text.me.actionBar.utils.bitmap.BitmapUtility;
import org.text.me.actionBar.utils.external_storage.ExternalStorageUtils;

import java.io.File;

import static android.widget.ImageView.ScaleType.FIT_CENTER;
import static org.text.me.actionBar.definitions.Constants.TAG;
import static org.text.me.actionBar.preferences.SharedConstants.*;
import static org.text.me.actionBar.preferences.SharedPrefs.*;

public class EditProfileFragment extends Fragment implements View.OnClickListener, QBCallback {

private View editProfileView;
private ImageView ivProfilePicture;
private TextView tvFullName;
private ImageButton ibPassword;
private ImageButton ibName;

private ProgressDialog progressDialog;

private String fullName = null;

private Bitmap DEFAULT_PROFILE_PICTURE = null;
private Bitmap PROFILE_PICTURE = null;

private static int DEFAULT_WIDTH = 300;
private static int DEFAULT_HEIGHT = 250;
private static final int DEFAULT_COMPRESS_QUALITY = 100;
private static final Bitmap.CompressFormat DEFAULT_COMPRESS_FORMAT = Bitmap.CompressFormat.PNG;
private static final boolean fileIsPublic = false;

public static EditProfileFragment getInstance () {
return new EditProfileFragment ();
}

@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setHasOptionsMenu ( false );
}

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
editProfileView = inflater.inflate ( R.layout.fragment_edit_profile, container, false );

setupUi ();
setupProgressDialog ();

return editProfileView;
}

private void setupUi () {
tvFullName = (TextView) editProfileView.findViewById ( R.id.tvName );
ivProfilePicture = (ImageView) editProfileView.findViewById ( R.id.ivEditProfilePicture );
ibPassword = (ImageButton) editProfileView.findViewById ( R.id.ibPassword );
ibName = (ImageButton) editProfileView.findViewById ( R.id.ibName );

tvFullName.setText ( getStringPrefs ( getActivity (), SIGN_IN_USER_FULL_NAME ) );

if ( getStringPrefs ( getActivity (), PROFILE_PICTURE_PATH ).equals ( null ) )
setupDefaultProfilePicture ();
else
setupProfilePicture ();


ibName.setOnClickListener ( this );
ibPassword.setOnClickListener ( this );
ivProfilePicture.setOnClickListener ( this );
}

private void setupProgressDialog () {
progressDialog = new ProgressDialog ( editProfileView.getContext () );
progressDialog.setMessage ( getResources ().getString ( R.string.loading ) );
progressDialog.setCancelable ( false );
}

private void setupDefaultProfilePicture () {
DEFAULT_PROFILE_PICTURE = BitmapUtility.decodeSampledBitmapFromResource (
getResources (),
R.drawable.action_people,
DEFAULT_WIDTH,
DEFAULT_HEIGHT );
ivProfilePicture.setScaleType ( FIT_CENTER );
ivProfilePicture.setImageBitmap ( DEFAULT_PROFILE_PICTURE );
}

private void setupProfilePicture () {
if ( PROFILE_PICTURE != null ) PROFILE_PICTURE.recycle ();

BitmapFactory.Options options = new BitmapFactory.Options ();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inPurgeable = true;
options.inInputShareable = true;
options.inDither = false;
options.inSampleSize = 8;
PROFILE_PICTURE = BitmapFactory.decodeFile ( getStringPrefs ( getActivity (), PROFILE_PICTURE_PATH ),
options );
ivProfilePicture.setImageBitmap ( PROFILE_PICTURE );

if ( !getBooleanPrefs ( getActivity (), IS_PROFILE_UPLOADED ) && !getStringPrefs ( getActivity (),
PROFILE_PICTURE_PATH )
.equals ( null ) ) {

progressDialog.show ();

File profilePicture = ExternalStorageUtils.saveBitmapToFile ( PROFILE_PICTURE, DEFAULT_COMPRESS_FORMAT,
DEFAULT_COMPRESS_QUALITY,
String.valueOf ( getIntPrefs ( getActivity
(),
SIGN_IN_USER_ID ) ) );
QBContent.uploadFileTask ( profilePicture, fileIsPublic, new QBCallbackImpl () {
@Override
public void onComplete (Result result) {
if ( result.isSuccess () ) {
//Get the uploaded file
QBFileUploadTaskResult taskResult = (QBFileUploadTaskResult) result;
QBFile file = taskResult.getFile ();
int uploadedFileId = file.getId ();

//Connect image to user
QBUser user = new QBUser ();
user.setId ( getIntPrefs ( getActivity (), SIGN_IN_USER_ID ) );
user.setFileId ( uploadedFileId );

QBUsers.updateUser ( user, new QBCallbackImpl () {
@Override
public void onComplete (Result result) {
if ( result.isSuccess () ) {
Toast.makeText ( getActivity (), getResources ().getString ( R.string
.action_edit_successful ), Toast.LENGTH_SHORT ).show ();
progressDialog.dismiss ();
setBooleanPrefs ( getActivity (), IS_PROFILE_UPLOADED, true );
} else {
progressDialog.hide ();
Toast.makeText ( getActivity (), result.getErrors ().get ( 0 ).toString (),
Toast.LENGTH_SHORT ).show ();
Log.e ( TAG, result.getErrors ().toString () );
}
}
} );
} else {
progressDialog.hide ();
Toast.makeText ( getActivity (), result.getErrors ().get ( 0 ).toString (),
Toast.LENGTH_SHORT ).show ();
Log.e ( TAG, result.getErrors ().toString () );
}
}
} );
}
}

@Override
public void onClick (View v) {
switch ( v.getId () ) {
case R.id.ibPassword:
startActivity ( new Intent ( this.getActivity (), ChangePasswordActivity.class ) );
break;

case R.id.ibName:
startActivity ( new Intent ( this.getActivity (), ChangeNameActivity.class ) );
break;

case R.id.ivEditProfilePicture:
Intent intent = new Intent ( getActivity (), GalleryActivity.class );
startActivity ( intent );
break;
}
}

@Override
public void onComplete (Result result) {

}

@Override
public void onComplete (Result result, Object query) {
Queries queryType = (Queries) query;
if ( result.isSuccess () ) {
switch ( queryType ) {
case QUERY_UPDATE_USER_INFO:
DataHolder.getDataHolder ().getSignInUser ().setFullName ( fullName );
progressDialog.dismiss ();
Toast.makeText ( this.getActivity (), getResources ().getString ( R.string
.action_edit_successful ),

Toast.LENGTH_SHORT ).show ();
break;
}
} else {
progressDialog.hide ();
Toast.makeText ( this.getActivity (), result.getErrors ().get ( 0 ).toString (),
Toast.LENGTH_SHORT ).show ();
Log.e ( TAG, result.getErrors ().toString () );
}
}
}

我得到的错误如下:

01-23 07:14:55.524    3126-3126/org.text.me W/dalvikvm﹕ threadid=1: thread exiting     with uncaught exception (group=0x4001d560)
01-23 07:14:55.574 3126-3126/org.text.me E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at org.text.me.actionBar.edit.fragments.EditProfileFragment.setupProfilePicture(EditProfileFragment.java:134)
at org.text.me.actionBar.edit.fragments.EditProfileFragment.setupUi(EditProfileFragment.java:93)
at org.text.me.actionBar.edit.fragments.EditProfileFragment.onCreateView(EditProfileFragment.java:76)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8313)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8313)
at android.view.ViewRoot.performTraversals(ViewRoot.java:843)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1864)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3698)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(Native Method)

如果我注释掉 progressDialog.show() ,程序运行起来就像一个魅力错误发生在 progressDialog.show();那里怎么会出现 NullPointerException?解决方案是什么?

最佳答案

setupProfilePicture(),它访问 progressDialog 实例并且仍然是 null,最终在 setupProgressDialog()< 之前被调用。逐步完成您在 onCreateView() 中进行的 setupUi()setupProgressDialog() 调用。

关于java - ProgressDialog 上的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21297782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com