java - XML 不会显示保存的 TimePicker 数据 (CurrentHour)

标签 java android xml

我有以下 XML 文件,显示标签不匹配错误。非常感谢任何建议!

(我正在尝试查看来自 TimePicker 的数据)

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

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

add_country xml(导致崩溃的TimePicker数据首先进入系统)

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

查看国家/地区 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>  


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("code", codeTv.getText());
            addEditContact.putExtra("time", timeTv.getCurrentHour());
            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();
   }
}



Database Connector




import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;


public class DatabaseConnector {

private static final String DB_NAME = "WorldCountries";
private SQLiteDatabase database;
private DatabaseOpenHelper dbOpenHelper;

public DatabaseConnector(Context context) {
    dbOpenHelper = new DatabaseOpenHelper(context, DB_NAME, null, 1);
}

   public void open() throws SQLException 
   {
      //open database in reading/writing mode
      database = dbOpenHelper.getWritableDatabase();
   } 

   public void close() 
   {
      if (database != null)
         database.close();
   }       

   public void insertContact(String name, String cap, String code, String time) 
           {
              ContentValues newCon = new ContentValues();
              newCon.put("name", name);
              newCon.put("cap", cap);
              newCon.put("time", time);
              newCon.put("code", code);

              open();
              database.insert("country", null, newCon);
              close();
           }


           public void updateContact(long id, String name, String cap,String code,  String time) 
           {
              ContentValues editCon = new ContentValues();
              editCon.put("name", name);
              editCon.put("cap", cap);
              editCon.put("time", time);
              editCon.put("code", code);

              open();
              database.update("country", editCon, "_id=" + id, null);
              close();
           }


           public Cursor getAllContacts() 
           {
              return database.query("country", new String[] {"_id", "name"}, 
                 null, null, null, null, "name");
           }

           public Cursor getOneContact(long id) 
           {
              return database.query("country", null, "_id=" + id, null, null, null, null);
           }

           public void deleteContact(long id) 
           {
              open(); 
              database.delete("country", "_id=" + id, null);
              close();
           }
}

洛卡特:

03-22 04:40:09.319: D/Activity(2206): Activity.onPause(), editTextTapSensorList size: 0
03-22 04:40:09.479: I/Adreno200-EGLSUB(2206): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-22 04:40:09.479: D/memalloc(2206): ion: Mapped buffer base:0x5d00d000 size:614400 offset:0 fd:69
03-22 04:40:09.569: D/memalloc(2206): ion: Mapped buffer base:0x5d997000 size:614400 offset:0 fd:75
03-22 04:40:09.599: D/memalloc(2206): ion: Mapped buffer base:0x5da3d000 size:614400 offset:0 fd:78
03-22 04:40:09.599: D/memalloc(2206): ion: Unmapping buffer  base:0x5c989000 size:614400
03-22 04:40:09.599: D/memalloc(2206): ion: Unmapping buffer  base:0x5ca41000 size:614400
03-22 04:40:09.599: D/memalloc(2206): ion: Unmapping buffer  base:0x5cf66000 size:614400 
03-22 04:40:15.376: D/Activity(2206): Activity.onPause(), editTextTapSensorList size: 0
03-22 04:40:15.416: I/Adreno200-EGLSUB(2206): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-22 04:40:15.426: D/memalloc(2206): ion: Mapped buffer base:0x5cf06000 size:614400 offset:0 fd:56
03-22 04:40:15.496: D/memalloc(2206): ion: Mapped buffer base:0x5ddd3000 size:614400 offset:0 fd:60
03-22 04:40:15.516: D/memalloc(2206): ion: Unmapping buffer  base:0x5d00d000 size:614400
03-22 04:40:15.516: D/memalloc(2206): ion: Unmapping buffer  base:0x5d997000 size:614400
03-22 04:40:15.526: D/memalloc(2206): ion: Unmapping buffer  base:0x5da3d000 size:614400
03-22 04:40:15.556: D/memalloc(2206): ion: Mapped buffer base:0x5d00d000 size:614400 offset:0  fd:63
03-22 04:40:18.409: D/Activity(2206): Activity.onPause(), editTextTapSensorList size: 0
03-22 04:40:18.469: W/dalvikvm(2206): threadid=1: thread exiting with uncaught exception  (group=0x410889d8)
03-22 04:40:18.469: E/AndroidRuntime(2206): FATAL EXCEPTION: main
03-22 04:40:18.469: E/AndroidRuntime(2206): 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 04:40:18.469: E/AndroidRuntime(2206):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at   android.os.Handler.dispatchMessage(Handler.java:99)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at android.os.Looper.loop(Looper.java:137)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at  android.app.ActivityThread.main(ActivityThread.java:4477)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at  java.lang.reflect.Method.invokeNative(Native Method)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at  java.lang.reflect.Method.invoke(Method.java:511)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at dalvik.system.NativeStart.main(Native Method)
03-22 04:40:18.469: E/AndroidRuntime(2206): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.TimePicker
03-22 04:40:18.469: E/AndroidRuntime(2206):     at com.nfc.linkingmanager.ViewCountry.setUpViews(ViewCountry.java:38)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at com.nfc.linkingmanager.ViewCountry.onCreate(ViewCountry.java:30)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at android.app.Activity.performCreate(Activity.java:4701)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
03-22 04:40:18.469: E/AndroidRuntime(2206):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
03-22 04:40:18.469: E/AndroidRuntime(2206):     ... 11 more

最佳答案

这就是问题 - 底线,我已经分成多行:

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

您还没有关闭 TableRow关闭 TableLayout 之前的元素元素。

(请注意,如果您以“自然缩进”的方式布置 XML,而不是在底部放置一长行,您自己可能会更容易看到这一点。)

我还希望您使用任何编辑器来编辑 XML 以非常清楚地显示错误,例如通过在 </TableLayout> 下划线.

关于java - XML 不会显示保存的 TimePicker 数据 (CurrentHour),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15565556/

相关文章:

java - 在 Java 中包含图像

android - 操作栏选项卡 - 在一个选项卡中有两个 fragment (一个是动态的)

java - 在 Eclipse 中调试 Android 上的死锁

java - SAX 模型是否有任何 XPath 处理器?

sql-server - 将两个 xml 片段合并为一个?

java - 根据对象属性动态定义XML标签

java - 用数组列表在java中写入文件但不起作用

java - 在默认包下创建一个新类

android - 如何处理耳机 Hook 的双击?

xml - XSLT:如果标签存在,应用模板;如果不是,选择静态值