2013-03-22 3 views
0

나는이 시점에서 내가 뭘 잘못하고 있는지 잘 모르겠다. 나는 시간 선택기에 저장된 데이터를 veging하려고 시도하고있다. 그러나 매번 어플리케이션을 실행할 때마다 강제 닫기 오류가 발생합니다.SUPER Simple TimePicker 구현 - 라인 74에서 강제 종료

P.

문제가보기 Country.java

timeTv.setCurrentHour (result.getInt (timeIndex))에서 선 (74)에 따른되어 나타납니다;

데이터 OUT - JAVA :

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.TextView; 
import android.widget.TimePicker; 

public class ViewCountry extends Activity { 

    private long rowID; 
    private TextView nameTv; 
    private TextView capTv; 
    private TextView codeTv; 
    private TimePicker timeTv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.view_country); 

     setUpViews(); 
     Bundle extras = getIntent().getExtras(); 
     rowID = extras.getLong(CountryList.ROW_ID); 
    } 

    private void setUpViews() { 
     nameTv = (TextView) findViewById(R.id.nameText); 
     capTv = (TextView) findViewById(R.id.capText); 
     timeTv = (TimePicker) findViewById(R.id.timePicker1); 
     codeTv = (TextView) findViewById(R.id.codeText); 
    } 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
     new LoadContacts().execute(rowID); 
    } 

    private class LoadContacts extends AsyncTask<Long, Object, Cursor> 
    { 
     DatabaseConnector dbConnector = new DatabaseConnector(ViewCountry.this); 

     @Override 
     protected Cursor doInBackground(Long... params) 
     { 
     dbConnector.open(); 
     return dbConnector.getOneContact(params[0]); 
     } 

     @Override 
     protected void onPostExecute(Cursor result) 
     { 
     super.onPostExecute(result); 

     result.moveToFirst(); 
     // get the column index for each data item 
     int nameIndex = result.getColumnIndex("name"); 
     int capIndex = result.getColumnIndex("cap"); 
     int codeIndex = result.getColumnIndex("code"); 
     int timeIndex = result.getColumnIndex("time"); 

     nameTv.setText(result.getString(nameIndex)); 
     capTv.setText(result.getString(capIndex)); 
     timeTv.setCurrentHour(result.getInt(timeIndex)); 
     codeTv.setText(result.getString(codeIndex)); 

     result.close(); 
     dbConnector.close(); 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     super.onCreateOptionsMenu(menu); 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.view_country_menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     switch (item.getItemId()) 
     { 
     case R.id.editItem: 
      Intent addEditContact = 
       new Intent(this, AddEditCountry.class); 

      addEditContact.putExtra(CountryList.ROW_ID, rowID); 
      addEditContact.putExtra("name", nameTv.getText()); 
      addEditContact.putExtra("cap", capTv.getText()); 
      addEditContact.putExtra("time", timeTv.getCurrentHour()); 
      addEditContact.putExtra("code", codeTv.getText()); 
      startActivity(addEditContact); 
      return true; 

     case R.id.deleteItem: 
      deleteContact(); 
      return true; 

     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

    private void deleteContact() 
    { 

     AlertDialog.Builder alert = new AlertDialog.Builder(ViewCountry.this); 

     alert.setTitle(R.string.confirmTitle); 
     alert.setMessage(R.string.confirmMessage); 

     alert.setPositiveButton(R.string.delete_btn, 
     new DialogInterface.OnClickListener() 
     { 
      public void onClick(DialogInterface dialog, int button) 
      { 
       final DatabaseConnector dbConnector = 
        new DatabaseConnector(ViewCountry.this); 

       AsyncTask<Long, Object, Object> deleteTask = 
        new AsyncTask<Long, Object, Object>() 
        { 
        @Override 
        protected Object doInBackground(Long... params) 
        { 
         dbConnector.deleteContact(params[0]); 
         return null; 
        } 

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

       deleteTask.execute(new Long[] { rowID });    
      } 
     } 
    ); 

     alert.setNegativeButton(R.string.cancel_btn, null).show(); 
    } 
} 

DATA OUT XML :

<?xml version="1.0" encoding="utf-8"?> 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="1" 
    android:layout_margin="5dp"> 

    <TableRow>   
    <TextView 
     style="@style/StyleLabel" 
     android:text="@string/name_lbl"/> 
    <TextView 
     android:id="@+id/nameText" 
     style="@style/StyleText"/>   
    </TableRow> 

    <TableRow>   
    <TextView 
     style="@style/StyleLabel" 
     android:text="@string/cap_lbl"/>   
    <TextView 
     android:id="@+id/capText" 
     style="@style/StyleText"/>   
    </TableRow> 

    <TableRow>   
    <TextView 
     style="@style/StyleLabel" 
     android:text="Time Limit"/>   
    <TextView 
     android:id="@+id/codeText" 
     style="@style/StyleText"/>   
    </TableRow> 
    <TableRow>   
    <TextView 
     style="@style/StyleLabel" 
     android:text="Linked Users"/>   
    <TextView 

     style="@style/StyleText"/>   
    </TableRow> 
    <TableRow>   
    <TextView 
     style="@style/StyleLabel" 
     android:text="@string/code_lbl"/>   
    <TextView 
     android:id="@+id/code_lbl" 
     style="@style/StyleText"/> 

    </TableRow> 

데이터 입력 - 자바

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; 

public class AddEditCountry extends Activity { 

private long rowID; 
private EditText nameEt; 
private EditText capEt; 
private EditText codeEt; 
private TimePicker timeEt; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.add_country); 

     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.getInt("time")); 
     } 

     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()); 
     } 
    } 
} 

