当前位置: 首页 > 知识库问答 >
问题:

应用程序在尝试打开发电机时崩溃

白高超
2023-03-14

我尝试在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();
            }
        }
    });
}}

主活动代码:

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>

我希望这将有助于我的问题:(

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

共有2个答案

端木昱
2023-03-14

这是关于在logcat中查找错误的。也许一旦Ricardo F.Seikka分享日志,我就可以删除这个答案。

在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,搜索致命的,接下来的几行应该会让你知道问题是什么。

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

  • 诸正谊
    2023-03-14

    好吧。我知道你的问题了!(如果不是问题中的错别字...)

    在xml中,您声明了一个名为视图的组件,而正确的组件名为视图,大写:

    <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" />
    

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

     类似资料:
    • 在我的应用程序中,有一个使用相机捕捉图像的工具。在Android6之前的所有版本中,它都能完美地工作,但在Android7.0版本中,它让我的应用程序崩溃了。 添加的权限 权限侦听器 打开相机的代码 崩溃日志 由:android.content.activityNotFoundException引起:未找到处理意图的活动{act=android.media.action.image_capture

    • 主要活动 碎片 这是一个简单对话框的代码。 这里有一个我从中学习的链接。如果我只需通过调用按钮创建对话框,它就可以完全正常工作。

    • 我正在开发一个Android应用程序,它必须从文件中解析文本。 我的解析器中有以下方法。java类: 每当调用缓冲区时,我都会遇到问题。while循环中的readLine()方法。 我传递以下路径信息File对象是: 现在我已经看了堆栈和在线上的许多帖子,以便尝试解决这个问题,并尝试使用一些解决方案,但没有运气。这是我从错误堆栈跟踪中获得的一个片段。 我确信文件的路径是正确的,因为我在调试时检查了

    • 下面给出的错误 process:com.example.rahul.maptask,pid:21986 java.lang.runtimeException:无法启动活动ComponentInfo{com.example.rahul.maptask/com.example.rahul.maptask.mainactivity}:java.lang.nullpointerException:试图在A

    • 当我运行应用程序时,它工作得很好,但是当我想调试一个变量值时,应用程序就崩溃了。下面是完整的调试日志。我不知道是什么错误来纠正它。你们能帮帮我吗?我读到了一些关于并且我禁用和启用了ADB集成,但没有帮助我。我试图清洁,重建和运行的项目再次和没有帮助我。每次我尝试开始调试时,应用程序都会崩溃。 提前谢谢!!

    • 我只有一个窗口,在那里你推菜单->首选项并启动一个意图 它自动地表示必须关闭应用程序。 未捕获的处理程序:由于未捕获的异常导致线程主退出 java.lang.runtimeException:无法启动活动ComponentInfo{com.example.app/com.example.app.PreferenciasGenerales}: java.lang.ClassCastException