gpt4 book ai didi

android studio ..代码没有错误但有异常

转载 作者:行者123 更新时间:2023-11-30 01:26:41 24 4
gpt4 key购买 nike

我在 Android Studio 中编写代码,代码中没有错误,但是当我运行它时它不起作用,并在我运行时显示“致命异常”错误。这是我的代码:

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

list

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

最佳答案

onCreate() 中调用 setContentView(...) 之前,您不能使用 findViewById

首先只声明你的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);
}

关于android studio ..代码没有错误但有异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36342228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com