Kepler411
2021-06-21 12:55
采纳率: 100%
浏览 54

Android Studio sql数据库运行插入和查询的页面闪退以及类型转换问题

1.creditJudge.java

package com.kepler.studentcreditsystem10;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class CreditJudge extends AppCompatActivity {
    Intent config1,config2;
    private EditText credit_name;
    private EditText credit_type ;
    private EditText credit_score;
 String score1;

    DBHelper databaseHelper = new DBHelper(CreditJudge.this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credit_jude);
   credit_name = findViewById(R.id.editTextTextPersonName4);
        credit_type = findViewById(R.id.credit_acttype_plain);
   credit_score = findViewById(R.id.credit_score_plain);
   score1 = String.valueOf(credit_score);

    }


    public void goCreditShow(View view) {
        config1 = new Intent(this,CreditsShow.class);


        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("ACTNAME", String.valueOf(credit_name));
        values.put("ACTTYPE", String.valueOf(credit_type));
        values.put("ACTSCORE",Float.parseFloat(score1));

        long id = db.insert("tb_activity", null, values);
        Log.d("myDeBug", "insert");


        db.close();

       startActivity(config1);
    }

}

相应的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CreditsShow">

    <TextView
        android:id="@+id/study"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="250dp"
        android:layout_marginRight="250dp"
        android:layout_marginBottom="638dp"
        android:text="@string/credit_study"
        android:textSize="25dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.239"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteY="70dp" />

    <TextView
        android:id="@+id/art"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="221dp"
        android:layout_marginRight="221dp"
        android:layout_marginBottom="544dp"
        android:text="@string/credit_science"
        android:textSize="25dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="53dp"
        tools:layout_editor_absoluteY="345dp" />

    <TextView
        android:id="@+id/science"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="225dp"
        android:layout_marginRight="225dp"
        android:layout_marginBottom="446dp"
        android:text="@string/credit_art"
        android:textSize="25dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="53dp"
        tools:layout_editor_absoluteY="255dp" />

    <TextView
        android:id="@+id/society"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="225dp"
        android:layout_marginRight="225dp"
        android:layout_marginBottom="351dp"
        android:text="@string/credit_society"
        android:textSize="25dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="53dp"
        tools:layout_editor_absoluteY="173dp" />

    <TextView
        android:id="@+id/study_score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="115dp"
        android:layout_marginRight="115dp"
        android:layout_marginBottom="355dp"
        android:text="0.0"
        android:textSize="25dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="278dp"
        tools:layout_editor_absoluteY="78dp" />

    <TextView
        android:id="@+id/science_score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="115dp"
        android:layout_marginRight="115dp"
        android:layout_marginBottom="453dp"
        android:text="0.0"
        android:textSize="25dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="281dp"
        tools:layout_editor_absoluteY="173dp" />

    <TextView
        android:id="@+id/art_score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="115dp"
        android:layout_marginRight="115dp"
        android:layout_marginBottom="550dp"
        android:text="0.0"
        android:textSize="25dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="284dp"
        tools:layout_editor_absoluteY="255dp" />

    <TextView
        android:id="@+id/society_score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="115dp"
        android:layout_marginRight="115dp"
        android:layout_marginBottom="642dp"
        android:text="0.0"
        android:textSize="25dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="284dp"
        tools:layout_editor_absoluteY="345dp" />

    <Button
        android:id="@+id/btn_credit_jude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="146dp"
        android:layout_marginRight="146dp"
        android:layout_marginBottom="224dp"
        android:onClick="openCreditJudge"
        android:text="@string/item_label"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="161dp"
        tools:layout_editor_absoluteY="559dp" />

    <Button
        android:id="@+id/credit_back1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="150dp"
        android:layout_marginRight="150dp"
        android:layout_marginBottom="112dp"
        android:text="@string/back"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="161dp"
        tools:layout_editor_absoluteY="670dp" />
</RelativeLayout>

 

2.CreditsShow.java

package com.kepler.studentcreditsystem10;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class CreditsShow extends AppCompatActivity {
  Intent config1,config2;
  TextView score_study ;
    TextView score_art ;
    TextView society_study ;
    TextView science_study ;
    Float score1,score2,score3,score4;


  DBHelper databaseHelper = new DBHelper(CreditsShow.this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credits);
     score_study = findViewById(R.id.study_score);
     score_art = findViewById(R.id.art_score);
       society_study = findViewById(R.id.society_score);
     science_study = findViewById(R.id.science_score);
        SQLiteDatabase db= databaseHelper.getReadableDatabase();

        Cursor cur=db.rawQuery("select * from tb_activity",null);
        int sum=cur.getCount();

        for(int i=0;i<sum;i++)
        {
            cur.moveToPosition(i);

          Float score_add =  cur.getFloat(cur.getColumnIndex("ACTSCORE"));
          String type = cur.getString(cur.getColumnIndex("ACTTYPE"));
            if(type.equals("学术活动"))
            {
                score1= score1 +score_add;
            }
            else if(type.equals("科研讲座"))
            {
                score2= score2 +score_add;
            }
            else if(type.equals("文体艺术"))
            {
                score3= score3 +score_add;
            }

        }
         score_art.setText(String.format(score1.toString()));
        score_study.setText(String.format(score1.toString()));
        society_study.setText(String.format(score1.toString()));
        science_study.setText(String.format(score1.toString()));
        cur.close();

    }

    public void openCreditJudge(View view) {
        config2=new Intent(this,CreditJudge.class);
        startActivity(config2);
    }
}

