试图在SQLite数据库中保存数据 - 清理(没有重大问题)仍然强制关闭

时间:2013-03-29 16:13:47

标签: java android sqlite forceclose

我对StackOverflow有点新意,我想知道是否可以对以下问题进行一些输入:我试图在sqlite database中保存一些数据,但每次尝试为此,app力关闭,我不知道为什么。我已经清理了文件,没有任何问题。 (我不知道我做错了什么 - 但有些东西编程不正确。)

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)
    {
          setContentView(R.layout.add_country);  // where your_layout is the name of the xml file with the layout you want to use minus the .xml extention
         //this layout must contain all of these views below that you are trying to initialize

          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"));    
             String time = extras.getString("time");
             String[] parts = time.split(":");
             timeEt.setCurrentHour(Integer.valueOf(parts[0]));
             timeEt.setCurrentMinute(Integer.valueOf(parts[1]));
             timeEt.setIs24HourView(false);
          }

          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() + ":"
                              + timeEt.getCurrentMinute().toString(),
                          codeEt.getText().toString());

              }
              else
              {
                  dbConnector.insertContact(nameEt.getText().toString(),
                          capEt.getText().toString(),
                          timeEt.getCurrentHour().toString() + ":"
                              + timeEt.getCurrentMinute().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" />

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

问题:

清理 - 没问题

LOGCAT:



03-29 13:23:28.950: D/OpenGLRenderer(20744): Enabling debug mode 0
    03-29 13:23:33.780: D/AndroidRuntime(20744): Shutting down VM
    03-29 13:23:33.780: W/dalvikvm(20744): threadid=1: thread exiting with uncaught exception (group=0x41f7b930)
    03-29 13:23:33.790: E/AndroidRuntime(20744): FATAL EXCEPTION: main
    03-29 13:23:33.790: E/AndroidRuntime(20744): android.app.SuperNotCalledException: Activity {com.app.gamedemo/com.app.gamedemo.AddEditCountry} did not call through to super.onCreate()
    03-29 13:23:33.790: E/AndroidRuntime(20744):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2146)
    03-29 13:23:33.790: E/AndroidRuntime(20744):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    03-29 13:23:33.790: E/AndroidRuntime(20744):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    03-29 13:23:33.790: E/AndroidRuntime(20744):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    03-29 13:23:33.790: E/AndroidRuntime(20744):    at android.os.Handler.dispatchMessage(Handler.java:99)
    03-29 13:23:33.790: E/AndroidRuntime(20744):    at android.os.Looper.loop(Looper.java:137)
    03-29 13:23:33.790: E/AndroidRuntime(20744):    at android.app.ActivityThread.main(ActivityThread.java:5041)
    03-29 13:23:33.790: E/AndroidRuntime(20744):    at java.lang.reflect.Method.invokeNative(Native Method)
    03-29 13:23:33.790: E/AndroidRuntime(20744):    at java.lang.reflect.Method.invoke(Method.java:511)
    03-29 13:23:33.790: E/AndroidRuntime(20744):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    03-29 13:23:33.790: E/AndroidRuntime(20744):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    03-29 13:23:33.790: E/AndroidRuntime(20744):    at dalvik.system.NativeStart.main(Native Method)
    03-29 13:23:35.390: I/Process(20744): Sending signal. PID: 20744 SIG: 9

1 个答案:

答案 0 :(得分:0)

问题在于onCreate()

 @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);

在尝试访问setContentView()之前,您需要致电EditTexts,依此类推。您的布局(xml文件)中的Views“实时”,因此它们为null,直到您layout为止。把它改成像

那样
 @Override
    public void onCreate(Bundle savedInstanceState)
    {
          setContentView(R.layout.your_layout);  // where your_layout is the name of the xml file with the layout you want to use minus the .xml extention
         //this layout must contain all of these views below that you are trying to initialize

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

修改

就像错误所说的那样,您正试图将TextView投射到TimePicker。这里

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

(R.id.timeEdit)指向TextView,但此处

private TimePicker timeEt;

您将其声明为TimePicker。如果您希望它是TimePicker,那么在xml中更改它

`<TimePicker 
    android:id="@+id/timeEdit"      
    style="@style/StyleText"/> `

此外,您的xml中有多个ID

android:id="@+id/codeText"

可能想要改变那个

第二次修改

03-29 13:23:33.790: E/AndroidRuntime(20744): android.app.SuperNotCalledException: Activity {com.app.gamedemo/com.app.gamedemo.AddEditCountry} did not call through to super.onCreate()

logcat中的这一行说明了一切。你没有打电话给super.onCreate()。将以下行添加为onCreate()方法

中的第一行
super.onCreate(savedInstanceState);

阅读logcat时,请查找Fatal Exception的位置。下一行应该说明它是什么(空指针异常,类别抛出异常等等)然后查找引用您的包名称的第一行,它将告诉您从哪里开始寻找错误。防爆。 Main.java 57告诉你,无论致命异常发生在第57行的Main.java文件中。问题可能最初来自其他地方,但这始终是一个好的起点,所以你可以缩小它并发布相关代码如果你需要帮助。