2014-10-02 108 views
1

我想打一个应用程序,但是当我运行时,logcat的说明:致命的异常主要致命异常主要SQL DATABSE

CODE:

package com.example.mythirdapp; 

import android.app.Activity; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.RadioButton; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
     SQLiteDatabase pf; 

     TableRow tablerow; 
     TextView t,t1,t2,t3,t4,t5;  
     String first_name,last_name; 
     String userGender; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     pf=openOrCreateDatabase("Profile",MODE_PRIVATE,null); 
     pf.execSQL("CREATE TABLE IF NOT EXISTS Persone(first_name VARCHAR,last_name VARCHAR,userGender VARCHAR);"); 
    } 


    public void add(View view){ 
     EditText fn = (EditText)findViewById(R.id.first_name); 
     EditText ln = (EditText)findViewById(R.id.last_name); 

     first_name=fn.getText().toString(); 
     last_name =ln.getText().toString(); 

     boolean checked = ((RadioButton) view).isChecked(); 

     // Check which radio button was clicked 
     switch(view.getId()) { 
      case R.id.radio_female: 
       if (checked) 
        userGender = "female"; 
       break; 
      case R.id.radio_male: 
       if (checked) 
        userGender = "male"; 
       break; 
     } 

     pf.execSQL("INSERT INTO Persone VALUES('"+first_name+"','"+last_name+"','"+userGender+"');"); 
    } 



    public void show(View view){ 
     Cursor c = pf.rawQuery("SELECT * FROM Persone", null); 
     int count = c.getCount(); 
     c.moveToFirst(); 
     TableLayout tablelayout = new TableLayout(getApplicationContext()); 
     tablelayout.setVerticalScrollBarEnabled(true); 
     TableRow tablerow; 
     TextView t,t1,t2,t3,t4,t5; 
     tablerow = new TableRow(getApplicationContext()); 

     t = new TextView(getApplicationContext()); 
     t.setText("First Name"); 
     t.setTextColor(Color.RED); 
     t.setTypeface(null,Typeface.BOLD); 
     t.setPadding(20,20,20,20); 
     tablerow.addView(t); 

     t3 = new TextView(getApplicationContext()); 
     t3.setText("Last Name"); 
     t3.setTextColor(Color.RED); 
     t3.setTypeface(null,Typeface.BOLD); 
     t3.setPadding(20,20,20,20); 
     tablerow.addView(t3); 

     t4 = new TextView(getApplicationContext()); 
     t4.setText("Gender"); 
     t4.setTextColor(Color.RED); 
     t4.setTypeface(null,Typeface.BOLD); 
     t4.setPadding(20,20,20,20); 
     tablerow.addView(t4); 

     tablelayout.addView(tablerow); 
     for(Integer j = 0;j<count;j++) 
     { 
      tablerow = new TableRow(getApplicationContext()); 

      t1 = new TextView(getApplicationContext()); 
      t1.setText(c.getString(c.getColumnIndex("first_name"))); 
      t1.setPadding(20, 20, 20, 20); 


      t2 = new TextView(getApplicationContext()); 
      t2.setText(c.getString(c.getColumnIndex("last_name"))); 
      t2.setPadding(20, 20, 20, 20); 

      t5 = new TextView(getApplicationContext()); 
      t5.setText(c.getString(c.getColumnIndex("userGender"))); 
      t5.setPadding(20, 20, 20, 20); 



      tablerow.addView(t1); 
      tablerow.addView(t2); 
      tablerow.addView(t5); 


      tablelayout.addView(tablerow); 

      c.moveToNext(); 
     } 
     setContentView(tablelayout); 
     pf.close(); 
    } 
} 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:paddingBottom="@dimen/activity_vertical_margin" 
 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
 
    android:paddingRight="@dimen/activity_horizontal_margin" 
 
    android:paddingTop="@dimen/activity_vertical_margin" 
 
    tools:context="com.example.mythirdapp.MainActivity" > 
 

 
    <TextView 
 
     android:id="@+id/textView2" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignLeft="@+id/textView1" 
 
     android:layout_below="@+id/textView1" 
 
     android:layout_marginTop="14dp" 
 
     android:text="@string/last_name" 
 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
 

 
    <EditText 
 
     android:id="@+id/first_name" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignBaseline="@+id/textView1" 
 
     android:layout_alignBottom="@+id/textView1" 
 
     android:layout_alignParentRight="true" 
 
     android:layout_toRightOf="@+id/textView1" 
 
     android:ems="10" 
 
     android:inputType="textPersonName" > 
 

 
     <requestFocus android:layout_width="match_parent" /> 
 

 
    </EditText> 
 

 
    <TextView 
 
     android:id="@+id/textView1" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignParentLeft="true" 
 
     android:layout_alignParentTop="true" 
 
     android:layout_marginTop="14dp" 
 
     android:text="@string/first_name" 
 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
 

 
    <Button 
 
     android:id="@+id/button1" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignParentBottom="true" 
 
     android:layout_alignParentLeft="true" 
 
     android:layout_marginBottom="16dp" 
 
     android:onClick="add" 
 
     android:text="@string/add" /> 
 

 
    <Button 
 
     android:id="@+id/Button01" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignBaseline="@+id/button1" 
 
     android:layout_alignBottom="@+id/button1" 
 
     android:layout_alignRight="@+id/last_name" 
 
     android:onClick="show" 
 
     android:text="@string/show" /> 
 

 
    <EditText 
 
     android:id="@+id/last_name" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignLeft="@+id/first_name" 
 
     android:layout_below="@+id/textView1" 
 
     android:ems="10" 
 
     android:inputType="textPersonName" /> 
 

 
    <TextView 
 
     android:id="@+id/textView3" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignLeft="@+id/textView2" 
 
     android:layout_below="@+id/last_name" 
 
     android:layout_marginTop="14dp" 
 
     android:text="@string/gender" 
 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
 

 
    <RadioGroup 
 
     android:id="@+id/radio_gender" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="fill_parent" 
 
     android:layout_alignRight="@+id/Button01" 
 
     android:layout_alignTop="@+id/textView3" 
 
     android:layout_toRightOf="@+id/button1" 
 
     > 
 

 
     <RadioButton 
 
      android:id="@+id/radio_female" 
 
      android:text="@string/radio_female" 
 
      android:onClick="add" /> 
 

 
     <RadioButton 
 
      android:id="@+id/radio_male" 
 
      android:text="@string/radio_male" 
 
      android:onClick="add"/> 
 

 
    </RadioGroup> 
 

 
