2 개의 안드로이드 앱을 만들었습니다. Timepickers를 사용하여 동일한 코드를 작성했지만 하나는 작동하고 다른 하나는 작동하지 않습니다.

user2855719

나는 문제가 무엇인지 알아 내려고 노력했지만 시간이 지남에 따라 점점 더 절망적 인 것처럼 보입니다. "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) : com.example.rustelefon.Beregning.onCreate (Beregning.java:32)

아짓 프라 탑 싱

Beregning 활동에서 timePicker1을 초기화하지 않았습니다. Beregning 클래스에 initCommonGui () 메서드를 추가하고 setCurrentTimeOnView ()를 호출하기 전에 onCreate 메서드에서 호출합니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관