我正在尝试开发一个应用程序,它根据微调器值对数据库执行查询,然后将其传递给webview中的下一个意图。由于某种原因,它不断崩溃的活动。这是我的代码:
//import android.R;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class Monthly extends Activity{
DatabaseAdapter dbAdapter;
private Spinner emonth;
private Spinner eyear;
private Button submit;
private double price1, price2, price3, price4, price5, price6, price7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monthly);
emonth = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.month, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
emonth.setAdapter(adapter);
eyear = (Spinner)findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.years, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
eyear.setAdapter(adapter2);
submit = (Button) findViewById(R.id.button1);
submit.setOnClickListener(submitOnClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
OnClickListener submitOnClickListener =
new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.button1)
{
String month=emonth.getSelectedItem().toString();
String year=eyear.getSelectedItem().toString();
dbAdapter.open();
Cursor price1=dbAdapter.queryFoodMonth(month, year);
Cursor price2=dbAdapter.queryClothingMonth(month, year);
Cursor price3=dbAdapter.queryElectronicsMonth(month, year);
Cursor price4=dbAdapter.queryUtilityMonth(month, year);
Cursor price5=dbAdapter.queryEntertainmentMonth(month, year);
Cursor price6=dbAdapter.queryFuelMonth(month, year);
Cursor price7=dbAdapter.queryOtherMonth(month, year);
dbAdapter.close();
Intent intent = new Intent(Monthly.this,WebChart.class);
intent.putExtra("PRICE1", getNum(price1));
intent.putExtra("PRICE2", getNum(price2));
intent.putExtra("PRICE3", getNum(price3));
intent.putExtra("PRICE4", getNum(price4));
intent.putExtra("PRICE5", getNum(price5));
intent.putExtra("PRICE6", getNum(price6));
intent.putExtra("PRICE7", getNum(price7));
startActivity(intent);
}
}
};
private double getNum(Cursor price){
double num = 0.0;
String stringNum = price.toString();
if(!stringNum.equals("")){
num = Integer.valueOf(stringNum);
}
return (num);
}
}
这是数据库适配器中提供光标的代码。
public Cursor queryFoodMonth(String month,String year)
{
Cursor c1 = database.rawQuery("SELECT SUM(price) FROM items where month= \" "+month+" \" AND year= \" "+year+" \" AND category = \"Food\";",null);
if (c1 !=null) {
c1.moveToFirst();
}
return c1;
}
public Cursor queryClothingMonth(String month,String year)
{
Cursor c2 = database.rawQuery("SELECT SUM(price) FROM items where month= \" "+month+" \" AND year= \" "+year+" \" AND category = \"Clothing\";",null);
if (c2 !=null) {
c2.moveToFirst();
}
return c2;
}
public Cursor queryElectronicsMonth(String month,String year)
{
Cursor c3 = database.rawQuery("SELECT SUM(price) FROM items where month= \" "+month+" \" AND year= \" "+year+" \" AND category = \"Electronics\";",null);
if (c3 !=null) {
c3.moveToFirst();
}
return c3;
}
public Cursor queryUtilityMonth(String month,String year)
{
Cursor c4 = database.rawQuery("SELECT SUM(price) FROM items where month= \" "+month+" \" AND year= \" "+year+" \" AND category = \"Utility\";",null);
if (c4 !=null) {
c4.moveToFirst();
}
return c4;
}
public Cursor queryEntertainmentMonth(String month,String year)
{
Cursor c5 = database.rawQuery("SELECT SUM(price) FROM items where month= \" "+month+" \" AND year= \" "+year+" \" AND category = \"Entertainment\";",null);
if (c5 !=null) {
c5.moveToFirst();
}
return c5;
}
public Cursor queryFuelMonth(String month,String year)
{
Cursor c6 = database.rawQuery("SELECT SUM(price) FROM items where month= \" "+month+" \" AND year= \" "+year+" \" AND category = \"Fuel\";",null);
if (c6 !=null) {
c6.moveToFirst();
}
return c6;
}
public Cursor queryOtherMonth(String month,String year)
{
Cursor c7 = database.rawQuery("SELECT SUM(price) FROM items where month= \" "+month+" \" AND year= \" "+year+" \" AND category = \"Other\";",null);
if (c7 !=null) {
c7.moveToFirst();
}
return c7;
}
这是logcat错误报告:
12-03 17:31:25.632: E/AndroidRuntime(1993): FATAL EXCEPTION: main
12-03 17:31:25.632: E/AndroidRuntime(1993): Process: com.csj.monthlybudg, PID: 1993
12-03 17:31:25.632: E/AndroidRuntime(1993): java.lang.NullPointerException
12-03 17:31:25.632: E/AndroidRuntime(1993): at com.csj.monthlybudg.Monthly$1.onClick(Monthly.java:78)
12-03 17:31:25.632: E/AndroidRuntime(1993): at android.view.View.performClick(View.java:4438)
12-03 17:31:25.632: E/AndroidRuntime(1993): at android.view.View$PerformClick.run(View.java:18422)
12-03 17:31:25.632: E/AndroidRuntime(1993): at android.os.Handler.handleCallback(Handler.java:733)
12-03 17:31:25.632: E/AndroidRuntime(1993): at android.os.Handler.dispatchMessage(Handler.java:95)
12-03 17:31:25.632: E/AndroidRuntime(1993): at android.os.Looper.loop(Looper.java:136)
12-03 17:31:25.632: E/AndroidRuntime(1993): at android.app.ActivityThread.main(ActivityThread.java:5001)
12-03 17:31:25.632: E/AndroidRuntime(1993): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 17:31:25.632: E/AndroidRuntime(1993): at java.lang.reflect.Method.invoke(Method.java:515)
12-03 17:31:25.632: E/AndroidRuntime(1993): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-03 17:31:25.632: E/AndroidRuntime(1993): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-03 17:31:25.632: E/AndroidRuntime(1993): at dalvik.system.NativeStart.main(Native Method)
我可以看到错误在第78行,在打开数据库连接但我不明白错误的时候。任何帮助都会非常感激,如果你发现我的问题天真,请不要苛刻我是android和java的新手。 谢谢!
答案 0 :(得分:0)
在致电dbAdapter = new DatabaseAdapter()
dbAdapter.open();
之类的操作
当您尝试使用在内存中指向任何内容的引用(即,您尚未为引用指向任何内容创建任何内容)时,通常会出现空指针异常。