</RelativeLayout>

比logcat

10-02 10:25:55.156: E/AndroidRuntime(854): FATAL EXCEPTION: main 
10-02 10:25:55.156: E/AndroidRuntime(854): Process: com.example.mythirdapp, PID: 854 
10-02 10:25:55.156: E/AndroidRuntime(854): java.lang.IllegalStateException: Could not execute method of the activity 
10-02 10:25:55.156: E/AndroidRuntime(854): at android.view.View$1.onClick(View.java:3823) 
10-02 10:25:55.156: E/AndroidRuntime(854): at android.view.View.performClick(View.java:4438) 
10-02 10:25:55.156: E/AndroidRuntime(854): at android.view.View$PerformClick.run(View.java:18422) 
10-02 10:25:55.156: E/AndroidRuntime(854): at android.os.Handler.handleCallback(Handler.java:733) 
10-02 10:25:55.156: E/AndroidRuntime(854): at android.os.Handler.dispatchMessage(Handler.java:95) 
10-02 10:25:55.156: E/AndroidRuntime(854): at android.os.Looper.loop(Looper.java:136) 
10-02 10:25:55.156: E/AndroidRuntime(854): at android.app.ActivityThread.main(ActivityThread.java:5017) 
10-02 10:25:55.156: E/AndroidRuntime(854): at java.lang.reflect.Method.invokeNative(Native Method) 
10-02 10:25:55.156: E/AndroidRuntime(854): at java.lang.reflect.Method.invoke(Method.java:515) 
10-02 10:25:55.156: E/AndroidRuntime(854): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
10-02 10:25:55.156: E/AndroidRuntime(854): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
10-02 10:25:55.156: E/AndroidRuntime(854): at dalvik.system.NativeStart.main(Native Method) 
10-02 10:25:55.156: E/AndroidRuntime(854): Caused by: java.lang.reflect.InvocationTargetException 
10-02 10:25:55.156: E/AndroidRuntime(854): at java.lang.reflect.Method.invokeNative(Native Method) 
10-02 10:25:55.156: E/AndroidRuntime(854): at java.lang.reflect.Method.invoke(Method.java:515) 
10-02 10:25:55.156: E/AndroidRuntime(854): at android.view.View$1.onClick(View.java:3818) 
10-02 10:25:55.156: E/AndroidRuntime(854): ... 11 more 
10-02 10:25:55.156: E/AndroidRuntime(854): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.RadioButton 
10-02 10:25:55.156: E/AndroidRuntime(854): at com.example.mythirdapp.MainActivity.add(MainActivity.java:46) 
10-02 10:25:55.156: E/AndroidRuntime(854): ... 14 more 

我认为这是settext问题,但我不知道为什么。 我想将文本添加到SQL中。 但是,当涉及到添加性别,它出错

+1

请张贴更多来自LogCat的fo,所以我们可以看到引发异常的地方。 – 2014-10-02 12:39:27

+0

postcat的异常 – Siva 2014-10-02 12:42:31

回答

1

你的问题是在这里

<Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="16dp" 
     android:onClick="add" 
     android:text="@string/add" /> 

正是这里android:onClick="add"add(View view)方法,你是铸造视图以单选按钮,

boolean checked = ((RadioButton) view).isChecked(); 

有时它会是巴顿,因为你已经宣布的onClick的按钮“添加”太

这就是为什么你有这样的例外

ClassCastException: android.widget.Button cannot be cast to android.widget.RadioButton 

