【问题标题】:App got crashed when tried to open a generator尝试打开生成器时应用程序崩溃
【发布时间】:2018-07-13 07:25:57
【问题描述】:

我尝试在 1 个应用程序中构建扫描仪和生成器。当我按下发电机按钮时,它突然崩溃了。我的日志中没有任何错误或警告。

这是我的生成器代码:

public class GeneratorActivity extends AppCompatActivity {

EditText text;
Button gen_btn;
ImageView image;
String text2Qr;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_generator);
    text = findViewById(R.id.text);
    gen_btn = findViewById(R.id.gen_btn);
    image = findViewById(R.id.image);
    gen_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            text2Qr = text.getText().toString().trim();
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            try{
                BitMatrix bitMatrix = multiFormatWriter.encode(text2Qr, BarcodeFormat.QR_CODE,200,200);
                BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
                image.setImageBitmap(bitmap);
            }
            catch (WriterException e){
                e.printStackTrace();
            }
        }
    });
}}

MainActivity 代码:

public class MainActivity extends AppCompatActivity {

Button gen, scan;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    gen = findViewById(R.id.gen);
    scan = findViewById(R.id.scan);
    gen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent gIntent = new Intent(MainActivity.this, GeneratorActivity.class);
            startActivity(gIntent);
        }
    });
    scan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent rIntent = new Intent(MainActivity.this, ReaderActivity.class);
            startActivity(rIntent);
        }
    });
}}

有人知道如何解决这个问题吗?请帮帮我。

更新

这是生成器的 xml 代码:

<EditText
    android:id="@+id/text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="415dp"
    android:layout_marginTop="50dp"
    android:hint="@string/enter_text_to_generate"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/view" />

<Button
    android:id="@+id/gen_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="33dp"
    android:text="@string/generate"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/text" />

<view
    android:id="@+id/view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="48dp"
    android:layout_marginTop="155dp"
    android:background="@android:color/black"
    app:layout_constraintBottom_toTopOf="@+id/text"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="0dp"
    android:layout_height="293dp"
    android:layout_below="@+id/view"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="188dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="348dp"
        android:layout_height="match_parent"
        android:contentDescription="@string/todo" />
</LinearLayout>

我希望这对我的问题有帮助:(

更新:这是由我的愚蠢错字修复的;)。非常感谢到目前为止所有回答我问题的人。我不能说我有多感激。特别是对于我已经投票支持正确答案的人。你真是个英雄!

【问题讨论】:

  • 从 logcat 中删除所有过滤器并再次检查。如果您的应用程序崩溃,则某处会出现日志...
  • @EduardoHerzer 如何从 logcat 中删除过滤器?是干净的项目吗?对不起,愚蠢的问题,我是新手。
  • @RicardoF.Seikka 发布您的错误..
  • @EduardoHerzer 就是这样。在我的日志中,我没有任何错误和警告,但是当我从 apk 完成安装后,当我想打开生成器应用程序部分时它就会崩溃。
  • @RicardoF.Seikka 好的...确实测试了其他一些手机...尝试创建构建 apk 运行它 oncd 让我知道

标签: java android


【解决方案1】:

好的。我有你的问题! (如果不是问题中的错字...)

在您的 xml 中,您声明了一个名为 view 的组件,正确的是 View 并带有大写:

<View
    android:id="@+id/view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="48dp"
    android:layout_marginTop="155dp"
    android:background="@android:color/black"
    app:layout_constraintBottom_toTopOf="@+id/text"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

如果它解决了你的问题,请告诉我

【讨论】:

  • 天哪,它有效!哈哈哈哈,我真的没注意。我仍然无法相信这是一个错字问题。非常感谢楼主,你帮了我很多!我不能说我有多感激。我还有两个与此案有关的问题。你能帮我解决吗?我真的需要尽快完成这项工作。再次,非常感谢您,先生! ;)
【解决方案2】:

请不要投票。我知道这不是解决方案,但这肯定会帮助我们确定问题。

这是关于在 logcat 中查找错误。可能有一次 Ricardo F. Seikka 分享了 logcat,我可以删除这个答案。

在 Windows (10) 上,这就是我在 logcat 没有捕获错误时所做的事情(实际发生的情况是 logcat 在出现错误时被清除(仅针对某些错误)并且生成了新的 logcat,因此我们不能查看 logcat 中的错误)

@Ricardo F. Seikka(我假设你也在使用 Windows)

  • 启动您的应用
  • 在点击按钮之前在终端中运行下面的命令

    "C:\Users\qwerty\AppData\Local\Android\sdk\platform-tools\adb.exe" logcat -c

(而不是 qwerty 添加您的姓名/查找 adb.exe)这将清除 logcat。 - 点击按钮后,运行下面的命令

"C:\Users\Dell\qwerty\Local\Android\sdk\platform-tools\adb.exe" logcat -d > "C:\Users\Dell\qwerty\log.txt"
  • 然后打开 log.txt,搜索 fatal,接下来的几行应该让您知道问题出在哪里。

如果您使用任何其他操作系统,请搜索 adb.exe 并尝试我的建议。

【讨论】:

  • 我找不到 adb.exe,并且在本地,我没有 android 文件夹
  • 试试这个,在 android studio 中,tools->android-> SDK Manager。 (左侧)外观和行为->系统设置-> Android SDK。选择 Android SDK,在右上角的“Android SDK Location”,在这里你可以找到 adb.exe stackoverflow.com/questions/35854238/…
  • 我正在使用 Windows 7。我找不到 sdk 文件夹,但有一个 android 文件夹
  • 非常感谢。我刚刚找到了adb.exe。现在我该怎么办?
  • logcat -d > *.txt (不要错过 > ,因为 > 是将 logcat 重定向到文本文件的那个)。
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-15
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
相关资源
最近更新 更多