2012-02-21 90 views
0

更新时,按钮不会显示在模拟器输出文本:我已经改变了活动代码,以我最好的工作集。我目前的问题是,当点击一个按钮时,App force会在模拟器中关闭。我logcat的说,问题是,在第38行,switch语句,其中up​​dateDate()被调用,并在第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); 
      } 


} 

感谢提前:)

+0

放在try/catch语句查找异常..尝试{tv.setText(日期);}赶上(){}然后告诉.. – sai 2012-02-21 12:45:03

回答

0

创建的TextView你加太后..所以

TextView tv = new TextView(this); 
       tv.setText(date); 

获取内容查看并说,

contentLayout.addView(tv); 
0

看起来你在updateDate()中创建了一个新的Textview,但是你没有做任何事情。相反,只是更新现有的TextView:

TextView txt = (TextView) findViewById(R.id.info); 
txt.setText(date); 
+0

只是尝试这样做,用模拟器跑了,仍然没有输出:(难道是在怎么说让Java日历组件连接问题时间和dat è?我不熟悉它是如何工作的。 – user1036553 2012-02-21 10:13:30

+0

以上建议是很好的第一步。但是,您还需要去掉'的setContentView(R.id.info);在''更新时间调用()'和'updateDate()'。有了这些你基本上什么改变屏幕上显示出对你没有用做任何一个TextView - 因此,什么也不显示/更新。 – 2012-02-21 10:18:07

0

我觉得你没有设置OnClickListener到按钮

public void onCreate(Bundle b) 
     { 
      super.onCreate(b); 
      setContentView(R.layout.main); 
      clock = (Button)this.findViewById(R.id.clock); //Time Button 
      dayOf = (Button)this.findViewById(R.id.dayOf); //Date Button 
      clock.setOnClickListener(this); 
      dayOf.setOnClickListener(this); 

     } 
0

这应该工作:

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; 
    TextView info; 

    @Override 
    //*******************onCreate******************* 
    public void onCreate(Bundle b) 
    { 
     super.onCreate(b); 

     this.setContentView(R.layout.main); // set content view firstly 


     clock = (Button)this.findViewById(R.id.clock); //Time Button 
     dayOf = (Button)this.findViewById(R.id.dayOf); //Date Button 
     // set OnClickListener, you can also use the "android:click" attribute in the XML 
     clock.setOnClickListener(this); 
     dayOf.setOnClickListener(this); 

     info = (TextView)this.findViewById(R.id.info); //TextView 
    } 

     //*******************onClick******************* 
     public void onClick(View v) //Called when one of the buttons above is clicked 
     { 
      switch(v.getId()) 
      { 
       case R.id.clock: 
        updateTime(); 
        break; // don't forget break ! Otherwise, updateDate() will be execute 

       case R.id.dayOf: 
        updateDate(); 
        break; 
      } 

     } 

      //*******************updateTime******************* 
      private void updateTime() 
      { 
       Calendar d = Calendar.getInstance(); 
       d.getTime(); 

       // just update info 
       info.setText(d.getTime().toString()); 

       // Following is wrong! 
       //TextView tv = new TextView(this); 
       //tv.setText(d.getTime().toString()); 
       // setContentView(R.id.info); 
      } 

      //*******************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); 

       // just update info 
       info.setText(d.getTime().toString()); 

       // Following is wrong! 
       //TextView tv = new TextView(this); 
       //tv.setText(date); 
       // setContentView(R.id.info); 
      } 
} 
+0

谢谢,这对我来说已经解决了:) – user1036553 2012-02-21 11:17:49