有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java应用程序显示空白屏幕,但预览效果良好

所以我试着编写一个保存相对高质量图像的应用程序,但当我在模拟器或手机中启动应用程序时,它只显示一个空白屏幕。在预览中,它看起来应该是这样的

我对Java几乎没有经验。不多。我尝试过一些事情,比如改变布局或显示什么,但没有任何效果

这是我的AndroidManifest.xml

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

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

    <uses-permission
        安卓:name="安卓.permission.WRITE_EXTERNAL_STORAGE"
        安卓:maxSdkVersion="18" />

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

        <activity 安卓:name=".DisplayImage">
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 安卓:name=".MainActivity">
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <provider
            安卓:authorities="com.example.安卓.fileprovider"
            安卓:name="安卓x.core.content.FileProvider"
            安卓:exported="false"
            安卓:grantUriPermissions="true">
             <meta-data
                 安卓:name="安卓.support.FILE_PROVIDER_PATHS"
                 安卓:resource="@xml/file_path" />
        </provider>
    </application>
</manifest>

这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        安卓:layout_width="250dp"
        安卓:layout_height="wrap_content"
        安卓:text="@string/capture_image"
        安卓:onClick="captureImage"
        安卓:layout_gravity="center_horizontal"/>

    <Button
        安卓:layout_width="250dp"
        安卓:layout_height="wrap_content"
        安卓:text="@string/display_image"
        安卓:onClick="displayImage"
        安卓:layout_gravity="center_horizontal"/>
</LinearLayout>

这是activity_main源代码:

public class MainActivity extends AppCompatActivity {

    String currentImagePath = null;
    private static final int IMAGE_REQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void captureImage(View view) {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if(cameraIntent.resolveActivity(getPackageManager()) != null){
            File imageFile = null;

            try {
                imageFile = getImageFile();
            }
            catch (IOException e) {
                e.printStackTrace();
            }

            if(imageFile != null) {
                Uri imageUri = FileProvider.getUriForFile(this, "com.example.安卓.fileprovider", imageFile);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(cameraIntent, IMAGE_REQUEST);

            }
        }
    }

    public void displayImage(View view) {
       Intent intent = new Intent(this, DisplayImage.class);
       intent.putExtra("image_path", currentImagePath);
       startActivity(intent);
    }

    private File getImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
        String imageName = "jps_" + timeStamp + "_";
        File storageDir  = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

        File imageFile = File.createTempFile(imageName, ".jpg", storageDir);
        currentImagePath = imageFile.getAbsolutePath();
        return imageFile;
    }
}

这是activity_display_image.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".DisplayImage">

    <ImageView
        安卓:contentDescription="@string/bild_ansicht"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:padding="4dp"
        安卓:id="@+id/mimageView"/>
</RelativeLayout>

最后但并非最不重要的是,它的源代码:

public class DisplayImage extends AppCompatActivity {

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_image);
        imageView = findViewById(R.id.mimageView);

        Bitmap bitmap = BitmapFactory.decodeFile(getIntent().getStringExtra("image_path"));
        imageView.setImageBitmap(bitmap);
    }
}

没有错误消息。我想让那些按钮显示出来。其余的应该可以正常工作


共 (2) 个答案

  1. # 1 楼答案

    我对安卓也不是很有经验,但你有没有尝试过删除清单中DisplayImage的意图过滤器? 您有两个定义了此功能的活动:

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
    

    这告诉Android,它可以从它们中的任何一个启动应用程序,而且由于DisplayImage是在清单中首先声明的,所以您看到的是一个空白屏幕,因为尚未从MainActivity加载任何图像

    检查一下,以便更好地理解意图和意图过滤器

    https://developer.android.com/guide/components/intents-filters

  2. # 2 楼答案

    多个主要入口点似乎让你在打开应用程序时感到困惑

    确保从正确的启动器图标打开应用程序

    你应该会在你的启动器中看到同一个应用的两个图标。您正在打开的DisplayImage活动可能有一个空白布局。因此,空白屏幕。

    我指的是清单文件中的这些

    <activity android:name=".DisplayImage">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>