更新时,按钮不会显示在模拟器输出文本:我已经改变了活动代码,以我最好的工作集。我目前的问题是,当点击一个按钮时,App force会在模拟器中关闭。我logcat的说,问题是,在第38行,switch语句,其中updateDate()被调用,并在第64行,用于设置当前日期的文字。安卓:点击
我已经创建了一个有两个按钮,显示您的当前日期和时间的应用,在Android模拟器打开和点击时没有下面的按钮显示为它应该是文本。我在按钮下使用TextView来显示输出。这里是我的main.xml和activity分别的代码。
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:orientation="vertical" >
<Button
android:id="@+id/dayOf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/buttonDay"
/>
<Button
android:id="@+id/clock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/buttonTime"
/>
<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/output"
android:textSize="72dip"
/>
</LinearLayout>
活动
package omaxwell.CS211D.TimeAndDate;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Calendar;
import android.widget.TextView;
/*
* This is the activity for my Android App that gives you the date and time
*/
//*******************TimeAndDateActivity*******************
public class TimeAndDateActivity extends Activity implements View.OnClickListener
{
Button dayOf;
Button clock;
@Override
//*******************onCreate*******************
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.main);
clock = (Button)this.findViewById(R.id.clock); //Time Button
clock.setOnClickListener(this);
dayOf = (Button)this.findViewById(R.id.dayOf); //Date Button
dayOf.setOnClickListener(this);
}
//*******************onClick*******************
public void onClick(View v) //Called when one of the buttons above is clicked
{
switch(v.getId())
{
case R.id.clock:
updateTime();
break;
case R.id.dayOf:
updateDate();
break;
}
}
//*******************updateTime*******************
private void updateTime()
{
Calendar d = Calendar.getInstance();
d.getTime();
TextView tv = (TextView) findViewById(R.string.output);
tv.setText(d.getTime().toString());
}
//*******************updateDay*******************
private void updateDate()
{
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
String date = String.valueOf(day) + "-" + String.valueOf(month) + "-" +String.valueOf(year);
TextView tv = (TextView) findViewById(R.string.output);
tv.setText(date);
}
}
感谢提前:)
放在try/catch语句查找异常..尝试{tv.setText(日期);}赶上(){}然后告诉.. – sai 2012-02-21 12:45:03