0

我複製了以下代碼: https://developer.android.com/training/camera/photobasics.html這段代碼在android studio中使用camera有什麼不對?

我還在清單文件中添加了權限。但是該應用一旦啓動就會崩潰。這裏有什麼問題?

MainActivity.java

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

public class MainActivity extends AppCompatActivity 
{ 
static final int REQUEST_IMAGE_CAPTURE = 1; 
ImageView mImageView=(ImageView) findViewById(R.id.img); 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button b1=(Button) findViewById(R.id.imgButton); 


    // 
    View.OnClickListener imgButtonClickListener=new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     { 
      dispatchTakePictureIntent(); 
     } 
    }; 
    // 

    b1.setOnClickListener(imgButtonClickListener); 

} 

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"); 
     mImageView.setImageBitmap(imageBitmap); 
    } 
} 

} 

MainActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
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" 
tools:context="com.example.nirvan.cameraexample.MainActivity"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/img" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="177dp" /> 

<Button 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="IMG" 
    android:id="@+id/imgButton" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="76dp" /> 


</RelativeLayout> 
+1

什麼錯誤信息(s)你在你的日誌? – Isaac

+1

如果它崩潰了,會有什麼異常呢?始終在問題中包含錯誤消息和例外情況。 –

+0

有投票結束這個問題。請編輯問題並提供更多關於您所得到異常的數據,並解釋您如何儘快編譯代碼。 – clearlight

回答

0

ImageView mImageView=(ImageView) findViewById(R.id.img); 

應該是你onCreate()裏面你setContent後查看

setContentView(R.layout.activity_main); 
ImageView mImageView=(ImageView) findViewById(R.id.img); 

該行將被分配給mImageViewfindViewById(R.id.img)這就需要你在setContentView(R.layout.activity_main)充氣佈局後運行的結果。

+1

如果你添加整行ImageView mImageView =(ImageView)findViewById(R.id.img);在oncreate()中,那麼mImageView將無法在onResultActivity()中訪問。 –

1

只需更換線

ImageView的mImageView =(ImageView的)findViewById(R.id.img);

ImageView的mImageView;

onCreate()添加下面線

mImageView =(ImageView的)findViewById(R.id.img);