我已经制作了2个Android应用程序。使用Timepickers,我写了相同的代码,但一个正在工作,另一个不是

时间:2014-01-25 16:05:26

标签: java android

我试图找出问题所在,但似乎我只是越来越无望了。 “Promillekalkulator”类工作正常,但另一类“Beregning”甚至没有打开。错误说我必须修复setCurrentTimeOnView方法,但我在类“Promillekalkulator”上有完全相同的方法。任何帮助都会非常感激:////

import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class Beregning extends Activity{
TextView pil, klokkeslett;
    Button btnFerdig;
TimePicker timePicker1;
private int hour1;
private int minute1;
private int hour2;
private int minute2;
Calendar c;

static final int TIME_DIALOG_ID = 999;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_beregn);
    initCommonGui();
    setCurrentTimeOnView();
    initListeners();

}


public void initCommonGui(){
    pil = (TextView) findViewById(R.id.pil);
    klokkeslett = (TextView) findViewById(R.id.klokkeslett);
    btnFerdig = (Button) findViewById(R.id.btnFerdig);
}

private void setCurrentTimeOnView() {
    // TODO Auto-generated method stub
 c = Calendar.getInstance();
    hour1 = c.get(Calendar.HOUR_OF_DAY);
    minute1 = c.get(Calendar.MINUTE);
     klokkeslett.setText(new StringBuilder().append(pad(hour1)).append(":")
                .append(pad(minute1)));
        timePicker1.setVisibility(View.INVISIBLE);

}

private void initListeners() {
    // TODO Auto-generated method stub
    pil.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {

            showDialog(TIME_DIALOG_ID);

        }

    });

    btnFerdig.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            otherActivity("resultat_promille");
        }
    });

}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case TIME_DIALOG_ID:
        // set time picker as current time
        return new TimePickerDialog(this, timePickerListener, hour1,     minute1,
                false);

    }
    return null;
}

private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int selectedHour,
            int selectedMinute) {
        hour2 = selectedHour;
        minute2 = selectedMinute;

         klokkeslett.setText(new StringBuilder().append(pad(hour2)).append(":")
                    .append(pad(minute2)));
        // set current time into timepicker
        timePicker1.setCurrentHour(hour2);
        timePicker1.setCurrentMinute(minute2);

    }
};



private static String pad(int c) {
    if (c >= 10)
        return String.valueOf(c);
    else
        return "0" + String.valueOf(c);
}

public void otherActivity(String a){
    Intent i=new Intent();
    if(a.equals("resultat_promille")){
        i.setClass(getApplicationContext(),Resultat_Promille.class);
    }
    startActivity(i);
    finish();
}

}

XML布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="28dp"
    android:layout_marginTop="102dp"
    android:text="cl"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="44dp"
    android:text="%"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="70dp"
    android:layout_marginTop="97dp"
    android:text="40" />

<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="130dp"
    android:layout_marginTop="97dp"
    android:text="50" />

 <RadioButton
    android:id="@+id/radioButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="190dp"
    android:layout_marginTop="97dp"
    android:text="60" />

 <RadioButton
    android:id="@+id/radioButton4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="70dp"
    android:layout_marginTop="167dp"
    android:text="3,5" />

<RadioButton
    android:id="@+id/radioButton5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="130dp"
    android:layout_marginTop="167dp"
    android:text="4" />

 <RadioButton
    android:id="@+id/radioButton6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="190dp"
    android:layout_marginTop="167dp"
    android:text="4,5" />

 <TextView
     android:id="@+id/textView3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/textView2"
     android:layout_below="@+id/radioButton4"
     android:layout_marginTop="35dp"
     android:text="Klokke" 
     android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
     android:id="@+id/pil"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBaseline="@+id/textView3"
     android:layout_alignBottom="@+id/textView3"
     android:layout_alignLeft="@+id/radioButton5"
     android:text="->"
     android:textColor="@color/darkgreen" 
     android:textAppearance="?android:attr/textAppearanceLarge"/>

 <TimePicker
     android:id="@+id/timePicker1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
    android:layout_marginLeft="40dp"
     android:layout_marginTop="450dp"
   />

 <Button
     android:id="@+id/btnFerdig"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="140dp"
     android:layout_marginTop="310dp"
     android:text="Ferdig!" />

 <TextView
     android:id="@+id/klokkeslett"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBaseline="@+id/pil"
     android:layout_alignBottom="@+id/pil"
     android:layout_alignRight="@+id/radioButton6"
     android:text="" />

</RelativeLayout>

这是另一个程序“Promillekalkulator”,它工作得很好

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;
import java.util.Calendar;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

public class Promillekalkulator extends Activity {

/**
 * @param args
 */
TextView textView5;

TimePicker timePicker1;
Button button1;


private int hour1;
private int minute1;
private int hour2;
private int minute2;
Calendar c;



static final int TIME_DIALOG_ID = 999;

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


    initCommonGui();
    setCurrentTimeOnView();
    initListeners();

}

private void setCurrentTimeOnView() {
    // TODO Auto-generated method stub
 c = Calendar.getInstance();
    hour1 = c.get(Calendar.HOUR_OF_DAY);
    minute1 = c.get(Calendar.MINUTE);

    // set current time into textview
    textView5.setText(new StringBuilder().append(pad(hour1)).append(":")
            .append(pad(minute1)));

    timePicker1.setVisibility(View.INVISIBLE);
}

private void initListeners() {
    // TODO Auto-generated method stub
    button1.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {

            showDialog(TIME_DIALOG_ID);

        }

    });
}
@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case TIME_DIALOG_ID:
        // set time picker as current time
        return new TimePickerDialog(this, timePickerListener, hour1, minute1,
                false);

    }
    return null;
}

private TimePickerDialog.OnTimeSetListener timePickerListener = new     TimePickerDialog.OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int selectedHour,
            int selectedMinute) {
        hour2 = selectedHour;
        minute2 = selectedMinute;

        // set current time into textview
        textView5.setText(new StringBuilder().append(pad(hour2))
                .append(":").append(pad(minute2)));

        // set current time into timepicker
        timePicker1.setCurrentHour(hour2);
        timePicker1.setCurrentMinute(minute2);

    }
};



private static String pad(int c) {
    if (c >= 10)
        return String.valueOf(c);
    else
        return "0" + String.valueOf(c);
}


public void initCommonGui() {

    textView5 = (TextView) findViewById(R.id.textView5);
    timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
    button1 = (Button) findViewById(R.id.button1);



}

}

XML布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="29dp"
    android:text="@string/klokkeslett"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="22sp" />

<TimePicker
    android:id="@+id/timePicker1"
    android:layout_width="2dp"
    android:layout_height="2dp"
    android:layout_alignLeft="@+id/textView4"
    android:layout_below="@+id/textView4"
    android:layout_marginTop="65dp" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView4"
    android:layout_below="@+id/textView4"
    android:text="@string/velg" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView4"
    android:layout_alignBottom="@+id/textView4"
    android:layout_marginLeft="5dp"
    android:layout_toRightOf="@+id/textView4"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

这些是错误: 01-25 17:37:04.255:E / AndroidRuntime(1542):at com.example.rustelefon.Beregning.setCurrentTimeOnView(Beregning.java:50) 01-25 17:37:04.255:E / AndroidRuntime(1542):at com.example.rustelefon.Beregning.onCreate(Beregning.java:32)

1 个答案:

答案 0 :(得分:1)

您尚未在Beregning活动中初始化timePicker1。在Beregning类中添加initCommonGui()方法,并在调用setCurrentTimeOnView()之前从onCreate方法调用它。