我在加载其他活动时遇到问题。单击btnHerb
和btnPrep
时,应用程序将停止。任何人都可以确定我的错误吗?
这是我的MainActivity.java类。
package com.apps.mangkepwengslab;
import com.apps.mangkepwengslab.MainActivity;
import com.apps.mangkepwengslab.ListHerb;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
final Context context=this;;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSearch= (Button) findViewById(R.id.btnSearch1);
Button btnHerb = (Button) findViewById(R.id.btnHerbal);
Button btnPrep= (Button) findViewById(R.id.btnPrep);
Button btnAbout = (Button) findViewById(R.id.btnAbouts);
Button btnExit = (Button) findViewById(R.id.btnExit);
//Create on click listener class
btnAbout.setOnClickListener(new View.OnClickListener() {
//When you click the button, Alert dialog will be showed
public void onClick(View v) {
/* Alert Dialog Code Start*/
abt();
/* Alert Dialog Code End*/
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
//When you click the button, Alert dialog will be showed
public void onClick(View v) {
/* Alert Dialog Code Start*/
alert();
/* Alert Dialog Code End*/
}
});
btnHerb.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent goToList= new Intent(MainActivity.this,ListHerb.class);
startActivity(goToList);
MainActivity.this.finish();
}
});
btnPrep.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent goToList= new Intent(MainActivity.this,PrepUse.class);
startActivity(goToList);
MainActivity.this.finish();
}
});
}
public void alert(){
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("EXIT"); //Set Alert dialog title here
alert.setMessage("Do you want to close the application?"); //Message here
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Close the Activity when click OK.
//Cavspot.this.finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Cancelled.
dialog.cancel();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
public void abt(){
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("About Mang Kepweng's Lab"); //Set Alert dialog title here
alert.setMessage("Mang Kepweng's Lab is a mobile android information system on medicinal plants. This application includes the most commonly used medicinal plants in the Philippines with its scientific name, description, taxonomical information, medicinal use, how to use and images.\n\n" +
"This application was developed by Mac John T. Poblete, Janila Aleli dela Peña and Janna Ria B. Novero, from Cavite State University - Naic. \n\n" +
"All rights reserved, 2014 "); //Message here
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
@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;
}
}
这是我的ListHerb.java类
package com.apps.mangkepwengslab;
import java.util.ArrayList;
import java.util.List;
import com.apps.mangkepwengslab.DBHelper;
import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class ListHerb extends ListActivity {
TextView selection;
public int idToModify;
DBHelper dbHerb;
List<String[]> list = new ArrayList<String[]>();
List<String[]> herb =null ;
String[] stg1;
private ListView lstHerb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_herb);
dbHerb = new DBHelper(this);
herb = dbHerb.selectAll();
stg1=new String[herb.size()];
int x=0;
String stg;
for (String[] name: herb) {
stg = name[1];
stg1[x]=stg;
x++;
}
lstHerb = (ListView) findViewById(R.id.lstHerb);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stg1);
this.setListAdapter(adapter);
lstHerb.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list_herb, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
最后这是我的数据库Helper类(DBHelper.java):
package com.apps.mangkepwengslab;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.apps.mangkepwengslab.DBHelper;
import com.apps.mangkepwengslab.ListHerb;
import com.apps.mangkepwengslab.DBHelper.OpenHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHelper {
private static final String DATABASE_NAME = "dbHerbal.db";
private static final int DATABASE_VERSION = 1;
static final String TABLE_NAME = "tblHerbal";
static Context context;
static SQLiteDatabase dbHerb;
private static final String TAG="NotesDbAdapter";
public DBHelper(Context context) {
DBHelper.context = context;
OpenHelper dbHelper = new OpenHelper(DBHelper.context);
DBHelper.dbHerb = dbHelper.getWritableDatabase();
}
public void deleteAll() {
dbHerb.delete(TABLE_NAME, null, null);
}
public List<String[]> selectAll()
{
List<String[]> list = new ArrayList<String[]>();
//Cursor cursor = db.query(TABLE_NAME, null, "cat =" + "Churches" , null, null, null, null);
List<String[]> v = null;
String[] stg1;
v = temp();
stg1=new String[v.size()];
int m=0;
String stg;
for (String[] name : v) {
stg = name[0];
stg1[m]=stg;
m++;
}
String l = stg1[0];
Log.e("L:", l);
Cursor cursor = dbHerb.rawQuery("SELECT * from newtable WHERE cat = '" + l + "'", null);
int x=0;
if (cursor.moveToFirst()) {
do {
String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),
cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)};
list.add(b1);
x=x+1;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
cursor.close();
Log.e("SIZE: ", String.valueOf(list.size()));
return list;
}
public List<String[]> fetch()
{
List<String[]> list = new ArrayList<String[]>();
List<String[]> v = null;
String[] stg1;
v = temp();
stg1=new String[v.size()];
int m=0;
String stg;
for (String[] name : v) {
stg = name[0];
stg1[m]=stg;
m++;
}
String l = stg1[0];
Log.e("L:", l);
Cursor cursor = dbHerb.rawQuery("SELECT * from newtable WHERE sName = '" + l + "'", null);
int x=0;
if (cursor.moveToFirst()) {
do {
String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),
cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6),cursor.getString(7)};
list.add(b1);
x=x+1;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
cursor.close();
Log.e("SIZE: ", String.valueOf(list.size()));
return list;
}
public List<String[]> getAllLabels()
{
List<String[]> list = new ArrayList<String[]>();
Cursor cursor = dbHerb.rawQuery("SELECT * from category", null);
int x=0;
if (cursor.moveToFirst()) {
do {
String[] b1=new String[]{cursor.getString(0)};
list.add(b1);
x=x+1;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
cursor.close();
return list;
}
public List<String[]> temp()
{
List<String[]> list = new ArrayList<String[]>();
Cursor cursor = dbHerb.rawQuery("SELECT * from temp", null);
int x=0;
if (cursor.moveToFirst()) {
do {
String[] b1=new String[]{cursor.getString(0)};
list.add(b1);
x=x+1;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
cursor.close();
return list;
}
public void delete(int rowId) {
dbHerb.delete(TABLE_NAME, null, null);
}
public static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
private void myInsert(String sName, String cName, String fName, String oName, String img1, String img2, String kingdom, String phylum, String cLass, String order, String family, String genus, String desc, String mUse, String prep, String warn, String ref, SQLiteDatabase db){
db.execSQL("insert into " + TABLE_NAME + " (sName, cName, fName, oName, img1, img2, kingdom, phylum, class, order, family, genus, desc, mUse, prep,warn) values ('"+ sName + "','" + cName + "','" + fName + "','" + oName + "','" + img1 + "','" + img2 + "','" + kingdom + "','" + phylum +"','" + cLass + "','" + order + "','" + family + "','" + genus + "','" + desc + "','" + mUse + "','" + prep + "','" + warn + "','" + ref + "') ");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER AUTOINCREMENT, sName PRIMARY KEY TEXT NOT NULL, cName TEXT NOT NULL, fName TEXT NOT NULL, oName TEXT, img1 TEXT NOT NULL, img2 TEXT NOT NULL, kingdom TEXT NOT NULL, phylum TEXT NOT NULL, class TEXT NOT NULL, order TEXT, family TEXT NOT NULL, genus TEXT NOT NULL, desc TEXT NOT NULL, mUse TEXT NOT NULL, prep TEXT NOT NULL, warn TEXT, ref TEXT NOT NULL)");
Log.d(TAG, "Database has been created");
myInsert ("Acacia nilotica Linn.", "Acacia", "Acacia", "Acacia De Cayenne, Acacia, Babul, Babul Acacia, Black Piquant, Casha, Cassie, Cassie á Piquants Blancs, Egyptian Acacia, Egyptian Mimosa, Egyptian Thorn, Goma Ar, Gum Arabic Tree, Indian Gum-Arabic-Tree, Mgunga, Motse, Piquant Lulu, Prickly Acacia, Scented-Thorn, Thorn-Mimosa, Thorny Acacia", "acacia1", "acacia2", "Plantae", "Tracheophyta", "Spermatopsida", "Fabales", "Leguminosae", "Acacia",
"The acacia or Babul tree is an average size tree that is common to india and africa. It is quite easily distinguishable by the dark brown or black, longitudinally fissured bark. It belongs to the plant family mimosaceae. The leaves are bipinnately compound and 5-10 cm long. The plant has a good defensive mechanism in its V-shaped thorns that are white or light grey in colour. The globose flowers are yellow and the pods are light grey in colour. The pods, 7.5-15 cm. long are compressed and constricted at the sutures between the seeds, which range from 8-12 in a single pod.",
"1. Burns \n 2. Wounds \n 3. Anemia \n 4. Yellow stained teeth \n 5. Blood dysentery \n 6. Gum diseases, loose teeth, and ulcers in the mouth 7. Cough 8. Eczema \n 9. Cough, diabetes, excessive urination, impotency, and throat infection \n 10. Conjunctivitis and sore eyes.",
"1. To take care of burns, mix the gum that oozes from the tree with equal quantities of powdered turmeric(Curcuma domestica) and coconut(Cocos nucifera) oil and apply. \n 2. For superficial wounds, boil some crushed bark in water and use the decoction for washing the wounds. Grind the tender leaves of the tree into a very fine paste. Apply over the wounds. You could also dust the wounds with some finely powdered bark. \n 3. For cases of anemia, fry one teaspoon each of the gum from the tree along with sweet basil (Ocimum basilicum) and purslane (Portulaca oleracea) in an adequate quantity of olive oil or gingilee oil for 10 minutes. Remove and soak in 1 cup rose water for one hour. Use one teaspoon three times a day. \n 4. To take care of yellow stained teeth, grind the following into a fine powder and brush the teeth regularly with it: 5-7 tablespoons of the charred bark of Babul, two tablespoon of rock salt. \n 5. To take care of blood dysentery, soak fro a few hours one teaspoon of pounded roots in a tumbler containing water. Make it warm and drink this water frequently. \n 6. To take care of gum disease, loose teeth, and ulcers in the mouth, boil half a handful of tender leaves in two cups of water. Use the water as a gargle. \n 7. To take care of common cough, make a paste with some tender leaves, take one teaspoon of this paste with one teaspoon of honey and a little warm water. \n 8. To treat the problem of eczema, boil 3-5 tablespoon each of the powdered bark of Babul and mango (Mangifra indica) in 4-5 cups of water. Foment the affected parts with this water. Apply clarified butter or ghee on the affected skin. \n 9. To take care of cough, diabetes, excessive urination, impotency, and throat infection, chew a small portion of the gum everyday until the ailment disappears. 10. To treat conjunctiviktis and sore eyes, grind some tender leaves of Babul with some water into a fine paste. Fold the paste in a clean piece of cloth or handkerchief and bandage it over the closed eyes at bedtime.",
null, "Amazing Healing Plants", db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
答案 0 :(得分:2)
在ListHerb
xml中你需要
<ListView android:id="@android:id/list"
因为你有
public class ListHerb extends ListActivity {
和
setContentView(R.layout.list_herb);
但你有
R.id.lstHerb
http://developer.android.com/reference/android/app/ListActivity.html
引用文档
ListActivity的默认布局由单个布局组成, 屏幕中央的全屏列表。但是,如果你愿意, 您可以通过设置自己的视图布局来自定义屏幕布局 在onCreate()中使用setContentView()。要做到这一点,你自己的观点必须 包含一个ID为“@android:id / list”的ListView对象(或列出if 它在代码中)