简体   繁体   English

无法证实活动ComponentInfo - 空指针异常

[英]Unable to substantiate activity ComponentInfo - Null Pointer Exception

I have spent the day trying to fathom this one out (needless to say I am a long way down the learnign curve - hence my need to ask for help!) - below is a simple database program (I'm trying to understand how they work to implement into another app i am working on). 我花了一天时间试图理解这一点(不用说我在学习曲线上走了很长一段路 - 因此我需要求助!) - 下面是一个简单的数据库程序(我试图理解他们是怎样的努力实现我正在努力的另一个应用程序)。

When I click the 'Show' Button - i get the following Run Time Error: 当我单击“显示”按钮时 - 我得到以下运行时错误:

Log Cat 记录猫

03-14 16:13:39.612: ERROR/AndroidRuntime(341): FATAL EXCEPTION: main
03-14 16:13:39.612: ERROR/AndroidRuntime(341): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.dbtest/com.android.dbtest.ShowActivity}: java.lang.NullPointerException
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at android.os.Looper.loop(Looper.java:123)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.main(ActivityThread.java:3647)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at java.lang.reflect.Method.invoke(Method.java:507)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at dalvik.system.NativeStart.main(Native Method)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): Caused by: java.lang.NullPointerException
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at android.app.Activity.findViewById(Activity.java:1647)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at com.android.dbtest.ShowActivity.<init>(ShowActivity.java:12)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at java.lang.Class.newInstanceImpl(Native Method)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at java.lang.Class.newInstance(Class.java:1409)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536)
03-14 16:13:39.612: ERROR/AndroidRuntime(341):     ... 11 more

My Main activity: 我的主要活动:

package com.android.dbtest;




import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;


public class DatabaseTestActivity extends Activity {
/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    }

    public void handleClick(View v){
        Intent intent = new Intent();
        intent.setClass(this, AddActivity.class);
        startActivity(intent);
    }

    public void showHandleClick(View v){
        Intent intent = new Intent();
        intent.setClass(this, ShowActivity.class);
        startActivity(intent);
    }
    }

My Show Activity (this is where i think the main error (guessing more than one) probably lies, as it is when i press the show button , that the runtime error occurs, i believe it is related to building the List): 我的显示活动(这是我认为主要错误(猜测不止一个)可能存在的地方,因为当我按下显示按钮时,发生运行时错误,我相信它与构建List有关):

package com.android.dbtest;

import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.util.Log;

public class ShowActivity extends DatabaseTestActivity{

private SQLiteAdapter mySQLiteAdapter;
ListView listContent = (ListView)findViewById(R.id.contentlist);

//TextView listContent = (TextView) findViewById(R.id.contentlist);

SimpleCursorAdapter cursorAdapter;
String cursor;

public static final String LOG_TAG = "dbtest";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show);
    // TODO Auto-generated method stub

    Log.v(LOG_TAG,  "step 1");


    //output.setText(String.valueOf( WHAT I GET FROM DATABASE));


    mySQLiteAdapter = new SQLiteAdapter(this);
    mySQLiteAdapter.openToRead();

    Cursor cursor = mySQLiteAdapter.queueAll();
    startManagingCursor(cursor);
    String[] from = new String[]{SQLiteAdapter.KEY_CONTENT};
    int[] to = new int[] {R.id.text};

    Log.v(LOG_TAG, "from value:" + from );
    Log.v(LOG_TAG, "to value:" + to );

    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
    listContent.setAdapter(cursorAdapter);

    mySQLiteAdapter.close();

}
}

My SQLiteAdapter: 我的SQLiteAdapter:

package com.android.dbtest;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class SQLiteAdapter { 
public static final String MYDATABASE_NAME = "MY_DATABASE"; 
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1; 
public static final String KEY_CONTENT = "Content"; 
public static final String KEY_ID = "_id";          //primary key


//create table MY_DATABASE (ID integer primary key, Content text not null); 
private static final String SCRIPT_CREATE_DATABASE =  "create table " 
+ MYDATABASE_TABLE + " (" 
+ KEY_ID + " integer primary key autoincrement, "           
+ KEY_CONTENT + " text not null "  +
        ");"; 

private SQLiteHelper sqLiteHelper; 
private SQLiteDatabase sqLiteDatabase;
private Context context;  
public SQLiteAdapter(Context c)
{  
    context = c;
}  

public SQLiteAdapter openToRead() throws android.database.SQLException 
{ 
    sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); 
    sqLiteDatabase = sqLiteHelper.getReadableDatabase();
    return this; 
} 
public SQLiteAdapter openToWrite() throws android.database.SQLException {
    sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
    sqLiteDatabase = sqLiteHelper.getWritableDatabase();  return this;
    }
