【问题标题】:My Android app crashes when trying to store SQLite data尝试存储 SQLite 数据时,我的 Android 应用程序崩溃
【发布时间】:2014-04-07 01:23:44
【问题描述】:

我尝试将 SQLite 示例代码导入我的 Android 应用程序,以使用 ListView 通过 SQLite 保存和管理我的数据。

在这个实现地址簿的 SQLite 示例中,当我填写新联系人的文本字段并按“保存”按钮将它们保存到我的 SQLite 时,我的 Android 应用程序崩溃,显示不幸的是我的应用程序已终止。我相信我的问题集中在按钮 Save(button1) 上,尤其是 line: android:onClick="run" 但我不明白确切的问题。为了您的方便,“run”方法是在 DisplayContact.java 存档中实现的。

我的activity_display_contact.xml布局中我的button1的代码是:

<Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/editTextCity"
      android:layout_alignParentBottom="true"
      android:layout_marginBottom="28dp"
      android:onClick="run"
      android:text="@string/save" />

我的 DisplayContact.java 的代码负责 ListView 的布局是:

package com.qualcomm.QCARSamples.ImageTargets;

import com.qualcomm.QCARSamples.ImageTargets1.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DisplayContact extends Activity {

   int from_Where_I_Am_Coming = 0;
   private DBHelper mydb ;
   TextView name ;
   TextView phone;
   TextView email;
   TextView street;
   TextView place;
   int id_To_Update = 0;


   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_display_contact);
      name = (TextView) findViewById(R.id.editTextName);
      phone = (TextView) findViewById(R.id.editTextPhone);
      email = (TextView) findViewById(R.id.editTextStreet);
      street = (TextView) findViewById(R.id.editTextEmail);
      place = (TextView) findViewById(R.id.editTextCity);

      mydb = new DBHelper(this);

      Bundle extras = getIntent().getExtras(); 
      if(extras !=null)
      {
         int Value = extras.getInt("id");
         if(Value>0){
            //means this is the view part not the add contact part.
            Cursor rs = mydb.getData(Value);
            id_To_Update = Value;
            rs.moveToFirst();
            String nam = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_NAME));
            String phon = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_PHONE));
            String emai = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_EMAIL));
            String stree = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_STREET));
            String plac = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_CITY));
            if (!rs.isClosed()) 
            {
               rs.close();
            }
            Button b = (Button)findViewById(R.id.button1);
            b.setVisibility(View.INVISIBLE);

            name.setText((CharSequence)nam);
            name.setFocusable(false);
            name.setClickable(false);

            phone.setText((CharSequence)phon);
            phone.setFocusable(false); 
            phone.setClickable(false);

            email.setText((CharSequence)emai);
            email.setFocusable(false);
            email.setClickable(false);

            street.setText((CharSequence)stree);
            street.setFocusable(false); 
            street.setClickable(false);

            place.setText((CharSequence)plac);
            place.setFocusable(false);
            place.setClickable(false);
           }
      }
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      Bundle extras = getIntent().getExtras(); 
      if(extras !=null)
      {
         int Value = extras.getInt("id");
         if(Value>0){
            getMenuInflater().inflate(R.menu.display_contact, menu);
         }
         else{
            getMenuInflater().inflate(R.menu.mainmenu, menu);
         }
      }
      return true;
   }

   public boolean onOptionsItemSelected(MenuItem item) 
   { 
      super.onOptionsItemSelected(item); 
      switch(item.getItemId()) 
   { 
      case R.id.Edit_Contact: 
      Button b = (Button)findViewById(R.id.button1);
      b.setVisibility(View.VISIBLE);
      name.setEnabled(true);
      name.setFocusableInTouchMode(true);
      name.setClickable(true);

      phone.setEnabled(true);
      phone.setFocusableInTouchMode(true);
      phone.setClickable(true);

      email.setEnabled(true);
      email.setFocusableInTouchMode(true);
      email.setClickable(true);

      street.setEnabled(true);
      street.setFocusableInTouchMode(true);
      street.setClickable(true);

      place.setEnabled(true);
      place.setFocusableInTouchMode(true);
      place.setClickable(true);

      return true; 
      case R.id.Delete_Contact:

      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setMessage(R.string.deleteContact)
     .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int id) {
            mydb.deleteContact(id_To_Update);
            Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();  
            Intent intent = new Intent(getApplicationContext(),com.qualcomm.QCARSamples.ImageTargets.MainActivity.class);
            startActivity(intent);
         }
      })
     .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int id) {
            // User cancelled the dialog
         }
      });
      AlertDialog d = builder.create();
      d.setTitle("Are you sure");
      d.show();

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

      } 
   } 

   public void run(View view)
   {    
      Bundle extras = getIntent().getExtras();
      if(extras !=null)
      {
         int Value = extras.getInt("id");
         if(Value>0){
            if(mydb.updateContact(id_To_Update,name.getText().toString(), phone.getText().toString(), email.getText().toString(), street.getText().toString(), place.getText().toString())){
               Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();   
               Intent intent = new Intent(getApplicationContext(),com.qualcomm.QCARSamples.ImageTargets.MainActivity.class);
               startActivity(intent);
             }      
            else{
               Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();   
            }
         }
         else{
            if(mydb.insertContact(name.getText().toString(), phone.getText().toString(), email.getText().toString(), street.getText().toString(), place.getText().toString())){
               Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_SHORT).show();  
            }       
            else{
               Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();  
            }
            Intent intent = new Intent(getApplicationContext(),com.qualcomm.QCARSamples.ImageTargets.MainActivity.class);
            startActivity(intent);
            }
      }
   }
}

