2017-01-09 194 views
2

我想創建一個攝像機應用程序,我想要一個圖像被捕獲並顯示到另一個Activity將圖像從一個活動傳遞到另一個在android

這裏是代碼 -

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) 
+0

「應用程序被迫停止」當你的應用程序崩潰時,它通常是由未捕獲的異常引起的。爲了幫助您診斷問題,我們需要從logcat進行堆棧跟蹤。 –

+0

請發佈您的logcat。 –

+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); 

    } 
} 

setContentView()之前,您不能調用findViewById()。這確實是一場崩潰。發佈logcat如果是別的東西

+1

非常感謝!有效!! –

相關問題