2017-01-09 4 views
2

イメージをキャプチャして別のActivityに表示したいカメラアプリケーションを作成したいとします。ここでイメージを別のアクティビティからアンドロイドに渡す

は、ここで私はイメージが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); 
    } 
} 

になりたい第二のクラスは、ここで

<?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> 
アクティビティ - 第二のXMLコードですコード -

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

です

画像をクリックして目盛りを押すと、アプリケーションが強制停止されます。助けてください。

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

はあなたがsetContentView()findViewById()を呼び出すことはできません

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

    } 
} 

好きDescriptionクラスを作成します。それは確かにクラッシュです。もし何か他のものがあればlogcatを投稿してください。

+1

ありがとうございます!出来た!! –

関連する問題