我的Logcat的代码如下:

04-07 04:09:25.113: E/AndroidRuntime(7943): FATAL EXCEPTION: main
04-07 04:09:25.113: E/AndroidRuntime(7943): java.lang.IllegalStateException: Could not find a method run(View) in the activity class com.qualcomm.QCARSamples.ImageTargets.ImageTargets for onClick handler on view class android.widget.Button with id 'button1'
04-07 04:09:25.113: E/AndroidRuntime(7943):     at android.view.View$1.onClick(View.java:3050)
04-07 04:09:25.113: E/AndroidRuntime(7943):     at android.view.View.performClick(View.java:3534)
04-07 04:09:25.113: E/AndroidRuntime(7943):     at android.view.View$PerformClick.run(View.java:14263)
04-07 04:09:25.113: E/AndroidRuntime(7943):     at android.os.Handler.handleCallback(Handler.java:605)
04-07 04:09:25.113: E/AndroidRuntime(7943):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 04:09:25.113: E/AndroidRuntime(7943):     at android.os.Looper.loop(Looper.java:137)
04-07 04:09:25.113: E/AndroidRuntime(7943):     at android.app.ActivityThread.main(ActivityThread.java:4441)
04-07 04:09:25.113: E/AndroidRuntime(7943):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 04:09:25.113: E/AndroidRuntime(7943):     at java.lang.reflect.Method.invoke(Method.java:511)
04-07 04:09:25.113: E/AndroidRuntime(7943):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-07 04:09:25.113: E/AndroidRuntime(7943):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-07 04:09:25.113: E/AndroidRuntime(7943):     at dalvik.system.NativeStart.main(Native Method)
04-07 04:09:25.113: E/AndroidRuntime(7943): Caused by: java.lang.NoSuchMethodException: run [class android.view.View]
04-07 04:09:25.113: E/AndroidRuntime(7943):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
04-07 04:09:25.113: E/AndroidRuntime(7943):     at java.lang.Class.getMethod(Class.java:915)
04-07 04:09:25.113: E/AndroidRuntime(7943):     at android.view.View$1.onClick(View.java:3043)
04-07 04:09:25.113: E/AndroidRuntime(7943):     ... 11 more

【问题讨论】:

  • 你有一个名为 ImageTargets 的活动吗?
  • 是的,我有。这是我项目的主要活动。在我的 Imagetargets 代码中,我使用 setContentView(R.layout.activity_display_contact);访问我的 SQLite 的 ListView。我的 SQLite 在三个类中实现,名为 DBHelper.java、MainActivity.java(这是我插入到我自己的项目中的示例 SQLite 项目的主要活动)和 DisplayContact.java。跨度>

标签: java android sqlite button crash


【解决方案1】:

这很奇怪:您的 Java 代码创建了一个名为“DisplayContact”的 Activity,但堆栈跟踪显示它在名为“ImageTargets”的 Activity 中找不到名为“run”的 onClick 处理程序。我会检查您的开发项目的设置方式。

【讨论】:

  • 我有一个名为 ImageTargets 的活动,它是我项目的主要活动。在我的 Imagetargets 代码中,我使用 setContentView(R.layout.activity_display_contact);访问我的 SQLite 的 ListView。我的 SQLite 在三个类中实现,名为 DBHelper.java、MainActivity.java(这是我插入到我自己的项目中的示例 SQLite 项目的主要活动)和 DisplayContact.java。你认为我必须在 ImageTargets.java 或其他地方插入“运行”吗?
  • 是的!!!您必须在 Activity 类中插入 run,在您的情况下是 ImageTargets。
  • 顺便说一句。 on... 如果您不将 DisplayContact 作为 Activity 启动,则可能永远不会调用 DisplayContact 类中的方法。因此,您还必须将 run 方法所依赖的部分也移动到 ImageTargets。
猜你喜欢
  • 1970-01-01
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 2017-11-10
  • 1970-01-01
相关资源
最近更新 更多