2016-03-31 35 views
1

私はAndroidスタジオにコードを記述していますが、コードにエラーはありませんが、動作させると動作しませんでしたし、 "致命的な例外"実行します。ここ 私のコード:MainActivity.javaandroid studio ..コードにエラーはありませんが、例外があります

package com.example.hayfa.sharingwithmultimedia; 

import android.content.Intent; 
import android.graphics.Bitmap; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.ImageView; 

import java.io.File; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 
    Uri uri; 
    ImageView mImageView = (ImageView) findViewById(R.id.imageView); 
    static final int REQUEST_IMAGE_CAPTURE = 1; 

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

    } 

    public 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"); 
      mImageView.setImageBitmap(imageBitmap); 
     } 
     File image=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture.jpg"); 
     uri= Uri.fromFile(image); 
     data.putExtra(MediaStore.EXTRA_OUTPUT, uri); 
    } 

    public void sendImage(View view){ 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_SEND); 
     intent.putExtra(Intent.EXTRA_STREAM, "This is my text to send"); 
     intent.putExtra(Intent.EXTRA_STREAM, uri); 

     intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "From my app"); 
     intent.setType("image/jpeg"); 
     startActivity(Intent.createChooser(intent,"Share image to ")); 
    } 
} 

マニフェストあなたがonCreate()setContentView(...)を呼び出し後までfindViewByIdを使用することはできません

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.hayfa.sharingwithmultimedia"> 

    <uses-feature android:name="android.hardware.camera" 
     android:required="true"/> 

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

contentmain.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: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" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.hayfa.sharingwithmultimedia.MainActivity" 
    tools:showIn="@layout/activity_main"> 

    <ImageView 
     android:layout_width="300dip" 
     android:layout_height="300dip" 
     android:id="@+id/imageView" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" TAKE PICTURE" 
     android:onClick="dispatchTakePictureIntent" 
     android:id="@+id/button" 
     android:layout_below="@+id/imageView" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="41dp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="SHARE PICTURE" 
     android:onClick="sendImage" 
     android:id="@+id/button2" 
     android:layout_below="@+id/button" 
     android:layout_alignLeft="@+id/button" 
     android:layout_alignStart="@+id/button" /> 
</RelativeLayout> 

答えて

1

のみごImageView

private ImageView mImageView; 

を宣言してからonCreate()

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mImageView = (ImageView) findViewById(R.id.imageView); 
} 
+0

でそれを割り当てるまず今、プログラムの実行をUに感謝し、私は写真を撮ると、ギャラリーに保存することができますが、問題私は可能性があり他のアプリと写真を共有していない..あなたはどんなアイデアを持っていますか? –

関連する問題