1

次のチュートリアルのソース コードに TimePicker を実装しようとしています。

http://www.lukehorvat.com/blog/android-time-picker-example/

ただし、アプリに実装すると、強制終了の問題が発生します。

助言がありますか?

JAVA:

import android.app.Activity;
import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TimePicker;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;

import com.nfc.linkingmanager.TimePickerFragment.TimePickedListener;

import java.util.Calendar;

public class AddEditCountry extends Activity implements TimePickedListener 
{
    private TextView mPickedTimeText;
    private Button mPickTimeButton;
    private long rowID; 
     private EditText nameEt;
     private EditText capEt;
     private EditText codeEt;
     private TimePicker timeEt;
     public static final String KEY_BUNDLE_TIME = "time";
     public static final String KEY_BUNDLE_MIN = "min";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {




          nameEt = (EditText) findViewById(R.id.nameEdit);
          capEt = (EditText) findViewById(R.id.capEdit);
          codeEt = (EditText) findViewById(R.id.codeEdit);
          timeEt = (TimePicker) findViewById(R.id.timeEdit);



          Bundle extras = getIntent().getExtras(); 

          if (extras != null)
          {
             rowID = extras.getLong("row_id");
             nameEt.setText(extras.getString("name"));  
             capEt.setText(extras.getString("cap"));  
             codeEt.setText(extras.getString("code"));
             timeEt.setCurrentHour(extras.containsKey(KEY_BUNDLE_TIME) ? extras.getInt(KEY_BUNDLE_TIME) : 0);
          }

          Button saveButton =(Button) findViewById(R.id.saveBtn);
          saveButton.setOnClickListener(new OnClickListener() {

              public void onClick(View v) 
              {
                 if (nameEt.getText().length() != 0)
                 {
                    AsyncTask<Object, Object, Object> saveContactTask = 
                       new AsyncTask<Object, Object, Object>() 
                       {
                          @Override
                          protected Object doInBackground(Object... params) 
                          {
                             saveContact();
                             return null;
                          }

                          @Override
                          protected void onPostExecute(Object result) 
                          {
                             finish();
                          }
                       }; 

                    saveContactTask.execute((Object[]) null); 
                 }

                 else
                 {
                    AlertDialog.Builder alert = new AlertDialog.Builder(AddEditCountry.this);
                    alert.setTitle(R.string.errorTitle); 
                    alert.setMessage(R.string.errorMessage);
                    alert.setPositiveButton(R.string.errorButton, null); 
                    alert.show();
                 }
              } 
         });
       }


           private void saveContact() 
           {
              DatabaseConnector dbConnector = new DatabaseConnector(this);

              if (getIntent().getExtras() == null)
              {
                  dbConnector.insertContact(nameEt.getText().toString(),
                          capEt.getText().toString(),
                          timeEt.getCurrentHour().toString(),
                          codeEt.getText().toString());

              }
              else
              {
                 dbConnector.updateContact(rowID,
                    nameEt.getText().toString(),
                    capEt.getText().toString(),
                    timeEt.getCurrentHour().toString(),                 
                    codeEt.getText().toString());
              }
           }


        @Override
        public void onTimePicked(Calendar time)
        {
            // display the selected time in the TextView
            mPickedTimeText.setText(DateFormat.format("h:mm a", time));
        }
}

国 XML を追加:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" android:layout_weight="1">

   <LinearLayout android:id="@+id/linearLayout"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical" 
      android:padding="5dp">

      <EditText android:id="@+id/nameEdit"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" 
         android:imeOptions="actionNext" 
         android:hint="@string/name_hint"
         android:inputType="textPersonName|textCapWords"/>

      <EditText android:id="@+id/capEdit"
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:imeOptions="actionNext" 
         android:hint="@string/cap_hint"
         android:inputType="textPersonName|textCapWords"/>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Data Limit"
        android:textColor="#ffffff"
        android:textAppearance="?android:textAppearanceMedium" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:gravity="left"
            android:textColor="#ffffff"
            android:text="10MB" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:gravity="right"
            android:textColor="#ffffff"
            android:text="Unlimited Data" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bandwidth Limit"
        android:textColor="#ffffff"
        android:textAppearance="?android:textAppearanceMedium" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:gravity="left"
            android:textColor="#ffffff"
            android:text="10kbs" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:textColor="#ffffff"
            android:gravity="right"
            android:text="Unlimited Bandwidth" />
    </LinearLayout>

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceSmall" />

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="WiFi Time Limit"
        android:textColor="#ffffff"
        android:textAppearance="?android:textAppearanceMedium" />


    <TextView
        android:id="@+id/text_picked_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <Button
        android:id="@+id/button_pick_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_below="@id/text_picked_time"
        android:text="Pick a time" />

    <EditText
        android:id="@+id/codeEdit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/code_hint"
        android:imeOptions="actionNext"
        android:inputType="textUri"
        android:lines="1" >

        <requestFocus />
    </EditText>

      <Button android:id="@+id/saveBtn" 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginTop="15dp"
         android:layout_gravity="center_horizontal"
         android:text="@string/save_btn"/>
   </LinearLayout>