public void close(){ 
    sqLiteHelper.close();
    } 
public long insert(String content){
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_CONTENT, content); 
    return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
    }  

public int deleteAll(){ 
    return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null); 
    } 



public Cursor queueAll(){
    String[] columns = new String[]{KEY_ID, KEY_CONTENT};
    Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,     null, null, null, null, null); 
    return cursor;
}


public class SQLiteHelper extends SQLiteOpenHelper 
{  
    public SQLiteHelper(Context context, String name,    CursorFactory factory, int version) 
    {  
        super(context, name, factory, version); 
        }
    @Override  public void onCreate(SQLiteDatabase db)
    {   
        // TODO Auto-generated method stub 
        db.execSQL(SCRIPT_CREATE_DATABASE); 
        } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {  
        // TODO Auto-generated method stub  } } }
}
}
}

finally the XML file for Show: 最后是Show的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_height="wrap_content" 
   android:text="TextView" 
   android:textAppearance="?android:attr/textAppearanceLarge" 
   android:layout_width="wrap_content" 
   android:id="@+id/textView1"></TextView>    

   <ListView android:id="@android:id/android:list"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"/>

 </LinearLayout>

You can not do like that as static initializer in your activity: 您不能在活动中将其作为静态初始值设定项:

ListView listContent = (ListView)findViewById(R.id.contentlist);

The reason is that you have nto yet loaded the layout for your activity and thus this call will fail findViewById(R.id.contentlist) . 原因是你已经为你的活动加载了nto,因此这个调用将失败findViewById(R.id.contentlist) Your error comes from the class static initializers, since you do not declare constructor of your class, still get error in the <init>. 您的错误来自类静态初始化程序,因为您没有声明类的构造函数,仍然会在<init>.得到错误<init>.

Fix: Initialize the list view in the onCreate , after you call setContentView(R.layout.show); 修复:调用setContentView(R.layout.show);后,在onCreate初始化列表视图setContentView(R.layout.show); .

Your ShowActivity activity has a layout containing a ListView with an id @android:id/list but you search for the listView with the id R.id.contentlist . 您的ShowActivity活动有一个包含带有id @android:id/listListView的布局,但您搜索带有ID为R.id.contentlist的listView。 Also you must initialize your views in the onCreate() method after you set the layout. 此外,您必须在设置布局后在onCreate()方法中初始化视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法实例化活动ComponentInfo空指针异常 - Unable to instantiate activity ComponentInfo Null Pointer Exception android无法启动活动ComponentInfo NULL Point异常 - android Unable to start activity ComponentInfo NULL Point Exception FATAL异常无法启动活动ComponentInfo - FATAL Exception Unable to start activity ComponentInfo 无法通过SQL异常启动活动ComponentInfo - Unable to start activity ComponentInfo with sql exception 无法恢复活动NULL POINTER EXCEPTION&错误夸大类片段? - unable to resume activity NULL POINTER EXCEPTION & Error inflating class fragment? 邮件发件人活动:无法启动活动ComponentInfo和Resources $ NotFound异常 - Mail sender activity: Unable to start activity ComponentInfo and Resources$NotFound Exception 无法启动活动ComponentInfo - Unable to start activity ComponentInfo 无法启动活动ComponentInfo | E / AndroidRuntime:致命异常 - Unable to start activity ComponentInfo | E/AndroidRuntime: FATAL EXCEPTION Android - FATAL EXCEPTION:main - 无法启动活动ComponentInfo NullPointerException - Android - FATAL EXCEPTION: main - Unable to start activity ComponentInfo NullPointerException 导入的项目立即崩溃-&gt; FATAL EXCEPTION:…无法实例化活动ComponentInfo - Imported project crashes immediately -> FATAL EXCEPTION: …Unable to instantiate activity ComponentInfo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM