gpt4 book ai didi

java - 崩溃可能是由于错误的 Activity 布局

转载 作者:行者123 更新时间:2023-11-30 00:12:10 25 4
gpt4 key购买 nike

我一直在开发一个应用程序,它一直运行良好,直到我在该 Activity 的相应 Java 文件中添加了一个 LinearLayout (id = 'toolbar') 和一些函数。这是我的代码:

AndroidManifest.xml

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".EditPhotoActivity"
android:parentActivityName=".MainActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>

</manifest>

MainActivty 运行良好,我认为问题不在其中。如果您想让我添加它的代码,请告诉我。

EditPhotoActivity.java

package com.example.com.myapplication;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.io.IOException;

public class EditPhotoActivity extends AppCompatActivity {

public ImageView image_view = findViewById(R.id.image_display) ;
public boolean toolbar_is_open = false ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_photo);
ImageView imageView = findViewById(R.id.image_display);
Intent intent = getIntent();
Uri content;
if(intent.getStringExtra(MainActivity.DATA_TYPE).equals("Uri")) {
Bundle bundle = intent.getExtras();
if(bundle != null) {
content = (Uri) bundle.get(MainActivity.IMAGE_KEY);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), content);
imageView.setImageBitmap(bitmap);
} catch(IOException e) {
e.printStackTrace();
Toast.makeText(this, "Failed to fetch image data!", Toast.LENGTH_LONG).show() ;
}
}
} else if(intent.getStringExtra(MainActivity.DATA_TYPE).equals("Bundle")) {
Bundle bundle = intent.getBundleExtra(MainActivity.IMAGE_KEY);
Bitmap bitmap = (Bitmap)bundle.get("data");
imageView.setImageBitmap(bitmap);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.publish_menu, menu);
return true ;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.share_button:
// Save the photo
break;
case R.id.save_button:
// Open Share Photo Activity
break;
}
return super.onOptionsItemSelected(item);
}

private void setToolbarState(boolean open) {
LinearLayout toolbar = findViewById(R.id.toolbar);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)toolbar.getLayoutParams();
params.height = open? 50 : 0;
toolbar.setLayoutParams(params);
toolbar_is_open = open ;
}

public void openRotateToolbar(View view) {
Button right = new Button(this), left = new Button(this);
right.setText(R.string.right); left.setText(R.string.left);
right.setBackgroundResource(R.drawable.custom_button);
left.setBackgroundResource(R.drawable.custom_button);
right.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
image_view.setRotation(90f);
}
});
left.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
image_view.setRotation(-90f);
}
});
if(!toolbar_is_open)
setToolbarState(true);
}

public void closeToolbar(View view) {
if(toolbar_is_open)
setToolbarState(false);
}
}

activity_edit_photo.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.com.myapplication.EditPhotoActivity"
tools:layout_editor_absoluteY="81dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:weightSum="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/image_display"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:background="@color/black"
android:padding="0dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="@+id/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_launcher_background"
tools:ignore="contentDescription" />

<LinearLayout
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#111"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/scrollView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent">

<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/close"
android:background="@drawable/custom_button"
android:textColor="#fff"
android:onClick="closeToolbar" />
</LinearLayout>

<HorizontalScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@color/darkGrey"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/custom_button"
android:text="@string/add_sticker"
android:textColor="#fff"
android:textSize="12sp"
tools:ignore="ButtonStyle" />

<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="-15dp"
android:background="@drawable/custom_button"
android:gravity="center"
android:text="@string/create_sticker"
android:textSize="12sp"
tools:ignore="ButtonStyle" />

<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/custom_button"
android:gravity="center"
android:text="@string/cut_sticker"
android:textSize="12sp"
tools:ignore="ButtonStyle" />

<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/custom_button"
android:gravity="center"
android:text="@string/apply_filter"
android:textSize="12sp"
tools:ignore="ButtonStyle" />

<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/custom_button"
android:gravity="center"
android:text="@string/rotate"
android:textSize="12sp"
tools:ignore="ButtonStyle"
android:onClick="openRotateToolbar" />

<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/custom_button"
android:gravity="center"
android:text="@string/flip"
android:textSize="12sp"
tools:ignore="ButtonStyle" />

<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/custom_button"
android:gravity="center"
android:text="@string/crop"
android:textSize="12sp"
tools:ignore="ButtonStyle" />


</LinearLayout>

</HorizontalScrollView>

</android.support.constraint.ConstraintLayout>

</RelativeLayout>

</android.support.constraint.ConstraintLayout>

我没有收到任何错误,但是当应用程序转换到 EditPhoto Activity 时它崩溃了。如果您需要我为您提供任何其他信息,请告诉我。

注意:我无法发布 logcat,因为我没有使用 ADB 并且我使用的计算机不是我的(这是我家人的)所以我不想启用 x- vt 在 BIOS 中,所以我也无法使用模拟器。为了测试我的应用程序,我构建了 apk,然后将其安装在我的手机上。

最佳答案

我发现了我的错误。对于遇到同样问题的任何人,我都会在这里回答这个问题。

行:

public ImageView image_view = findViewById(R.id.image_display) ;

原因 NullPointerException因为在应用程序打开 Activity 时它还没有所有的 View 和 ID。它仅在调用 onCreate 时获取它们所以我将行替换为

public ImageView image_view ;

内部 onCreatesuper.onCreate(savedInstanceState)之后和 setContentView(R.layout.activity_edit_photo)我添加的电话

image_view = findViewById(R.id.image_display) ;

关于java - 崩溃可能是由于错误的 Activity 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47980127/

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