gpt4 book ai didi

android - 应用程序在单击按钮时停止并显示错误消息 "Appname has stopped unfortunately"

转载 作者:太空狗 更新时间:2023-10-29 16:03:38 24 4
gpt4 key购买 nike

标题 1

这是我用过的代码。我想给数据库添加一个名字。当我在 EditText 中输入名称时,它会接受,当我单击提交按钮时,它会显示消息“不幸的是 App_name 已停止” activity_main.xml

    <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="70dp"
android:text="USERNAME"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="28dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:inputType="textPersonName" >

<requestFocus />
</EditText>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="50dp"
android:text="SUBMIT" />

</RelativeLayout>`

标题 1

Main_Activity.java

package com.example.dbconcept;

import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {
Button b;
String user;
EditText usr;
SQLiteDatabase db;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.button1);
try{
db=openOrCreateDatabase("Mydb",MODE_PRIVATE,null);
db.execSQL("create table table1(user varchar(50))");
}
catch(Exception e)
{
}
b.setOnClickListener(this);
}


public void onClick(View arg0) {
user=usr.getText().toString();
db.execSQL("insert into table1 values('"+user+"')");


}



}

最佳答案

你还没有初始化usr

只需添加

usr = (EditText) findViewById(R.id.editText1)

到你的onCreate

关于android - 应用程序在单击按钮时停止并显示错误消息 "Appname has stopped unfortunately",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22480902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com