0

我对 StackOverflow 有点陌生,我想知道是否可以就以下问题提供一些意见:我正在尝试在 a 中保存一些数据,sqlite database但每次我尝试这样做时,应用程序强制关闭并且我不确定为什么。我已经清理了文件,没有任何问题。(我不知道我做错了什么——但有些东西被错误地编程了。)

爪哇:

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
4

1 回答 1

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等等。您Views在布局(xml 文件)中的“实时”,因此它们一直存在,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);

编辑

就像错误所说的那样,您正在尝试将 aTextView转换为 a TimePicker。这里

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

(R.id.timeEdit)指向一个TextView但在这里

private TimePicker timeEt;

您将其声明为TimePicker. 如果您希望它是 aTimePicker然后在您的 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. 下一行应该说明它是什么(Null Pointer Exception、Class Cast Exception 等),然后查找引用您的包名称的第一行,它会告诉您从哪里开始查找错误。前任。Main.java 57 在第 57 行告诉您 Main.java 文件中发生的任何致命异常。问题可能最初来自其他地方,但这始终是一个很好的起点,因此您可以缩小范围并发布相关代码如果你需要帮助。

于 2013-03-29T16:16:54.290 回答