Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an imageview in this app, I am able to capture an image on clicking the imageview, but when I want to display that image on the imageview after capturing, the app is getting crashed. Tried some codes from internet, but the problem still persists. What is the problem? I cannot understand simply what is causing this.. :(


My code is below:


Java
package com.example.newfragment;
 
import java.io.File;

import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
 
public class TextFragment extends Fragment implements OnClickListener {
    TextView text,add;
    private ImageView img;
    Button b;
    int value;
    String mobile;
    private static final int CAMERA_REQUEST = 1888; 
    Camera camera;
  
    @Override
 
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
 
        View view = inflater.inflate(R.layout.text_fragment, container, false);
       
        add= (TextView)view.findViewById(R.id.address);
        img= (ImageView)view.findViewById(R.id.img);
        b= (Button)view.findViewById(R.id.b);
        
        img.setOnClickListener(this);
        b.setOnClickListener(this);
        
       
    
        return view;
 
    }
    
    ////////////data received here////////////
    public void change(int txt, String txt1, String txt2 ){
    	
        
        add.setText(txt1 );
        value = txt;
        mobile = txt2;
 
    }
   
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.equals(img))
		{
			/*Clicking and saving image to sd card*/
			//////////////////////////////////////
            File file = new File(Environment.getExternalStorageDirectory(), "Frag_list");
            file.mkdirs();
            
            String path = Environment.getExternalStorageDirectory() +"/Frag_list/"+value+".jpg";
            File file2= new File(path);
            Uri outputFileUri = Uri.fromFile( file2 );
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
            intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );

            startActivityForResult( intent, CAMERA_REQUEST );
            /////////////////////////////////////
            
            
            
		}
		  
		if(v.equals(b))
		{
			/*Calling number*/
			////////////////////////////////////////////////
			Uri number = Uri.parse("tel:" +mobile);
			Intent callIntent = new Intent(Intent.ACTION_CALL, number);
			startActivity(callIntent);
			////////////////////////////////////////////////
		
		}
		
	}
	
	///setting image in display
	///////////////////////////////////////////////////////////
	 public void onActivityResult(int requestCode, int resultCode, Intent data) {  
	        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) 
	        {  
	            Bitmap photo = (Bitmap) data.getExtras().get("data"); ////////////////////////PROBLEM: APP GETTING CRASHED HERE
	            img.setImageBitmap(photo);
	        	
	        }
	      
		 
		
	 	}
	 ////////////////////*/

}


And the error I am getting:

Java
10-01 13:34:18.850: W/dalvikvm(7121): threadid=1: thread exiting with uncaught exception (group=0x416159a8)
10-01 13:34:18.873: E/AndroidRuntime(7121): FATAL EXCEPTION: main
10-01 13:34:18.873: E/AndroidRuntime(7121): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=198496, result=-1, data=null} to activity {com.example.newfragment/com.example.newfragment.MainActivity}: java.lang.NullPointerException
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:3525)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at android.app.ActivityThread.handleSendResult(ActivityThread.java:3568)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at android.app.ActivityThread.access$1100(ActivityThread.java:162)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at android.os.Handler.dispatchMessage(Handler.java:107)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at android.os.Looper.loop(Looper.java:194)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at android.app.ActivityThread.main(ActivityThread.java:5371)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at java.lang.reflect.Method.invokeNative(Native Method)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at java.lang.reflect.Method.invoke(Method.java:525)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at dalvik.system.NativeStart.main(Native Method)
10-01 13:34:18.873: E/AndroidRuntime(7121): Caused by: java.lang.NullPointerException
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at com.example.newfragment.TextFragment.onActivityResult(TextFragment.java:118)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:153)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at android.app.Activity.dispatchActivityResult(Activity.java:5311)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:3521)
10-01 13:34:18.873: E/AndroidRuntime(7121): 	... 11 more
10-01 13:34:20.822: I/Process(7121): Sending signal. PID: 7121 SIG: 9
Posted
Updated 16-Apr-20 22:27pm
v3
Comments
Richard MacCutchan 1-Oct-15 4:34am    
java.lang.NullPointerException means that you are using a variable somewhere that has not been initialised. You will need to use your debugger to find out exactly where the problem occurs.
Srimoyee Sarkar 1-Oct-15 5:34am    
I know where is the error. I have marked that in my code. But I don't know what is causing this.. I cannot find any error there
Richard MacCutchan 1-Oct-15 6:55am    
Then it is fair to assume that either variable img or variable photo are the offending items. So you need to trace through your code to find out why.
Srimoyee Sarkar 1-Oct-15 6:59am    
I have solved it. :) Just changed the code to another (Added my solution below). Now it's working fine. Thanks for commenting.

1 solution

previous one:

C#
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK)
           {
               Bitmap photo = (Bitmap) data.getExtras().get("data"); ////////////////////////PROBLEM: APP GETTING CRASHED HERE
               img.setImageBitmap(photo);

           }



current one:

SQL
File image = new  File(Environment.getExternalStorageDirectory() +"/Frag_list/"+value+".jpg");
                if(image.exists()){

                         img.setImageBitmap(BitmapFactory.decodeFile(image.getAbsolutePath()));/////////////setting image in imageview
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900