2016-03-31 9 views
1

Ich schreibe Code in Android Studio es ist kein Fehler im Code, aber wenn ich laufe habe es nicht funktioniert und zeigen mir "FATALE AUSNAHME" Fehler, wenn ich mache Lauf. hier mein Code:Android Studio .. Es gibt keinen Fehler im Code, aber es gibt Ausnahme

In MainActivity.java

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

Manifest

<?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> 

Antwort

1

Sie nicht findViewById erst, nachdem Sie setContentView(...) rufen in onCreate() verwenden können.

Zuerst nur erklären, um Ihre ImageView

private ImageView mImageView; 

und ordnen sie in onCreate()

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

u danken, das Programm laufen und ich kann Foto nehmen und es auf der Galerie speichern, aber das Problem jetzt kann ich nicht teilen Photo mit einer anderen App .. hast du eine Idee ?? –

Verwandte Themen