【发布时间】:2018-07-06 03:53:31
【问题描述】:
我制作了这个带有两个按钮和一个 textview 的小型 android 应用程序,非常基本。但它不是在模拟器中启动的。没有任何构建错误。
这是 activity_main.xml 文件
<?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 文件
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 的所有实例。但是,在这个版本中没有任何错误,即使在模拟器中应用程序也没有启动一次。
【问题讨论】:
-
要检查由于您的应用程序崩溃导致的原因,您可以参考将 Logcat 检查到您的 IDE 中。
标签: java android android-layout android-studio