Reputation: 35
I want to create a camera application where I want an image to be captured and displayed to another Activity
.
Here is the code-
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);
}
}
}
Here is the second class where I want the image to be displayed-
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);
}
}
Here is the XML code of second activity-
<?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>
The application is forced stop as soon as I click the picture and press the tick mark. Please help.
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)
Upvotes: 2
Views: 264
Reputation: 625
Make you Description
class like
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);
}
}
You can't call findViewById()
before setContentView()
. That's a crash for sure. Post logcat if it's something else
Upvotes: 2