问题描述
我要创建我的Android应用程序的数据库。我已经写了code在我的项目的数据库;每当我运行它显示强制关闭在模拟器上。我曾尝试不同的方式,但没有任何工程。我去哪儿了?
// EventContentProvider.java
包com.event.test;进口android.content.ContentProvider;
进口android.content.ContentUris;
进口android.content.ContentValues;
进口android.content.Context;
进口android.content.UriMatcher;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteOpenHelper;
进口android.database.sqlite.SQLiteQueryBuilder;
进口android.net.Uri;公共类EventContentProvider扩展ContentProvider的{私有静态最后弦乐DATABASE_NAME =event.db;私有静态最终UriMatcher sUriMatcher;
私有静态最终诠释活动= 1;
私有静态最终诠释EVENT_ID = 2;私有静态类DatabaseHelper扩展SQLiteOpenHelper {公共DatabaseHelper(上下文的背景下){ 超级(上下文,DATABASE_NAME,空,1);
}@覆盖
公共无效的onCreate(SQLiteDatabase DB){
db.execSQL(CREATE TABLE EVENT1(_ID INTEGER PRIMARY KEY,FIRST_NAME TEXT,姓氏TEXT););}@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
}
} 私人DatabaseHelper mOpenHelper; @覆盖
公共布尔的onCreate(){
mOpenHelper =新DatabaseHelper(的getContext());
返回true;
}@覆盖
公众诠释删除(URI URI,字符串,其中,字符串[] whereArgs){
SQLiteDatabase DB = NULL;
诠释计数= 0;
尝试{
DB = mOpenHelper.getWritableDatabase();
开关(sUriMatcher.match(URI)){
活动情况:
计数= db.delete(Event.EVENT_TABLE_NAME,其中,whereArgs);
打破;
默认:
抛出新抛出:IllegalArgumentException(无匹配内容);
}
的getContext()getContentResolver()有NotifyChange(URI,空)。
}赶上(例外五){
} {最后
如果(DB!= NULL)
db.close();
}
返回计数;
}@覆盖
公共字符串的getType(URI URI){
开关(sUriMatcher.match(URI)){
活动情况:
返回Event.CONTENT_TYPE;
默认:
抛出新抛出:IllegalArgumentException(无匹配内容);
}
}@覆盖
公共乌里插入(URI URI,ContentValues initialValues){ SQLiteDatabase分贝= mOpenHelper.getWritableDatabase();
ContentValues值;
如果(initialValues!= NULL){
值=新ContentValues(initialValues);
}其他{
值=新ContentValues();
}
长ROWID = -1;
乌里contentUri = NULL;
开关(sUriMatcher.match(URI)){
活动情况:
ROWID = db.insertOrThrow(Event.EVENT_TABLE_NAME,Event._ID,价值观);
contentUri = ContentUris.withAppendedId(Event.CONTENT_URI,ROWID);
打破;
默认:
抛出新抛出:IllegalArgumentException(未知URI+ URI);
}
db.close(); 如果(ROWID大于0){
返回contentUri;
} 返回null;
}@覆盖
公众诠释更新(开放的URI,ContentValues价值观,选择字符串,字符串[] selectionArgs两个){
SQLiteDatabase分贝= mOpenHelper.getWritableDatabase(); 长ROWID = -1;
尝试{
开关(sUriMatcher.match(URI)){
活动情况:
ROWID = db.update(Event.EVENT_TABLE_NAME,价值观,选择,
selectionArgs两个);
打破;
默认:
抛出新抛出:IllegalArgumentException(未知URI+ URI); }
ROWID = db.update(Event.EVENT_TABLE_NAME,价值观,选择,
selectionArgs两个);
}赶上(例外五){
} {最后
如果(DB!= NULL)
db.close();
} 返回(INT)ROWID;
}@覆盖
公共光标查询(URI URI,字符串[]投影,选择字符串,字符串[] selectionArgs两个,字符串中将sortOrder){
SQLiteQueryBuilder QB =新SQLiteQueryBuilder(); 开关(sUriMatcher.match(URI)){
活动情况:
qb.setTables(Event.EVENT_TABLE_NAME);
qb.setProjectionMap(Event.sEventProjectionMap);
打破;
默认:
抛出新抛出:IllegalArgumentException(没有内容查询匹配);
} SQLiteDatabase DB = NULL;
光标C = NULL;
尝试{
DB = mOpenHelper.getReadableDatabase();
C = qb.query(分贝,投影,选择,selectionArgs两个,NULL,NULL,中将sortOrder);
c.setNotificationUri(的getContext()getContentResolver(),URI);
}赶上(例外五){
}
返回℃;
}
静态的 {
sUriMatcher =新UriMatcher(UriMatcher.NO_MATCH);
sUriMatcher.addURI(Event.AUTHORITY,事件,事件);
sUriMatcher.addURI(Event.AUTHORITY事件/#,EVENT_ID);
}
}
//Event.java
包com.event.test;进口的java.util.HashMap;
进口android.net.Uri;
进口android.provider.BaseColumns;公共类事件实现BaseColumns {公共静态最后弦乐AUTHORITY =com.event.data.contentprovider;
公共静态最终乌里CONTENT_URI = Uri.parse(内容://com.event.data.contentprovider/events);
公共静态最后弦乐CONTENT_TYPE =vnd.android.cursor.dir / vnd.event.event
公共静态最后弦乐CONTENT_ITEM_TYPE =vnd.android.cursor.event / vnd.event.event
公共静态最后弦乐EVENT_TABLE_NAME =EVENT1;
公共静态最后弦乐FIRSTNAME =FIRST_NAME;
公共静态最后弦乐LASTNAME =姓氏;
公共静态字符串_ID;
公共静态HashMap的<字符串,字符串> sEventProjectionMap;静态的 {
Event.sEventProjectionMap =新的HashMap<字符串,字符串>();
Event.sEventProjectionMap.put(Event._ID,Event._ID);
Event.sEventProjectionMap.put(Event.FIRSTNAME,Event.FIRSTNAME);
Event.sEventProjectionMap.put(Event.LASTNAME,Event.LASTNAME);}
}
//manifestfile.xml
< XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.event.test
安卓版code =1
机器人:=的versionName1.0>
<采用-SDK安卓的minSdkVersion =10/><应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>
<活动机器人:名字=。EventManagementActivity
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/>
<类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<供应商的android:NAME =com.event.test.EventContentProvider
机器人:当局=com.event.data.contentprovider/>
< /用途>
< /清单>
//EventManagementActivity.java
包com.event.test; 进口android.app.Activity;
进口android.content.ContentValues;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;公共类EventManagementActivity扩展活动实现OnClickListener {
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); 按钮BTN =(按钮)findViewById(R.id.btn01);
btn.setText(preSS我);
btn.setOnClickListener(本);
}@覆盖
公共无效的onClick(查看arg0中)
{
开关(arg0.getId()){
案例R.id.btn01:
ContentValues CV =新ContentValues();
cv.put(Event.FIRSTNAMEDATTA);
cv.put(Event.LASTNAME帕布);
。getContentResolver()插入(Event.CONTENT_URI,CV);
的String []海峡= {Event.FIRSTNAME,Event.LASTNAME};
字符串,其中= Event.FIRSTNAME +=?AND+
Event.LASTNAME +=?; 的String [] = whereArgs {达塔,帕布};
光标C = getContentResolver()查询(Event.CONTENT_URI,
STR,
哪里,
whereArgs,
空值);
如果(c.moveToFirst()){
INT ID = c.getInt(0);
串的firstName = c.getString(0);
串的lastName = c.getString(1);
编辑的EditText =(EditText上)findViewById(R.id.edit01);
edit.setText(ID +的firstName ++ lastName的);
TextView的电视=(的TextView)findViewById(R.id.text01);
tv.setText(ID +的firstName ++ lastName的);
} c.close();
打破;}
}
}
//main.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=
机器人:ID =@ + ID / text01
/>
<的EditText
机器人:ID =@ + ID / edit01
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
/>
<按钮
机器人:ID =@ + ID / btn01
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
/>
< / LinearLayout中>//R.java
包com.event.test; 最终大众R级{
公共静态final类ATTR {
}
公共静态final类绘制{
公共静态最终诠释图标= 0x7f020000;
}
公共静态最后的类ID {
公共静态最终诠释btn01 = 0x7f050002;
公共静态最终诠释edit01 = 0x7f050001;
公共静态最终诠释text01 =成0x7f050000;
}
公共静态最后的客舱布局{
公共静态最终诠释主要= 0x7f030000;
}
公共静态最后类字符串{
公共静态最终诠释APP_NAME = 0x7f040001;
公共静态最终诠释你好= 0x7f040000;
}
}
// logcat的
一月10日至4日:12:06.904:DEBUG / MediaScanner(285):总时间:981ms
一月10日至4日:12:06.984:DEBUG / MediaScannerService(285):完成扫描体积内
一月10日至4日:12:07.344:DEBUG / dalvikvm(216):GC_CONCURRENT释放243K,51%免费2798K / 5703K,外部410K / 517K,暂停为4ms + 3ms的
一月10日至4日:12:07.854:DEBUG / dalvikvm(178):GC_EXPLICIT释放331K,50%的自由2952K / 5895K,1313K外部/ 1400K,64ms的暂停
一月10日至4日:12:12.318:WARN / ActivityManager(73):活动破坏了HistoryRecord超时{4072bb18 com.event.test / .EventManagementActivity}
10-04 01:12:15.079:信息/ InputReader(73):设备重新配置:ID =为0x0,姓名= qwerty2,显示尺寸现在是240x432
一月10日至4日:12:15.079:信息/ InputManager-回调(73):发现设备qwerty2没有虚拟按键。
10-04 01:12:15.653:信息/ ARMAssembler(73):生成scanline__00000177:在1138243纳秒:03515104_00001001_00000000在[0x443256e8 0x44325520] [91 IPP](114项)
10-04 01:12:16.543:信息/ dalvikvm(73):日新:调整JitTable从512至1024
一月10日至4日:12:18.393:信息/流程(239):发送信号。 PID:239 SIG:9
一月10日至4日:12:18.403:信息/ ActivityManager(73):过程com.event.test(PID 239)已经死亡。
一月10日至4日:12:21.153:DEBUG / dalvikvm(178):GC_EXTERNAL_ALLOC释放61K,51%免费2947K / 5895K,1202K外部/ 1400K,暂停过48ms
一月10日至4日:12:24.723:信息/ ActivityManager(73):开始:意向{行动= android.intent.action.MAIN猫= [android.intent.category.LAUNCHER] FLG = 0x10200000 CMP = com.event.test /.EventManagementActivity}从PID 178
一月10日至4日:12:24.773:信息/ ActivityManager(73):启动PROC com.event.test的活动com.event.test / .EventManagementActivity:PID = 335 UID = 10034导报= {}
10-04 01:12:25.253:信息/ ARMAssembler(73):生成scanline__00000177:在551089纳秒:03515104_00001002_00000000在[0x443258a8 0x443256f0] [87 IPP](110项)
一月10日至4日:12:25.303:信息/ ActivityThread(335):酒吧com.event.data.contentprovider:com.event.test.EventContentProvider
一月10日至4日:12:25.583:DEBUG / AndroidRuntime(335):关闭VM
一月10日至4日:12:25.583:WARN / dalvikvm(335):主题ID = 1:螺纹未捕获的异常(组= 0x40015560)退出
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):致命异常:主要
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.event.test / com.event.test.EventManagementActivity}:显示java.lang.NullPointerException
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在android.os.Handler.dispatchMessage(Handler.java:99)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在android.os.Looper.loop(Looper.java:123)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在android.app.ActivityThread.main(ActivityThread.java:3683)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在java.lang.reflect.Method.invokeNative(本机方法)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在java.lang.reflect.Method.invoke(Method.java:507)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在dalvik.system.NativeStart.main(本机方法)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):致:显示java.lang.NullPointerException
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在com.event.test.EventManagementActivity.onCreate(EventManagementActivity.java:21)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
一月10日至4日:12:25.594:ERROR / AndroidRuntime(335):11 ...更多
一月10日至4日:12:25.603:WARN / ActivityManager(73):强制整理活动com.event.test / .EventManagementActivity
10-04 01:12:26.123:WARN / ActivityManager(73):为HistoryRecord活动暂停超时{4070aaa0 com.event.test / .EventManagementActivity}
一月10日至4日:12:27.503:信息/流程(335):发送信号。 PID:335 SIG:9
一月10日至4日:12:27.543:WARN / InputManagerService(73):窗口已经集中,忽视聚焦增益:com.android.internal.view.IInputMethodClient$Stub$Proxy@4073ba40
一月10日至4日:12:27.874:信息/ ActivityManager(73):过程com.event.test(PID 335)已经死亡。
一月10日至4日:12:36.575:WARN / ActivityManager(73):活动破坏了HistoryRecord超时{4070aaa0 com.event.test / .EventManagementActivity}
堆栈跟踪点,你到code导致该问题的确切行:
在com.event.test.EventManagementActivity.onCreate(EventManagementActivity.java:21)
虽然堆栈跟踪可以看看混乱,这是非常有用的。关键是看过去所有的Android班,并跟踪它归结为你的类:
com.event.test.EventManagementActivity.onCreate(EventManagementActivity.java:21)... 21指行21
空指针异常通常意味着东西没有实例化。调试,并逐步完成,检查你的变量到这一行。你引用的东西空。
I want to create a database for my android application. I have written the code for a database in my project; whenever I run it shows "force close" on the emulator. I have tried different ways ,but nothing works. Where did I go wrong?
// EventContentProvider.java
package com.event.test;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
public class EventContentProvider extends ContentProvider {
private static final String DATABASE_NAME = "event.db";
private static final UriMatcher sUriMatcher;
private static final int EVENTS = 1;
private static final int EVENT_ID = 2;
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE event1 (_ID INTEGER PRIMARY KEY, first_name TEXT, last_name TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
private DatabaseHelper mOpenHelper;
@Override
public boolean onCreate() {
mOpenHelper = new DatabaseHelper(getContext());
return true;
}
@Override
public int delete(Uri uri, String where, String[] whereArgs) {
SQLiteDatabase db = null;
int count = 0;
try {
db = mOpenHelper.getWritableDatabase();
switch(sUriMatcher.match(uri)){
case EVENTS:
count = db.delete(Event.EVENT_TABLE_NAME, where, whereArgs);
break;
default:
throw new IllegalArgumentException("No content matched");
}
getContext().getContentResolver().notifyChange(uri, null);
} catch(Exception e) {
} finally {
if(db != null)
db.close();
}
return count;
}
@Override
public String getType(Uri uri) {
switch(sUriMatcher.match(uri)){
case EVENTS:
return Event.CONTENT_TYPE;
default:
throw new IllegalArgumentException("No content matched");
}
}
@Override
public Uri insert(Uri uri, ContentValues initialValues) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
ContentValues values;
if (initialValues != null) {
values = new ContentValues(initialValues);
} else {
values = new ContentValues();
}
long rowId = -1;
Uri contentUri = null;
switch(sUriMatcher.match(uri)){
case EVENTS:
rowId = db.insertOrThrow(Event.EVENT_TABLE_NAME, Event._ID, values);
contentUri = ContentUris.withAppendedId(Event.CONTENT_URI, rowId);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
db.close();
if(rowId > 0){
return contentUri;
}
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
long rowId = -1;
try {
switch (sUriMatcher.match(uri)) {
case EVENTS:
rowId = db.update(Event.EVENT_TABLE_NAME, values, selection,
selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
rowId = db.update(Event.EVENT_TABLE_NAME, values, selection,
selectionArgs);
} catch (Exception e) {
} finally {
if(db != null)
db.close();
}
return (int) rowId;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
switch(sUriMatcher.match(uri)){
case EVENTS:
qb.setTables(Event.EVENT_TABLE_NAME);
qb.setProjectionMap(Event.sEventProjectionMap);
break;
default:
throw new IllegalArgumentException("No content matched in query ");
}
SQLiteDatabase db = null;
Cursor c = null;
try {
db = mOpenHelper.getReadableDatabase();
c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
} catch (Exception e) {
}
return c;
}
static {
sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sUriMatcher.addURI(Event.AUTHORITY, "events", EVENTS);
sUriMatcher.addURI(Event.AUTHORITY, "events/#", EVENT_ID);
}
}
//Event.java
package com.event.test;
import java.util.HashMap;
import android.net.Uri;
import android.provider.BaseColumns;
public class Event implements BaseColumns{
public static final String AUTHORITY = "com.event.data.contentprovider";
public static final Uri CONTENT_URI = Uri.parse("content://com.event.data.contentprovider/events");
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.event.event";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.event/vnd.event.event";
public static final String EVENT_TABLE_NAME = "event1";
public static final String FIRSTNAME = "first_name";
public static final String LASTNAME = "last_name";
public static String _ID;
public static HashMap<String,String> sEventProjectionMap;
static {
Event.sEventProjectionMap = new HashMap<String,String>();
Event.sEventProjectionMap.put(Event._ID, Event._ID);
Event.sEventProjectionMap.put(Event.FIRSTNAME, Event.FIRSTNAME);
Event.sEventProjectionMap.put(Event.LASTNAME, Event.LASTNAME);
}
}
//manifestfile.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.event.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".EventManagementActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:name="com.event.test.EventContentProvider"
android:authorities="com.event.data.contentprovider" />
</application>
</manifest>
//EventManagementActivity.java
package com.event.test;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class EventManagementActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btn01);
btn.setText("Press Me");
btn.setOnClickListener(this);
}
@Override
public void onClick(View arg0)
{
switch(arg0.getId()){
case R.id.btn01:
ContentValues cv = new ContentValues();
cv.put(Event.FIRSTNAME, "DATTA");
cv.put(Event.LASTNAME, "Prabhu");
getContentResolver().insert(Event.CONTENT_URI, cv);
String[] str = {Event.FIRSTNAME, Event.LASTNAME};
String where = Event.FIRSTNAME + " = ? AND " +
Event.LASTNAME + " = ?";
String [] whereArgs = {"Datta", "Prabhu"};
Cursor c = getContentResolver().query(Event.CONTENT_URI,
str,
where,
whereArgs,
null);
if (c.moveToFirst() ) {
int id = c.getInt(0);
String firstName = c.getString(0);
String lastName = c.getString(1);
EditText edit = (EditText) findViewById(R.id.edit01);
edit.setText( id + firstName + " " + lastName);
TextView tv = (TextView) findViewById(R.id.text01);
tv.setText(id + firstName + " " + lastName);
}
c.close();
break;
}
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/text01"
/>
<EditText
android:id="@+id/edit01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
//R.java
package com.event.test;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int btn01=0x7f050002;
public static final int edit01=0x7f050001;
public static final int text01=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
//logcat
10-04 01:12:06.904: DEBUG/MediaScanner(285): total time: 981ms
10-04 01:12:06.984: DEBUG/MediaScannerService(285): done scanning volume internal
10-04 01:12:07.344: DEBUG/dalvikvm(216): GC_CONCURRENT freed 243K, 51% free 2798K/5703K, external 410K/517K, paused 4ms+3ms
10-04 01:12:07.854: DEBUG/dalvikvm(178): GC_EXPLICIT freed 331K, 50% free 2952K/5895K, external 1313K/1400K, paused 64ms
10-04 01:12:12.318: WARN/ActivityManager(73): Activity destroy timeout for HistoryRecord{4072bb18 com.event.test/.EventManagementActivity}
10-04 01:12:15.079: INFO/InputReader(73): Device reconfigured: id=0x0, name=qwerty2, display size is now 240x432
10-04 01:12:15.079: INFO/InputManager-Callbacks(73): No virtual keys found for device qwerty2.
10-04 01:12:15.653: INFO/ARMAssembler(73): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x44325520:0x443256e8] in 1138243 ns
10-04 01:12:16.543: INFO/dalvikvm(73): Jit: resizing JitTable from 512 to 1024
10-04 01:12:18.393: INFO/Process(239): Sending signal. PID: 239 SIG: 9
10-04 01:12:18.403: INFO/ActivityManager(73): Process com.event.test (pid 239) has died.
10-04 01:12:21.153: DEBUG/dalvikvm(178): GC_EXTERNAL_ALLOC freed 61K, 51% free 2947K/5895K, external 1202K/1400K, paused 48ms
10-04 01:12:24.723: INFO/ActivityManager(73): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.event.test/.EventManagementActivity } from pid 178
10-04 01:12:24.773: INFO/ActivityManager(73): Start proc com.event.test for activity com.event.test/.EventManagementActivity: pid=335 uid=10034 gids={}
10-04 01:12:25.253: INFO/ARMAssembler(73): generated scanline__00000177:03515104_00001002_00000000 [ 87 ipp] (110 ins) at [0x443256f0:0x443258a8] in 551089 ns
10-04 01:12:25.303: INFO/ActivityThread(335): Pub com.event.data.contentprovider: com.event.test.EventContentProvider
10-04 01:12:25.583: DEBUG/AndroidRuntime(335): Shutting down VM
10-04 01:12:25.583: WARN/dalvikvm(335): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): FATAL EXCEPTION: main
10-04 01:12:25.594: ERROR/AndroidRuntime(335): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.event.test/com.event.test.EventManagementActivity}: java.lang.NullPointerException
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at android.os.Looper.loop(Looper.java:123)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at java.lang.reflect.Method.invoke(Method.java:507)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at dalvik.system.NativeStart.main(Native Method)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): Caused by: java.lang.NullPointerException
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at com.event.test.EventManagementActivity.onCreate(EventManagementActivity.java:21)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-04 01:12:25.594: ERROR/AndroidRuntime(335): ... 11 more
10-04 01:12:25.603: WARN/ActivityManager(73): Force finishing activity com.event.test/.EventManagementActivity
10-04 01:12:26.123: WARN/ActivityManager(73): Activity pause timeout for HistoryRecord{4070aaa0 com.event.test/.EventManagementActivity}
10-04 01:12:27.503: INFO/Process(335): Sending signal. PID: 335 SIG: 9
10-04 01:12:27.543: WARN/InputManagerService(73): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4073ba40
10-04 01:12:27.874: INFO/ActivityManager(73): Process com.event.test (pid 335) has died.
10-04 01:12:36.575: WARN/ActivityManager(73): Activity destroy timeout for HistoryRecord{4070aaa0 com.event.test/.EventManagementActivity}
The stack trace points you to the exact line of code that is causing the problem:
at com.event.test.EventManagementActivity.onCreate(EventManagementActivity.java:21)
While the stack trace can look confusing, it's very useful. The key is to look past all of the Android classes, and trace it down to your class:
com.event.test.EventManagementActivity.onCreate(EventManagementActivity.java:21) ... the 21 means line 21.
The null pointer exception usually means something is not instantiated. Debug, and step through to check your variables up to this line. You are referencing something null.
这篇关于在Eclipse Android SDK中内容提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!