方法不被称为java

时间:2016-02-10 12:42:50

标签: java android

因此,只要用户点击按钮,我就会调用此方法。它不起作用。 继承人的代码。 这可能是什么问题。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button refresh=(Button)findViewById(R.id.btn1);
    displayer();
     refresh.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             TextView Tv = (TextView) findViewById(R.id.TV);
             TextView Tv2 = (TextView) findViewById(R.id.Tv2);
             String date = sdf.format(calendar.getTime());
             Tv.setText("The current time is " + date);
             String str = date.charAt(0) + "" + date.charAt(1) + "" + date.charAt(3) + "" + date.charAt(4);
             Tv2.setText("So the password will be " + str);
         }
     });

private void displayer() {
    TextView Tv=(TextView)findViewById(R.id.TV);
    TextView Tv2=(TextView)findViewById(R.id.Tv2);
    String date = sdf.format(calendar.getTime());
    Tv.setText("The current time is "+date);
    String str=date.charAt(0)+""+date.charAt(1)+""+date.charAt(3)+""+date.charAt(4);
    Tv2.setText("So the password will be " + str);
}

< -------------------------- EDIT ------------------ ----------> 这是编辑过的文件。该方法不被称为。因为textview没有变化。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button refresh=(Button)findViewById(R.id.btn1);
    final TextView Tv=(TextView)findViewById(R.id.TV);
    final TextView Tv2=(TextView)findViewById(R.id.Tv2);
    displayer();
     refresh.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             displayer();
         }
     });
}

private void displayer() {
    String date = sdf.format(calendar.getTime());
    Tv.setText("The current time is "+date);
    String str=date.charAt(0)+""+date.charAt(1)+""+date.charAt(3)+""+date.charAt(4);
    Tv2.setText("So the password will be " + str);
  }

显示的错误是在找不到方法显示器Tv和Tv2。

1 个答案:

答案 0 :(得分:3)

移动下面的displayer();

@Override
public void onClick(View v) {   

所以你得到:

@Override
public void onClick(View v) {
     displayer(); //Call method "displayer" when user presses button
     TextView Tv = (TextView) findViewById(R.id.TV);
     TextView Tv2 = (TextView) findViewById(R.id.Tv2);
     String date = sdf.format(calendar.getTime());
     Tv.setText("The current time is " + date);
     String str = date.charAt(0) + "" + date.charAt(1) + "" + date.charAt(3) + "" + date.charAt(4);
     Tv2.setText("So the password will be " + str);
}

但是因为两次做同样的事没有意义,所以你只需要:

@Override
public void onClick(View v) {
     displayer();         //Call method "displayer" when user presses button
}

<强> 修改 所以你的所有代码都是这样的:

private Button refresh;
private TextView Tv;
private TextView Tv2;

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

    refresh = (Button)findViewById(R.id.btn1);
    Tv = (TextView)findViewById(R.id.TV);
    Tv2 = (TextView)findViewById(R.id.Tv2);

    refresh.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        displayer();            //Call method "displayer" when user presses button
    }
});

private void displayer() {
    String date = sdf.format(calendar.getTime());
    Tv.setText("The current time is "+date);
    String str=date.charAt(0)+""+date.charAt(1)+""+date.charAt(3)+""+date.charAt(4);
    Tv2.setText("So the password will be " + str);
}

PS: 检查您的ID是否未在您的XML中混淆

<TextView
        android:id="@+id/TV"   //ID is important
        //Probably more stuff
/>

<TextView
        android:id="@+id/Tv2"  //ID is important
        //Probably more stuff
/>