时间:2017-01-09 18:31:54

标签: android layout

我想创建一个相机应用程序,我希望将图像捕获并显示给另一个Activity

这是代码 -

public class MainActivity extends AppCompatActivity {
    static final int REQUEST_IMAGE_CAPTURE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button photoclick= (Button) findViewById(R.id.clickphoto);
        Button recordgetter= (Button) findViewById(R.id.getrecord);
        Typeface font= Typeface.createFromAsset(getAssets(), "insomnia.ttf");
        photoclick.setTypeface(font);
        recordgetter.setTypeface(font);
        photoclick.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dispatchTakePictureIntent();
            }
        });
    }

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");

            Intent i=new Intent(this,Description.class);
            i.putExtra("BitmapImage", imageBitmap);
            startActivity(i);
        }
    }
}

这是我想要显示图像的第二个类 -

public class Description extends AppCompatActivity {
    ImageView capturedimage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent i=getIntent();
        Bitmap bitmap = (Bitmap) i.getParcelableExtra("BitmapImage");

        capturedimage=(ImageView) findViewById(R.id.capturedImage);
        capturedimage.setImageBitmap(bitmap);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_description);
    }
}

以下是第二项活动的XML代码 -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_description"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.rishabhgambhir.findthelost.Description">

    <ImageView
        android:id="@+id/capturedImage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@color/colorPrimaryDark" />
</RelativeLayout>

单击图片并按勾号后,应用程序将被强制停止。请帮忙。

00:22:33.615 19175-19175/com.example.rishabhgambhir.findthelost E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.example.rishabhgambhir.findthelost, PID: 19175
                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rishabhgambhir.findthelost/com.example.rishabhgambhir.findthelost.Description}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                        at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5422)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
                                                                                        at com.example.rishabhgambhir.findthelost.Description.onCreate(Description.java:24)
                                                                                        at android.app.Activity.performCreate(Activity.java:6251)
                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                        at android.os.Looper.loop(Looper.java:148) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5422) 
                                                                                        at java.lang.reflect.Method.invoke(Native Method)

1 个答案:

答案 0 :(得分:2)

让你成为Description

public class Description extends AppCompatActivity {
    ImageView capturedimage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_description);
        Intent i=getIntent();
        Bitmap bitmap = (Bitmap) i.getParcelableExtra("BitmapImage");
        capturedimage=(ImageView) findViewById(R.id.capturedImage);
        capturedimage.setImageBitmap(bitmap);

    }
}

您无法在findViewById()之前致电setContentView()。那肯定是崩溃。发布logcat,如果它是别的