Android Studio中的小部件字符序列错误

时间:2015-07-18 03:03:36

标签: java android database sqlite

我是一个非常愚蠢的新手android程序员, 在这里我有一些问题在我的情况下,我想运行我在Android Studio IDE上制作的Android程序。 但是当我运行程序时,错误字段说   "处理:com.example.ever_ncn.cashflow,PID:6317     java.lang.NullPointerException:尝试调用虚方法' void android.widget.TextView.setText(java.lang.CharSequence)'在空对象引用"

我仍然不理解另一个问题的解释。

我很困惑这个错误,我谷歌搜索它,但它仍然没有解决。 请掌握,用简单易懂的解释回答我的问题。 谢谢。

NB。我的主要活动是显示一个包含微调器的活动,该微调器是从数据库中获取的微调器值。 这是Main_Activity.java的源代码:

public class MainActivity extends ActionBarActivity {

private static Button BtnINewTrans;
private static Button BtnIViewCash;
private static Button BtnIAddCateg;
DatabaseHelper dbHelper = new DatabaseHelper(this);
Spinner selectCategory;
ArrayAdapter<String> adapterCategory;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    onButtonClickButtonListener();
    select_spinner_Category();


}

public void select_spinner_Category (){
    ArrayList<String> AllCategoryListing= dbHelper.getAllCategory();
    selectCategory = (Spinner) findViewById(R.id.spnCategSelect);
    adapterCategory = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.id.spnCategSelect, AllCategoryListing);
    adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    selectCategory.setAdapter(adapterCategory);
    selectCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
            String currencyValue = String.valueOf(parent.getSelectedItem());
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    return true;
}

public void onButtonClickButtonListener(){
    BtnINewTrans = (Button)findViewById(R.id.btnNewTrans);
    BtnINewTrans.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intentNewTrans = new Intent ("com.example.ever_ncn.cashflow.NewTransaction");
                    startActivity(intentNewTrans);
                }
            }
    );

    BtnIViewCash = (Button)findViewById(R.id.btnViewCashflow);
    BtnIViewCash.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intentViewCash = new Intent ("com.example.ever_ncn.cashflow.ViewCashflow");
                    startActivity(intentViewCash);
                }
            }
    );

    BtnIAddCateg = (Button)findViewById(R.id.btnAddCateg);
    BtnIAddCateg.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intentAddCateg = new Intent ("com.example.ever_ncn.cashflow.AddCategory");
                    startActivity(intentAddCateg);
                }
            }
    );

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

这个用于database_helper.java:

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String MyVillageSoftware = "MyVillageSoftware";
public static final String DATABASE_NAME = "Cashflow.db";
public static final String TABLE_Categ_NAME = "category_table";
public static final String COL1 = "CategId";
public static final String COL2 = "CategName";
public static final String COL3 = "Note";
public static final String COL4 = "Currency";



public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 2);


}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("Create table " + TABLE_Categ_NAME +
            " (CategID Integer PRIMARY KEY AUTOINCREMENT, " +
            "CategName Text," +
            " Note Text," +
            " Currency Text)");

}

public boolean insertCategData(String categname, String note, String currency){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, categname);
    contentValues.put(COL3, note);
    contentValues.put(COL4, currency);
    long result = db.insert(TABLE_Categ_NAME, null, contentValues);
     if (result == -1)
         return true;
     else
         return false;

}
public ArrayList<String>getAllCategory(){
    ArrayList<String> AllCategoryList = new ArrayList<String>();
    SQLiteDatabase db = this.getReadableDatabase();
    String selectCateg="Select * FROM " +TABLE_Categ_NAME;
    Cursor cursor = db.rawQuery(selectCateg, null);
    if(cursor.getCount()>0){
        while (cursor.moveToNext()){
            String categname=cursor.getString(cursor.getColumnIndex(COL2));
            AllCategoryList.add(COL2);

        }return AllCategoryList;
    }

    return AllCategoryList;

}


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

}

}

1 个答案:

答案 0 :(得分:0)

基本上错误在错误消息中解释。学习编程包括学习阅读错误信息。

您正在TextView对象上调用setText()方法,除了您调用它的TextView不存在(在内存中,因为它没有被分配)。不知道它报告的是哪一行错误来自我无法帮助超越这一点。我建议调试它并检查在它们不应该出现时变为空的对象。