无法启动组件活动组件信息空指针异常

无法启动组件活动组件信息空指针异常

本文介绍了无法启动组件活动组件信息空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何找到在logcat的输出的行号。另外,在该最新的Andr​​oid Studio版本,正规的logcat不工作,所以我要开始了Android设备监视器来获得看看吧。我只能捡个PNG,对于如此诚挚的歉意。

I'm not sure how to find the line number in the Logcat output. Also, with this latest Android Studio version, the regular logcat doesn't work, so I have to start the Android Device Monitor to get a look at it. And I can only snag a png, so sincere apologies for that.

下面是MainActivity类别:

Here's the MainActivity class:

public class MainActivity extends Activity {

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.listViewCategories);
    String[] categories = getResources().getStringArray(R.array.categories_array);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, categories);

    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // ListView Clicked item index
            int itemPosition = position;
            // ListView Clicked item value
            String itemValue = (String) listView.getItemAtPosition(position);
            // Show Alert
            /*Toast.makeText(getApplicationContext(),
                    "Position: " + itemPosition + "  List Item: " + itemValue,
                    Toast.LENGTH_LONG)
                    .show();*/
            Intent intent = new Intent(MainActivity.this, DisplayResult.class);
            intent.putExtra("ITEMVALUE", itemValue);
            startActivity(intent);
        }


    });
}

}

下面是第二个活动,DisplayResult:

Here's the second activity, DisplayResult:

public class DisplayResult extends Activity {

String selectedCategory;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.verses);

    Bundle b = getIntent().getExtras();
    String selectedCategory = b.getString("ITEMVALUE");

    //Toast.makeText(DisplayResult.this, "Category passed: " + selectedCategory,
    // Toast.LENGTH_LONG).show();

    final ListView lv = (ListView) findViewById(R.id.listViewItems);
    ArrayList<Promise> results = null;
    lv.setAdapter(new MyCustomArrayAdapter(this, results));
} // end onCreate

public ArrayList<Promise> GetPromises() {
    String table = "promises";
    ArrayList<Promise> promises = new ArrayList<Promise>();
    Promise prms = new Promise();
    SQLiteDatabase newDB;
    //SQLiteDatabase db = MyDBHandler.getReadableDatabase();

    //ArrayList<Promise> results = null;
    ArrayList<Promise> results = new ArrayList<Promise>();

    try {
        MyDBHandler dbHandler = new MyDBHandler(this.getApplicationContext());
        newDB = dbHandler.getReadableDatabase();
        Cursor c = newDB.rawQuery("SELECT * FROM " + MyDBHandler.TABLE_NAME +
                " WHERE KEY_CATEGORY = ?", new String[]{selectedCategory});

        if (c != null) {
            if (c.moveToFirst()) {
                do {
                    String category = c.getString(c.getColumnIndex("KEY_CATEGORY"));
                    String book = c.getString(c.getColumnIndex("KEY_BOOK"));
                    String chapter = c.getString(c.getColumnIndex("KEY_CHAPTER"));
                    String verse = c.getString(c.getColumnIndex("KEY_VERSE"));
                    String word = c.getString(c.getColumnIndex("KEY_WORD"));
                    Promise promise = new Promise();
                    /*promise.add(Strings)
                    results.add(promise)*/
                    results.add(new Promise(category, book, chapter, verse, word));
                } while (c.moveToNext());
            } // end inner if
        } // end outer if
    } catch (SQLiteException se) {
        Log.e(getClass().getSimpleName(), "Could not create or open the database");
    } finally {

    }
    return results;
}

}

这里的DBHandler类:

Here's the DBHandler class:

public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "promisesdatabase.sqlite";

public static final String TABLE_NAME = "promises";

public static final String KEY_ROWID = "id";
public static final String KEY_CATEGORY = "category";
public static final String KEY_BOOK = "book";
public static final String KEY_CHAPTER = "chapter";
public static final String KEY_VERSE = "verse";
public static final String KEY_WORD = "word";

public MyDBHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
     String DATABASE_CREATE =
            "CREATE TABLE if not exists " + TABLE_NAME + " (" +
                    KEY_ROWID + "integer PRIMARY KEY," +
                    KEY_CATEGORY + " TEXT," +
                    KEY_BOOK + " TEXT," +
                    KEY_CHAPTER + " TEXT," +
                    KEY_VERSE + " TEXT," +
                    KEY_WORD + " TEXT" + ")";

     db.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

这里的CustomArrayAdapter类:

Here's the CustomArrayAdapter class:

public class MyCustomArrayAdapter extends ArrayAdapter {

private static ArrayList<Promise> promiseArrayList;

private LayoutInflater mInflater;

public MyCustomArrayAdapter(Context context, ArrayList<Promise> results) {
    super(context, 0, results);
    promiseArrayList = results;
    mInflater = LayoutInflater.from(context);
}
public int getCount() {
    return promiseArrayList.size();
}
public Object getItem(int position) {
    return promiseArrayList.get(position);
}
public long getItemId(int position) {
    return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    // Promise Promise = getItem(position); took out due to other blog geekswithblogs
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder holder; // view lookup cache stored in tag
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.verses, null);
        holder = new ViewHolder();
        //LayoutInflater inflater = LayoutInflater.from(getContext());
        //convertView = inflater.inflate(R.layout.verses, parent, false);
        holder.txtcategory = (TextView) convertView.findViewById(R.id.txtViewCategory);
        holder.txtbook = (TextView) convertView.findViewById(R.id.txtViewBook);
        holder.txtchapter = (TextView) convertView.findViewById(R.id.txtViewChapter);
        holder.txtverse = (TextView) convertView.findViewById(R.id.txtViewVerse);
        holder.txtword = (TextView) convertView.findViewById(R.id.txtViewWord);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    // Populate the data into the template view using the data object
    holder.txtcategory.setText(promiseArrayList.get(position).getCategory());
    holder.txtbook.setText(promiseArrayList.get(position).getBook());
    holder.txtchapter.setText(promiseArrayList.get(position).getChapter());
    holder.txtverse.setText(promiseArrayList.get(position).getVerse());
    holder.txtword.setText(promiseArrayList.get(position).getWord());
    // Return the completed view to render on screen
    return convertView;
}
private static class ViewHolder {
    TextView id;
    TextView txtcategory;
    TextView txtbook;
    TextView txtchapter;
    TextView txtverse;
    TextView txtword;
}

还有一个承诺类(数据模型),但我不认为有必要发布。

There is also a Promise class (the data model) but I didn't think it necessary to post.

这里的logcat的PNG

Here's the logcat png

推荐答案

我可能是错的,但应行

ArrayList<Promise> results = null;

不读

ArrayList<Promise> results = GetPromises();

这个问题似乎是,你在DisplayResult的ListView尝试使用从MyCustomAdapter,正在给予承诺的有效ArrayList的空,而不是要显示的数据。

The issue seems to be that your ListView in DisplayResult is trying to display using the data from your MyCustomAdapter, which is being given null instead of a valid ArrayList of Promises.

这篇关于无法启动组件活动组件信息空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 21:51