点击添加闪退
package com.hala.dataresoverdemo; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.net.Uri; import android.support.annotation.IdRes; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.RadioGroup; import android.widget.Toast; public class Main2Activity extends AppCompatActivity { ContentResolver resolver; private EditText name,age,id; private RadioGroup radioGroup; private String gender; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); name=(EditText)findViewById(R.id.name); age=(EditText)findViewById(R.id.age); id=(EditText)findViewById(R.id.id); radioGroup=(RadioGroup)findViewById(R.id.gender); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { if(checkedId==R.id.male){ gender="男"; }else{ gender="女"; } } }); } public void operate(View v){ String mname=name.getText().toString(); String mage=age.getText().toString(); switch (v.getId()){ case R.id.insert: //参数一:URI(统一资源定位符) //参数二:values ContentValues values=new ContentValues(); values.put("name",mname); values.put("age",mage); values.put("gender",gender); //这里的uri就是ContentProvider中insert方法的返回值,可以从这里获得id Uri uri=resolver.insert(Uri.parse("content://com.hala.myprovide"),values); long id= ContentUris.parseId(uri); Toast.makeText(this, "添加成功,新学生的学号是:"+id, Toast.LENGTH_SHORT).show(); break; } } }
reslover文件
package com.hala.dataproviderdemo; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.net.Uri; import android.util.Log; public class MyContentProvider extends ContentProvider { public MyContentProvider() { } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { // Implement this to handle requests to delete one or more rows. throw new UnsupportedOperationException("Not yet implemented"); } @Override public String getType(Uri uri) { // TODO: Implement this to handle requests for the MIME type of the data // at the given URI. throw new UnsupportedOperationException("Not yet implemented"); } @Override public Uri insert(Uri uri, ContentValues values) { // TODO: Implement this to handle requests to insert a new row. Log.e("TAG","调用了dataprovider中的insert方法"); long id=db.insert("info_db",null,values); //将id追加到uri后边,这个id就是数据库中那个主键_id return ContentUris.withAppendedId(uri, id); } SQLiteDatabase db; /** * 在ContentProvider(dataproviderdemo第一次启动)创建时调用 * @return */ @Override public boolean onCreate() { // TODO: Implement this to initialize your content provider on startup. SQLiteOpenHelper helper=new SQLiteOpenHelper(getContext(),"stu.db",null,1) { @Override public void onCreate(SQLiteDatabase db) { String sql="create table info_tb (_id integer primary key autoincrement, "+ "name varchar(20),"+ "age integer,"+ "gender varchar(2))"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }; db=helper.getReadableDatabase(); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // TODO: Implement this to handle query requests from clients. throw new UnsupportedOperationException("Not yet implemented"); } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO: Implement this to handle requests to update one or more rows. throw new UnsupportedOperationException("Not yet implemented"); } }
provider文件
报错
03-14 23:58:55.442 3833-3833/com.hala.dataresoverdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hala.dataresoverdemo, PID: 3833
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.ContentResolver.insert(android.net.Uri, android.content.ContentValues)' on a null object reference
at com.hala.dataresoverdemo.Main2Activity.operate(Main2Activity.java:54)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
正在回答
空指针异常。 ContentResolver resolver;resolver为空吧。Main2Activity类中operate()那一块。祝:学习愉快
从程序报错的地方能看出来是空指针的问题,在Main2Activity54的位置,你可以看看contentvalue里面的值是不是空的,没有传进去
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星