所以无论你通过检查解决这个问题是什么观点的实例或声明的另一种方法为按钮

+0

对不起,我不明白。你的意思是我应该改变onclick名称的名称,一个是“add”,一个是“another”。然后在mainactivity中,我应该创建public void add(){}和public void another(){}。 ....? – 2014-10-02 15:57:02

+0

是的,这就是你要做的 – Yazan 2014-10-02 23:14:05

+0

它的作品,非常感谢!!!!!!!!!! – 2014-10-03 12:38:19

0

您的代码工作正常。我不知道你是如何实现你的布局,但假设它是最简单的:有两个editTexts,两个radioButtons和一个按钮来显示结果。以下是我如何实现这些事情。

MainActivity.class

import android.app.Activity; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.RadioButton; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

public class MainActivity extends Activity 
{ 
    SQLiteDatabase pf; 

TableRow tablerow; 
TextView t, t1, t2, t3, t4, t5; 
String first_name, last_name; 
String userGender; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    pf = openOrCreateDatabase("Profile", MODE_PRIVATE, null); 
    pf.execSQL("CREATE TABLE IF NOT EXISTS Persone(first_name VARCHAR,last_name VARCHAR,userGender VARCHAR);"); 
} 

public void add(View view) 
{ 
    EditText fn = (EditText) findViewById(R.id.first_name); 
    EditText ln = (EditText) findViewById(R.id.last_name); 

    first_name = fn.getText().toString(); 
    last_name = ln.getText().toString(); 

    boolean checked = ((RadioButton) view).isChecked(); 

    // Check which radio button was clicked 
    switch (view.getId()) 
    { 
     case R.id.radio_female: 
      if (checked) 
       userGender = "female"; 
      break; 
     case R.id.radio_male: 
      if (checked) 
       userGender = "male"; 
      break; 
    } 

    pf.execSQL("INSERT INTO Persone VALUES('" + first_name + "','" + last_name + "','" + userGender + "');"); 
} 

public void show(View view) 
{ 
    Cursor c = pf.rawQuery("SELECT * FROM Persone", null); 
    int count = c.getCount(); 
    c.moveToFirst(); 
    TableLayout tablelayout = new TableLayout(getApplicationContext()); 
    tablelayout.setVerticalScrollBarEnabled(true); 
    TableRow tablerow; 
    TextView t, t1, t2, t3, t4, t5; 
    tablerow = new TableRow(getApplicationContext()); 

    t = new TextView(getApplicationContext()); 
    t.setText("First Name"); 
    t.setTextColor(Color.RED); 
    t.setTypeface(null, Typeface.BOLD); 
    t.setPadding(20, 20, 20, 20); 
    tablerow.addView(t); 

    t3 = new TextView(getApplicationContext()); 
    t3.setText("Last Name"); 
    t3.setTextColor(Color.RED); 
    t3.setTypeface(null, Typeface.BOLD); 
    t3.setPadding(20, 20, 20, 20); 
    tablerow.addView(t3); 

    t4 = new TextView(getApplicationContext()); 
    t4.setText("Gender"); 
    t4.setTextColor(Color.RED); 
    t4.setTypeface(null, Typeface.BOLD); 
    t4.setPadding(20, 20, 20, 20); 
    tablerow.addView(t4); 

    tablelayout.addView(tablerow); 
    for (Integer j = 0; j < count; j++) 
    { 
     tablerow = new TableRow(getApplicationContext()); 

     t1 = new TextView(getApplicationContext()); 
     t1.setText(c.getString(c.getColumnIndex("first_name"))); 
     t1.setPadding(20, 20, 20, 20); 

     t2 = new TextView(getApplicationContext()); 
     t2.setText(c.getString(c.getColumnIndex("last_name"))); 
     t2.setPadding(20, 20, 20, 20); 

     t5 = new TextView(getApplicationContext()); 
     t5.setText(c.getString(c.getColumnIndex("userGender"))); 
     t5.setPadding(20, 20, 20, 20); 

     tablerow.addView(t1); 
     tablerow.addView(t2); 
     tablerow.addView(t5); 

     tablelayout.addView(tablerow); 

     c.moveToNext(); 
    } 
    setContentView(tablelayout); 
    pf.close(); 
} 
} 

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.stackoverflow_1.MainActivity" > 

<EditText 
    android:id="@+id/first_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<EditText 
    android:id="@+id/last_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:ems="10" /> 

<RadioButton 
    android:id="@+id/radio_female" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="add" 
    android:text="Female" /> 

<RadioButton 
    android:id="@+id/radio_male" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="add" 
    android:text="Male" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="show" 
    android:text="Show" /> 

</LinearLayout> 
+0

我也添加了一个RadioGroup,这有什么关系吗? – 2014-10-02 14:11:20

+0

代码在您的计算机上工作吗? – 2014-10-02 15:58:23

+0

@JiYicheng RadioGroup不会影响任何东西......是的,它可以在我的电脑上运行......你的电脑仍然不工作......? – 2014-10-02 17:47:17