我是android studio的新手。现在我正在创建一个基本应用程序,当您单击屏幕上的按钮时递归计数从4减少到0。当我在设计视图中查看我的activity_main.xml时,它会显示我想要的内容,但是当我在我的虚拟设备上运行我的应用程序时,它只显示一个没有任何内容的白色屏幕。我怎样才能解决这个问题?我会在这里粘贴我的代码,几乎所有代码都在activity_main.xml和MainActivity.java中
这是activity_main.xml:
<script src="https://unpkg.com/vue"></script>
<div id="app">
<ul>
<li v-for="eatCategory in eatCategories">
<label>
<input type="checkbox" :value="eatCategory.id" v-model="checkedEatCategories">
<span class="title">{{eatCategory.title}}</span>
</label>
</li>
</ul>
<ul>
<li v-for="post in posts">
{{ post.title }}
</li>
</ul>
</div>
这是我的MainActivity.java
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >``
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/button1Contents"
android:onClick="sendMessage"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="@string/textView1Contents" />
</RelativeLayout>
这是我的strings.xml
public class MainActivity extends Activity {
private static final String TAG = "OneButtonRecursion";
public void sendMessage(View view) {
Button clickedButton = (Button) view;
if (clickedButton == findViewById(R.id.button1)) {
TextView cup = (TextView) findViewById(R.id.textView1);
int handFull = Integer.parseInt(cup.getText().toString());
if (handFull > 0) {
cup.setText(handFull + "");
playNextCup(cup, handFull);
}
} else {
}
}
public static void playNextCup(TextView cupIn, int handFullIn) {
handFullIn--;
cupIn.setText(handFullIn + "");
if (handFullIn != 0) {
playNextCup(cupIn, handFullIn);
Log.i(TAG, "In recursion " + handFullIn);
} else {
}
}
}
答案 0 :(得分:3)
您忘记了Override
onCreate()
方法
onCreate()
活动开始时调用。这是大多数初始化应该去的地方:调用setContentView(int)来扩展活动的UI,
您的活动中只需Override
onCreate()
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}