</ScrollView>

タイムピッカーフラグメント.JAVA

package com.nfc.linkingmanager;


import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;

import java.util.Calendar;

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener
{
    private TimePickedListener mListener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        // use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
    }

    @Override
    public void onAttach(Activity activity)
    {
        // when the fragment is initially shown (i.e. attached to the activity), cast the activity to the callback interface type
        super.onAttach(activity);
        try
        {
            mListener = (TimePickedListener) activity;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString() + " must implement " + TimePickedListener.class.getName());
        }
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute)
    {
        // when the time is selected, send it to the activity via its callback interface method
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);

        mListener.onTimePicked(c);
    }

    public static interface TimePickedListener
    {
        public void onTimePicked(Calendar time);
    }
}

ログキャット:

03-25 02:50:06.783: I/Adreno200-EGLSUB(27045): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-25 02:50:06.783: D/memalloc(27045): ion: Mapped buffer base:0x5ca43000 size:614400 offset:0 fd:57
03-25 02:50:06.783: E/(27045): Can't open file for reading
03-25 02:50:06.783: E/(27045): Can't open file for reading
03-25 02:50:06.823: D/memalloc(27045): ion: Mapped buffer base:0x5d234000 size:614400 offset:0 fd:61
03-25 02:50:08.695: D/Activity(27045): Activity.onPause(), editTextTapSensorList size: 0
03-25 02:50:08.715: W/dalvikvm(27045): threadid=1: thread exiting with uncaught exception (group=0x4108b9d8)
03-25 02:50:08.715: E/AndroidRuntime(27045): FATAL EXCEPTION: main
03-25 02:50:08.715: E/AndroidRuntime(27045): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nfc.linkingmanager/com.nfc.linkingmanager.AddEditCountry}: java.lang.NullPointerException
03-25 02:50:08.715: E/AndroidRuntime(27045):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960)
03-25 02:50:08.715: E/AndroidRuntime(27045):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
03-25 02:50:08.715: E/AndroidRuntime(27045):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-25 02:50:08.715: E/AndroidRuntime(27045):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
03-25 02:50:08.715: E/AndroidRuntime(27045):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 02:50:08.715: E/AndroidRuntime(27045):    at android.os.Looper.loop(Looper.java:137)
03-25 02:50:08.715: E/AndroidRuntime(27045):    at android.app.ActivityThread.main(ActivityThread.java:4477)
03-25 02:50:08.715: E/AndroidRuntime(27045):    at java.lang.reflect.Method.invokeNative(Native Method)
03-25 02:50:08.715: E/AndroidRuntime(27045):    at java.lang.reflect.Method.invoke(Method.java:511)
03-25 02:50:08.715: E/AndroidRuntime(27045):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
03-25 02:50:08.715: E/AndroidRuntime(27045):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-25 02:50:08.715: E/AndroidRuntime(27045):    at dalvik.system.NativeStart.main(Native Method)
03-25 02:50:08.715: E/AndroidRuntime(27045): Caused by: java.lang.NullPointerException
03-25 02:50:08.715: E/AndroidRuntime(27045):    at com.nfc.linkingmanager.AddEditCountry.onCreate(AddEditCountry.java:73)
03-25 02:50:08.715: E/AndroidRuntime(27045):    at android.app.Activity.performCreate(Activity.java:4701)
03-25 02:50:08.715: E/AndroidRuntime(27045):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
03-25 02:50:08.715: E/AndroidRuntime(27045):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
03-25 02:50:08.715: E/AndroidRuntime(27045):    ... 11 more
4

2 に答える 2

2

XML に TimeEdit という要素がありません。そのため、テキストを設定しようとすると、null ポインター例外でクラッシュします。

于 2013-03-25T06:55:40.457 に答える
1

Timepicker を定義する場所

 timeEt = (TimePicker) findViewById(R.id.timeEdit);

あなたのxmlファイルで。xml ファイルを確認して、すべてに異なる ID を指定してください。

于 2013-03-25T06:57:26.420 に答える