相应的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CreditJudge"
    android:background="@drawable/bg1">

    <TextView
        android:id="@+id/credit_actname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="235dp"
        android:layout_marginRight="235dp"
        android:layout_marginBottom="649dp"
        android:text="@string/credit_actname"
        android:textSize="25dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="43dp"
        tools:layout_editor_absoluteY="115dp" />

    <TextView
        android:id="@+id/credit_acttype"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="239dp"
        android:layout_marginRight="239dp"
        android:layout_marginBottom="565dp"
        android:text="@string/credit_acttype"
        android:textSize="25dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="43dp"
        tools:layout_editor_absoluteY="217dp" />

    <TextView
        android:id="@+id/credit_score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="238dp"
        android:layout_marginRight="238dp"
        android:layout_marginBottom="435dp"
        android:text="@string/credit_score"
        android:textSize="25dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="42dp"
        tools:layout_editor_absoluteY="308dp" />

    <Button
        android:id="@+id/handin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="152dp"
        android:layout_marginRight="152dp"
        android:layout_marginBottom="195dp"
        android:onClick="goCreditShow"
        android:text="@string/credit_tj"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="152dp"
        tools:layout_editor_absoluteY="630dp" />

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="-1dp"
        android:layout_marginRight="-1dp"
        android:layout_marginBottom="329dp"
        android:ems="10"
        android:inputType="textPersonName"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="168dp"
        tools:layout_editor_absoluteY="115dp" />

    <EditText
        android:id="@+id/credit_score_plain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginBottom="424dp"
        android:ems="10"
        android:hint="@string/credit_score_text"
        android:inputType="number"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="168dp"
        tools:layout_editor_absoluteY="217dp" />

    <EditText
        android:id="@+id/credit_acttype_plain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginBottom="560dp"
        android:ems="10"
        android:hint="@string/credit_acttype_type"
        android:inputType="textPersonName"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="167dp"
        tools:layout_editor_absoluteY="308dp" />

    <TextView
        android:id="@+id/textView12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="239dp"
        android:layout_marginRight="239dp"
        android:layout_marginBottom="336dp"
        android:text="@string/credit_fj"
        android:textSize="25sp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="43dp"
        tools:layout_editor_absoluteY="405dp" />

    <EditText
        android:id="@+id/editTextTextPersonName4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="3dp"
        android:layout_marginRight="3dp"
        android:layout_marginBottom="638dp"
        android:ems="10"
        android:hint="@string/credit_actname_text"
        android:inputType="textPersonName"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="168dp"
        tools:layout_editor_absoluteY="405dp" />

    <TextView
        android:id="@+id/type_introduction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="180dp"
        android:layout_marginLeft="180dp"
        android:layout_marginEnd="13dp"
        android:layout_marginRight="13dp"
        android:layout_marginBottom="504dp"
        android:text="@string/type_intro" />

</RelativeLayout>

错误提示如下:

 Caused by: java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText{34ab734 VFED..CL. ......I. 0,0-0,0 #7f08006f app:id/credit_score_plain}"
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
        at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
        at java.lang.Float.parseFloat(Float.java:451)
        at com.kepler.studentcreditsystem10.CreditJudge.goCreditShow(CreditJudge.java:41)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) 
        at android.view.View.performClick(View.java:7448) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967) 
        at android.view.View.performClickInternal(View.java:7425) 
        at android.view.View.access$3600(View.java:810) 
        at android.view.View$PerformClick.run(View.java:28305) 
        at android.os.Handler.handleCallback(Handler.java:938) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
 

 

找到相应代码行为:

values.put("ACTSCORE",Float.parseFloat(score1));

或者就是CreditsShow里

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'float java.lang.Float.floatValue()' on a null object reference
        at com.kepler.studentcreditsystem10.CreditsShow.onCreate(CreditsShow.java:45)


 
score_add =  cur1.getFloat(cur1.getColumnIndex("ACTSCORE"));

救救孩子吧

 

ACTSCORE类型为FLOAT

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 帅次 2021-06-21 14:44
    已采纳

    在CreditJudge中获取credit_score中的内容有误,修改为
    score1 = String.valueOf(credit_score.getText().toString());

    打赏 评论
  • CSDN专家-sinjack 2021-06-21 13:08

    建议:调试运行一下,看下哪行代码出现的异常,也可以加点输出信息,可能存在异常数据。

    打赏 评论
  • Kepler411 2021-06-21 15:45

    太感谢了!!!!!

    打赏 评论

相关推荐 更多相似问题