데이터 입력 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" /> 

<TimePicker 
    android:id="@+id/timeEdit" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:layout_weight="1.0" /> 

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




    <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> 

ViewCountry.java :

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.TextView; 
import android.widget.TimePicker; 

public class ViewCountry extends Activity { 

    private long rowID; 
    private TextView nameTv; 
    private TextView capTv; 
    private TextView codeTv; 
    private TimePicker timeTv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.view_country); 

     setUpViews(); 
     Bundle extras = getIntent().getExtras(); 
     rowID = extras.getLong(CountryList.ROW_ID); 
    } 

    private void setUpViews() { 
     nameTv = (TextView) findViewById(R.id.nameText); 
     capTv = (TextView) findViewById(R.id.capText); 
     timeTv = (TimePicker) findViewById(R.id.timeEdit); 
     codeTv = (TextView) findViewById(R.id.codeText); 
    } 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
     new LoadContacts().execute(rowID); 
    } 

    private class LoadContacts extends AsyncTask<Long, Object, Cursor> 
    { 
     DatabaseConnector dbConnector = new DatabaseConnector(ViewCountry.this); 

     @Override 
     protected Cursor doInBackground(Long... params) 
     { 
     dbConnector.open(); 
     return dbConnector.getOneContact(params[0]); 
     } 

     @Override 
     protected void onPostExecute(Cursor result) 
     { 
     super.onPostExecute(result); 

     result.moveToFirst(); 
     // get the column index for each data item 
     int nameIndex = result.getColumnIndex("name"); 
     int capIndex = result.getColumnIndex("cap"); 
     int codeIndex = result.getColumnIndex("code"); 
     int timeIndex = result.getColumnIndex("time"); 

     nameTv.setText(result.getString(nameIndex)); 
     capTv.setText(result.getString(capIndex)); 
     timeTv.setCurrentHour(result.getInt(timeIndex)); 
     codeTv.setText(result.getString(codeIndex)); 

     result.close(); 
     dbConnector.close(); 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     super.onCreateOptionsMenu(menu); 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.view_country_menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     switch (item.getItemId()) 
     { 
     case R.id.editItem: 
      Intent addEditContact = 
       new Intent(this, AddEditCountry.class); 

      addEditContact.putExtra(CountryList.ROW_ID, rowID); 
      addEditContact.putExtra("name", nameTv.getText()); 
      addEditContact.putExtra("cap", capTv.getText()); 
      addEditContact.putExtra("time", timeTv.getCurrentHour()); 
      addEditContact.putExtra("code", codeTv.getText()); 
      startActivity(addEditContact); 
      return true; 

     case R.id.deleteItem: 
      deleteContact(); 
      return true; 

     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

    private void deleteContact() 
    { 

     AlertDialog.Builder alert = new AlertDialog.Builder(ViewCountry.this); 

     alert.setTitle(R.string.confirmTitle); 
     alert.setMessage(R.string.confirmMessage); 

     alert.setPositiveButton(R.string.delete_btn, 
     new DialogInterface.OnClickListener() 
     { 
      public void onClick(DialogInterface dialog, int button) 
      { 
       final DatabaseConnector dbConnector = 
        new DatabaseConnector(ViewCountry.this); 

       AsyncTask<Long, Object, Object> deleteTask = 
        new AsyncTask<Long, Object, Object>() 
        { 
        @Override 
        protected Object doInBackground(Long... params) 
        { 
         dbConnector.deleteContact(params[0]); 
         return null; 
        } 

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

       deleteTask.execute(new Long[] { rowID });    
      } 
     } 
    ); 

     alert.setNegativeButton(R.string.cancel_btn, null).show(); 
    } 
} 

view_country.xml

<?xml version="1.0" encoding="utf-8"?> 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:stretchColumns="1" 
android:layout_margin="5dp"> 

<TableRow>   
    <TextView 
    style="@style/StyleLabel" 
    android:text="@string/name_lbl"/> 
<TextView 
    android:id="@+id/nameText" 
    style="@style/StyleText"/>   
</TableRow> 

<TableRow>   
<TextView 
    style="@style/StyleLabel" 
    android:text="@string/cap_lbl"/>   
<TextView 
    android:id="@+id/capText" 
    style="@style/StyleText"/>   
</TableRow> 

    <TableRow>  
    <TextView 
    style="@style/StyleLabel" 
    android:text="@string/code_lbl"/>      
<TextView 
    android:id="@+id/codeText" 
    style="@style/StyleText"/>   
</TableRow> 
<TableRow>   
<TextView 
    style="@style/StyleLabel" 
    android:text="Linked Users"/>   
<TextView 

    style="@style/StyleText"/>   
</TableRow> 
<TableRow>   
<TextView 
    style="@style/StyleLabel" 
    android:text="Time Limit"/>   
<TextView 
    android:id="@+id/timeEdit" 
    style="@style/StyleText"/> 

</TableRow> 
</TableLayout> 

가장 최근의 로그 캣 (view_country.xml의 텍스트 뷰에 timeEdit를 추가 한 후)

03-22 03:02:27.724: D/Activity(32616): Activity.onPause(), editTextTapSensorList size: 0 
03-22 03:02:27.794: W/dalvikvm(32616): threadid=1: thread exiting with uncaught exception (group=0x410889d8) 
03-22 03:02:27.794: E/AndroidRuntime(32616): FATAL EXCEPTION: main 
03-22 03:02:27.794: E/AndroidRuntime(32616): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nfc.linkingmanager/com.nfc.linkingmanager.ViewCountry}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.TimePicker 
03-22 03:02:27.794: E/AndroidRuntime(32616): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at android.app.ActivityThread.access$600(ActivityThread.java:127) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at android.os.Looper.loop(Looper.java:137) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at android.app.ActivityThread.main(ActivityThread.java:4477) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at java.lang.reflect.Method.invokeNative(Native Method) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at java.lang.reflect.Method.invoke(Method.java:511) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at dalvik.system.NativeStart.main(Native Method) 
03-22 03:02:27.794: E/AndroidRuntime(32616): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.TimePicker 
03-22 03:02:27.794: E/AndroidRuntime(32616): at com.nfc.linkingmanager.ViewCountry.setUpViews(ViewCountry.java:38) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at com.nfc.linkingmanager.ViewCountry.onCreate(ViewCountry.java:30) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at android.app.Activity.performCreate(Activity.java:4701) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051) 
03-22 03:02:27.794: E/AndroidRuntime(32616): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924) 
03-22 03:02:27.794: E/AndroidRuntime(32616): ... 11 more 
+0

