在Android应用

时间:2017-04-12 15:05:40

标签: java android android-camera avatar

我想让用户在设置用户信息时创建一个头像(如果您愿意,可以创建个人资料图片)。我创建了一种单击/触摸的方法,可以要求用户拍照,长按一次会要求用户从他们的画廊中选择一张照片。

以下是我的类文件中的方法:

public void onLaunchCamera(View v) {

    avatarButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String strAvatarPrompt = "Take your picture to store as your avatar!";
            Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
        }
    });

    avatarButton.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            String strAvatarPrompt = "Choose a picture to use as your avatar!";
            Intent pickPhoto = new Intent(Intent.ACTION_PICK);
            pickPhoto.setType("image/*");
            startActivityForResult(Intent.createChooser(pickPhoto, strAvatarPrompt), TAKE_AVATAR_GALLERY_REQUEST);
            return true;
        }
    });
    }

以下是与ImageButton关联的XML:

<ImageButton
            android:id="@+id/ImageButton_Avatar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:maxHeight="@dimen/avatar_size"
            android:minHeight="@dimen/avatar_size"
            android:onClick="onLaunchCamera"
            android:scaleType="fitXY"
            android:src="@drawable/avatar"></ImageButton>

当我点击ImageButton并且我不明白为什么时,它所做的只是崩溃。有什么想法吗?

由于

编辑:在下面添加logcat(抱歉格式化。无法解决如何正确排序的问题:

[ 04-12 18:32:50.989  5901: 5901 D/         ]
HostConnection::get() New     Host Connection established 0xb8a44530, tid 5901
04-12 18:32:51.039 5901-5901/cct.mad.lab D/OpenGLRenderer: Enabling debug mode 0
04-12 18:32:55.739 5901-5901/cct.mad.lab V/RenderScript: 0xb8c53300     Launching thread(s), CPUs 2
04-12 18:32:57.389 5901-5901/cct.mad.lab D/AndroidRuntime: Shutting down VM
04-12 18:32:57.389 5901-5901/cct.mad.lab W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb0d2db20)
04-12 18:32:57.399 5901-5901/cct.mad.lab E/AndroidRuntime: FATAL EXCEPTION: main
Process: cct.mad.lab,     PID: 5901                                                        java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
                                                           at   android.view.View.performClick(View.java:4438)
                                                           at  android.view.View$PerformClick.run(View.java:18422)
                                                            at a   android.os.Handler.handleCallback(Handler.java:733)
                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                           at android.os.Looper.loop(Looper.java:136)
                                                           at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                           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:779)
                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                           at dalvik.system.NativeStart.main(Native Method)
                                                        Caused by: java.lang.reflect.InvocationTargetException
                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                           at android.view.View$1.onClick(View.java:3818)
                                                           at android.view.View.performClick(View.java:4438) 
                                                           at android.view.View$PerformClick.run(View.java:18422) 
                                                           at android.os.Handler.handleCallback(Handler.java:733) 
                                                           at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                           at android.os.Looper.loop(Looper.java:136) 
                                                           at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                           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:779) 
                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                           at dalvik.system.NativeStart.main(Native Method) 
                                                        Caused by: java.lang.NullPointerException
                                                           at cct.mad.lab.SettingsActivity.onLaunchCamera(SettingsActivity.java:201)
                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                           at java.lang.reflect.Method.invoke(Method.java:515) 
                                                           at android.view.View$1.onClick(View.java:3818) 
                                                           at android.view.View.performClick(View.java:4438) 
                                                           at android.view.View$PerformClick.run(View.java:18422) 
                                                           at android.os.Handler.handleCallback(Handler.java:733) 
                                                           at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                           at android.os.Looper.loop(Looper.java:136) 
                                                           at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                           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:779) 
                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                           at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:1)

看起来您可能没有定义avatarButton,如果您按照LogCat上的Caused By路径,您会看到底部的是NullPointerException。

由于我看不到行号,因此问题发生在第201行 - 我在代码中看到的唯一明显的空指针是avatarButton。

根据你想做的事情,你会想要对此有所不同。

从XML中删除android:onClick =“onLaunchCamera”。

设置内容视图后,在你的onCreate()中添加以下内容:

register_field

这允许您设置具有更多控件的click和longClick侦听器。你拥有它的方式,在你第一次点击它们之前,你从未真正定义onClick或onLongClick。