Android应用程序在AVD中不断崩溃,但在Gradle中正确构建 [英] Android app keeps crashing in AVD, but is built properly in Gradle

查看:104
本文介绍了Android应用程序在AVD中不断崩溃,但在Gradle中正确构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用两个按钮和一个textview制作了这个小型Android应用程序,非常基本。但它不是从模拟器开始的。没有任何构建错误。

I made this small android app with two buttons and a textview, pretty basic. But its not starting in the emulator. No build errors whatsoever.

这是activity_main.xml文件

Here's the activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
android:orientation="vertical">

<Button
    android:id="@+id/toast_button"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    android:text="@string/toast_text"
    android:background="@color/colorPrimary"
    android:textColor="@color/white"
    android:onClick="showToast"/>

<TextView
    android:id="@+id/text_view_counter"
    android:layout_width="match_parent"
    android:layout_height="370dp"
    android:background="@color/yellow"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:text="@string/counter_initial_value"
    android:textSize="160sp"
    android:gravity="center"
    android:textStyle="bold"
    android:textColor="@color/colorPrimary"
    />


<Button
    android:id="@+id/count_button"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:text="@string/count"
    android:background="@color/colorPrimary"
    android:textColor="@color/white"
    android:onClick="counterUp"/>
    </LinearLayout>

这是MainActivity.java文件

Here's the MainActivity.java file

package com.example.android.testapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

final TextView mShowCounter = (TextView) findViewById(R.id.text_view_counter);
final Button button = (Button) findViewById(R.id.toast_button);
private int mCount = 0;

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


public void showToast(View view) {
    Toast toast = Toast.makeText(this, 
R.string.toast_popup,Toast.LENGTH_LONG);
    toast.show();
}

public void counterUp(View view) {
    mCount++;
    if(mShowCounter != null) {
        mShowCounter.setText(Integer.toString(mCount));
    }
}
}

我试过制作多个项目,但这不起作用。在第一个项目中,它在第一个构建之后开始显示错误,说无法解析符号R,突出显示R为红色的所有实例。但是,在这个版本中,应用程序在模拟器中甚至没有启动过任何错误。

I've tried making multiple projects, but this is not working. In the first project, it started showing an error after the first build saying "cannot resolve symbol R", highlighting all instances of R in red. However, in this build there have been no errors still the app has not started even once in the emulator.

推荐答案

你正在尝试在创建之前访问 ** xml ** 实例会导致应用程序崩溃。

You are trying to access **xml** instances before it get's created that's makes app causing to crashed.

要检查由于您的应用程序崩溃导致您可以参考检查 Logcat 到您的IDE。

To check the caused due to your app crashed you may refer to check Logcat into your IDE.

将您的实例化代码放在 onCreate()回调中: -

Put Your instantiation code inside onCreate() callback:-

package com.example.android.testapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

final TextView mShowCounter;
final Button button;
private int mCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mShowCounter = (TextView) findViewById(R.id.text_view_counter);
    button = (Button) findViewById(R.id.toast_button);
}


public void showToast(View view) {
    Toast toast = Toast.makeText(this, 
R.string.toast_popup,Toast.LENGTH_LONG);
    toast.show();
}

public void counterUp(View view) {
    mCount++;
    if(mShowCounter != null) {
        mShowCounter.setText(Integer.toString(mCount));
    }
}
}

这篇关于Android应用程序在AVD中不断崩溃,但在Gradle中正确构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