에 적합한 레이아웃을 사용하는 ViewCountry.java의 라인 번호 74? –

+0

timeTv.setCurrentHour (result.getInt (timeIndex)); – NoobNinja

+0

Eclipse를 사용합니까? 디버깅을 시도하십시오. onPostExecute() (예 : 74 번)에 중단 점을 배치하고 result, timeIndex 및 timeTv의 상태를 검사하면됩니다. 당신은 그 객체들 중 어느 것이 null인지를 매우 빨리 말할 수있을 것입니다. –

답변

0

사용

여기서
timeTv = (TimePicker) findViewById(R.id.timeEdit); 

대신 당신이 TimePicker 아이디 android:id="@+id/timeEdit" 정의되어 있지만 현재 활동 레이아웃에서 사용할 수 없습니다 timePicker1를 얻거나 만들려고 노력했기 때문에

timeTv = (TimePicker) findViewById(R.id.timePicker1); 

있는지 당신은 활동

+0

나는 여전히 (라인 74에서 at-timeTv.setCurrentHour (result.getInt (timeIndex)) 충돌했다; 아직도? - 업데이트 된 logcat [위에서 굵은 글씨]로 판단) – NoobNinja

+0

이 도움은 대단히 감사합니다 ... – NoobNinja

+0

@NoobNinja : 또한 add_country xml 파일 게시 –