你好,
我是java编程的新手,我收到了以下错误:显然,我收到了一个NotFound异常
2020-11-30 20:52:40.874 7409-7409/com.Alex.mydiabetesdiary E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.Alex.mydiabetesdiary, PID: 7409
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:237)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2400)
at android.content.res.Resources.getLayout(Resources.java:1252)
at android.view.LayoutInflater.inflate(LayoutInflater.java:530)
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:425)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:416)
at android.widget.AbsListView.obtainView(AbsListView.java:2387)
at android.widget.ListView.makeAndAddView(ListView.java:2067)
at android.widget.ListView.fillDown(ListView.java:793)
at android.widget.ListView.fillFromTop(ListView.java:855)
at android.widget.ListView.layoutChildren(ListView.java:1838)
at android.widget.AbsListView.onLayout(AbsListView.java:2184)
at android.view.View.layout(View.java:22844)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
at android.view.View.layout(View.java:22844)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22844)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:530)
at android.view.View.layout(View.java:22844)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22844)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:22844)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:784)
at android.view.View.layout(View.java:22844)
at android.view.ViewGroup.layout(ViewGroup.java:6389)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3470)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2938)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
at android.view.Choreographer.doCallbacks(Choreographer.java:796)
at android.view.Choreographer.doFrame(Choreographer.java:731)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2020-11-30 20:52:42.130 537-556/system_process E/JobScheduler.Background: App com.google.android.gms became active but still in NEVER bucket
当我查看代码时,我有一些部分:
主要活动
package com.Alex.mydiabetesdiary;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button btn_add, btn_viewAll;
EditText et_description, et_carbohydrates;
Switch sw_favorite;
ListView lv_foodList;
ArrayAdapter foodArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_add = (Button) findViewById(R.id.btn_add);
btn_viewAll = (Button) findViewById(R.id.btn_viewAll);
sw_favorite = (Switch) findViewById(R.id.sw_favorite);
et_description = (EditText) findViewById(R.id.et_description);
et_carbohydrates = (EditText) findViewById(R.id.et_carbohydrates);
lv_foodList = (ListView) findViewById(R.id.lv_foodList);
DBOHelper dboHelper = new DBOHelper (MainActivity.this);
//ShowFoodOnListView(dboHelper);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FoodModel foodModel;
try {
foodModel = new FoodModel(-1, et_description.getText().toString(), Float.parseFloat(et_carbohydrates.getText().toString()));
Toast.makeText(MainActivity.this, foodModel.toString(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "error creating food", Toast.LENGTH_LONG).show();
foodModel = new FoodModel(-1, "Error", 0);
}
DBOHelper dboHelper = new DBOHelper(MainActivity.this);
boolean success = dboHelper.addOne(foodModel);
Toast.makeText(MainActivity.this, "Success= " + success, Toast.LENGTH_LONG).show();
ShowFoodOnListView(dboHelper);
}
});
btn_viewAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
DBOHelper dboHelper = new DBOHelper(MainActivity.this);
//List<FoodModel> foodModelList = dboHelper.getAllfood();
//foodArrayAdapter = new ArrayAdapter<FoodModel>(MainActivity.this, android.R.layout.simple_list_item_1,foodModelList);
ShowFoodOnListView(dboHelper);
//Toast.makeText(MainActivity.this, foodModelList.toString(),Toast.LENGTH_LONG).show();
} catch (Exception e)
{
Toast.makeText(MainActivity.this, e.getMessage(),Toast.LENGTH_LONG).show();
}
}
});
lv_foodList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FoodModel clickedFood = (FoodModel) parent.getItemAtPosition(position);
//dboHelper.deleteOne(clickedFood);
//ShowFoodOnListView(dboHelper);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.voedingswaardetabel.nl/voedingswaarde/voedingsmiddel/?id="+clickedFood.getId()));
startActivity(browserIntent);
//Toast.makeText(MainActivity.this,"Deleted "+clickedFood.toString(),Toast.LENGTH_LONG).show();
}
});
}
private void ShowFoodOnListView(DBOHelper dboHelper) {
//foodArrayAdapter = new ArrayAdapter<FoodModel>(MainActivity.this, android.R.layout.simple_list_item_1, dboHelper.getAllfood());
//return;
//kapot//foodArrayAdapter = new ArrayAdapter<FoodModel>(MainActivity.this, R.layout.food_lv_detail, dboHelper.getAllfood());
//kapot foodArrayAdapter = new ArrayAdapter<FoodModel>(MainActivity.this,R.layout.food_lv_detail,R.id.lv_foodList,dboHelper.getAllfood());
//List<FoodModel> listFoodmodel= dboHelper.getAllfood();
foodArrayAdapter = new ArrayAdapter<FoodModel>(MainActivity.this,0,dboHelper.getAllfood());
lv_foodList.setAdapter(foodArrayAdapter);
}
}
我的食物模型
package com.Alex.mydiabetesdiary;
public class FoodModel {
private int id;
private String description;
private float energyKcal;
private float energyKJoule;
private float Water;
private float Protein;
private float carbohydrates;
private float sugars;
private float fat;
private float saturatedFat;
private float monounsaturatedFat;
private float polyunsaturatedFat;
private float cholesterol;
private float dietaryFiber;
private boolean favorite;
//Constructors
public FoodModel(int id, String description, float energyKcal, float energyKJoule,
float water, float protein, float carbohydrates, float sugars, float fat,
float saturatedFat, float monounsaturatedFat, float polyunsaturatedFat,
float cholesterol, float dietaryFiber, boolean favorite) {
this.id = id;
this.description = description;
this.energyKcal = energyKcal;
this.energyKJoule = energyKJoule;
Water = water;
Protein = protein;
this.carbohydrates = carbohydrates;
this.sugars = sugars;
this.fat = fat;
this.saturatedFat = saturatedFat;
this.monounsaturatedFat = monounsaturatedFat;
this.polyunsaturatedFat = polyunsaturatedFat;
this.cholesterol = cholesterol;
this.dietaryFiber = dietaryFiber;
this.favorite = favorite;
}
public FoodModel(int id, String description, float carbohydrates) {
this.id = id;
this.description = description;
this.carbohydrates = carbohydrates;
}
public FoodModel() {
}
//Tostring
@Override
public String toString() {
return "FoodModel{" +
"id=" + id +
", description='" + description + '\'' +
", energyKcal=" + energyKcal +
", energyKJoule=" + energyKJoule +
", Water=" + Water +
", Protein=" + Protein +
", carbohydrates=" + carbohydrates +
", sugars=" + sugars +
", fat=" + fat +
", saturatedFat=" + saturatedFat +
", monounsaturatedFat=" + monounsaturatedFat +
", polyunsaturatedFat=" + polyunsaturatedFat +
", cholesterol=" + cholesterol +
", dietaryFiber=" + dietaryFiber +
", favorite=" + favorite +
'}';
}
public String getDescriptions() {
return "energyKcal=" + energyKcal +" Protein=" + Protein + " fat=" + fat;
}
//Getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public float getEnergyKcal() {
return energyKcal;
}
public void setEnergyKcal(float energyKcal) {
this.energyKcal = energyKcal;
}
public float getEnergyKJoule() {
return energyKJoule;
}
public void setEnergyKJoule(float energyKJoule) {
this.energyKJoule = energyKJoule;
}
public float getWater() {
return Water;
}
public void setWater(float water) {
Water = water;
}
public float getProtein() {
return Protein;
}
public void setProtein(float protein) {
Protein = protein;
}
public float getCarbohydrates() {
return carbohydrates;
}
public void setCarbohydrates(float carbohydrates) {
this.carbohydrates = carbohydrates;
}
public float getSugars() {
return sugars;
}
public void setSugars(float sugars) {
this.sugars = sugars;
}
public float getFat() {
return fat;
}
public void setFat(float fat) {
this.fat = fat;
}
public float getSaturatedFat() {
return saturatedFat;
}
public void setSaturatedFat(float saturatedFat) {
this.saturatedFat = saturatedFat;
}
public float getMonounsaturatedFat() {
return monounsaturatedFat;
}
public void setMonounsaturatedFat(float monounsaturatedFat) {
this.monounsaturatedFat = monounsaturatedFat;
}
public float getPolyunsaturatedFat() {
return polyunsaturatedFat;
}
public void setPolyunsaturatedFat(float polyunsaturatedFat) {
this.polyunsaturatedFat = polyunsaturatedFat;
}
public float getCholesterol() {
return cholesterol;
}
public void setCholesterol(float cholesterol) {
this.cholesterol = cholesterol;
}
public float getDietaryFiber() {
return dietaryFiber;
}
public void setDietaryFiber(float dietaryFiber) {
this.dietaryFiber = dietaryFiber;
}
public boolean isFavorite() {
return favorite;
}
public void setFavorite(boolean favorite) {
this.favorite = favorite;
}
}
我的食物适配器
package com.Alex.mydiabetesdiary;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import androidx.core.graphics.drawable.IconCompat;
import java.util.List;
public class FoodAdapter extends BaseAdapter {
List<FoodModel> listFoodModel;
LayoutInflater mInflater;
public FoodAdapter(Context c, List<FoodModel> p) {
listFoodModel = p;
mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return listFoodModel.size();
}
@Override
public Object getItem(int position) {
return listFoodModel.get(position);
}
@Override
public long getItemId(int position) {
FoodModel selectedFood = (FoodModel) listFoodModel.get(position);
return selectedFood.getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
FoodModel foodModel = listFoodModel.get(position);
//LayoutInflater inflater = (LayoutInflater) this.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View v = mInflater.inflate(R.layout.food_lv_detail,null);
TextView tv_foodName = (TextView) v.findViewById(R.id.tv_foodname);
TextView tv_otherdescription = (TextView) v.findViewById(R.id.tv_otherdescription);
TextView tv_carbohydrates = (TextView) v.findViewById(R.id.tv_carbohydrates);
tv_foodName.setText(foodModel.getDescription());
tv_carbohydrates.setText(foodModel.getCarbohydrates() + "");
tv_carbohydrates.setText(foodModel.getDescriptions());
return v;
}
}
我的dbo助手
package com.Alex.mydiabetesdiary;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOError;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DBOHelper extends SQLiteOpenHelper {
public static final String FOOD_TABLE = "FOOD_TABLE";
public static final String COLUMN_FOOD_ENERGY_KCAL_FLOAT = "FOOD_ENERGY_KCAL FLOAT";
public static final String COLUMN_FOOD_ENERY_KJOULE = "FOOD_ENERY_KJOULE";
public static final String COLUMN_FOOD_WATER = "FOOD_WATER";
public static final String COLUMN_FOOD_PROTEIN = "FOOD_PROTEIN";
public static final String COLUMN_FOOD_CARBOHYDRATES = "FOOD_CARBOHYDRATES";
public static final String COLUMN_FOOD_SUGARS = "FOOD_SUGARS";
public static final String COLUMN_FOOD_SATURATEDFAT = "FOOD_SATURATEDFAT";
public static final String COLUMN_FOOD_MONOUNSATURATEDFAT = "FOOD_MONOUNSATURATEDFAT";
public static final String COLUMN_FOOD_POLYUNSATURATEDFAT = "FOOD_POLYUNSATURATEDFAT";
public static final String COLUMN_FOOD_CHOLESTROL = "FOOD_CHOLESTROL";
public static final String COLUMN_FOOD_DIETARYFIBER = "FOOD_DIETARYFIBER";
public static final String COLUMN_FOOD_DESCRIPTION = "FOOD_DESCRIPTION";
public static final String COLUMN_FOOD_SITEID = "FOOD_SITEID";
public static final String COLUMN_FOOD_FAT = "FOOD_FAT";
public DBOHelper(@Nullable Context context) {
super(context, "mydiabetes.db", null, 1);
}
// only triggerd the first time
@Override
public void onCreate(SQLiteDatabase db) {
//String createTableStatement = "CREATE TABLE " + FOOD_TABLE + " (ID Integer PRIMARY KEY AUTOINCREMENT, " + COLUMN_FOOD_DESCRIPTION + " STRING " + COLUMN_FOOD_ENERGY_KCAL_FLOAT + ", " + COLUMN_FOOD_ENERY_KJOULE + " FLOAT, "
// + COLUMN_FOOD_WATER + " FLOAT, " + COLUMN_FOOD_PROTEIN + " FLOAT, " + COLUMN_FOOD_CARBOHYDRATES + " FLOAT, " + COLUMN_FOOD_SUGARS + " FLOAT, "
// + COLUMN_FOOD_SATURATEDFAT + " FLOAT, " + COLUMN_FOOD_MONOUNSATURATEDFAT + " FLOAT, " + COLUMN_FOOD_POLYUNSATURATEDFAT + " FLOAT, " + COLUMN_FOOD_CHOLESTROL + " FLOAT, "
// + COLUMN_FOOD_DIETARYFIBER + " FLOAT)";
String createTableStatement = "CREATE TABLE FOOD_TABLE (ID Integer PRIMARY KEY AUTOINCREMENT, FOOD_SITEID Integer, FOOD_DESCRIPTION STRING, FOOD_ENERGY_KCAL FLOAT, FOOD_ENERGY_KJOULE FLOAT, FOOD_WATER FLOAT, FOOD_PROTEIN FLOAT, FOOD_CARBOHYDRATES FLOAT, FOOD_SUGARS FLOAT, FOOD_FAT FLOAT, FOOD_SATURATEDFAT FLOAT, FOOD_MONOUNSATURATEDFAT FLOAT, FOOD_POLYUNSATURATEDFAT FLOAT, FOOD_CHOLESTEROL FLOAT, FOOD_DIETARYFIBER FLOAT, FOOD_FAVORITE BOOLEAN)";
db.execSQL(createTableStatement);
//FillFoodTable();
//String createTableStatement = "CREATE TABEL FOOD_TABLE (ID Integer PRIMARY KEY AUTOINCREMENT, FOOD_DESCRIPTION STRING, FOOD_ENERGY_KCAL FLOAT, FOOD_ENERGY_KJOULE FLOAT, FOOD_WATER FLOAT, FOOD_PROTEIN FLOAT, FOOD_CARBOHYDRATES FLOAT, FOOD_SUGARS FLOAT, FOOD_SATURATEDFAT FLOAT, FOOD_MONOUNSATURATEDFAT FLOAT, FOOD_POLYUNSATURATEDFAT FLOAT, FOOD_CHOLESTEROL FLOAT, FOOD_DIETARYFIBER FLOAT, FOOD_FAVORITE BOOLEAN)";
}
private void FillFoodTable() {
Intent data = new Intent(Intent.ACTION_GET_CONTENT);
String filePath = data.getData().getPath();
if (filePath.contains("/root_path"))
filePath = filePath.replace("/root_path", "");
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("delete from " + FOOD_TABLE);
try {
FileReader file = new FileReader(filePath);
BufferedReader buffer = new BufferedReader(file);
ContentValues contentValues = new ContentValues();
String line = "";
db.beginTransaction();
while ((line = buffer.readLine()) != null) {
String[] str = line.split(",", 14);
String siteId = str[0].toString();
String description = str[1].toString();
String energy_Kcal = str[2].toString();
String energy_kjoule = str[3].toString();
String water = str[4].toString();
String protein = str[5].toString();
String carbohydrates = str[6].toString();
String sugars = str[7].toString();
String fat = str[8].toString();
String saturatedFat = str[9].toString();
String monounsaturatedFat = str[10].toString();
String polyunsaturatedFat = str[11].toString();
String cholesterol = str[12].toString();
String dietaryfiber = str[13].toString();
//String favorite =str[14].toString();
contentValues.put(COLUMN_FOOD_SITEID, siteId);
contentValues.put(COLUMN_FOOD_DESCRIPTION, description);
contentValues.put(COLUMN_FOOD_ENERGY_KCAL_FLOAT, energy_Kcal);
contentValues.put(COLUMN_FOOD_ENERY_KJOULE, energy_kjoule);
contentValues.put(COLUMN_FOOD_WATER, water);
contentValues.put(COLUMN_FOOD_PROTEIN, protein);
contentValues.put(COLUMN_FOOD_CARBOHYDRATES, carbohydrates);
contentValues.put(COLUMN_FOOD_SUGARS, sugars);
contentValues.put(COLUMN_FOOD_FAT, fat);
contentValues.put(COLUMN_FOOD_SATURATEDFAT, saturatedFat);
contentValues.put(COLUMN_FOOD_MONOUNSATURATEDFAT, monounsaturatedFat);
contentValues.put(COLUMN_FOOD_POLYUNSATURATEDFAT, polyunsaturatedFat);
contentValues.put(COLUMN_FOOD_CHOLESTROL, cholesterol);
contentValues.put(COLUMN_FOOD_DIETARYFIBER, dietaryfiber);
db.insert(FOOD_TABLE, null, contentValues);
}
db.setTransactionSuccessful();
db.endTransaction();
} catch (IOException e) {
}
}
//is only triggerd when version table changed
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public boolean addOne(FoodModel foodModel) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_FOOD_CARBOHYDRATES, foodModel.getCarbohydrates());
cv.put(COLUMN_FOOD_DESCRIPTION, foodModel.getDescription());
long insert = db.insert(FOOD_TABLE, null, cv);
if (insert == -1) {
return false;
} else {
return true;
}
}
public boolean deleteOne(FoodModel foodModel) {
SQLiteDatabase db = this.getWritableDatabase();
String queryString = "DELETE FROM " + FOOD_TABLE + " WHERE ID = " + foodModel.getId();
Cursor cursor = db.rawQuery(queryString, null);
if (cursor.moveToFirst()) {
return true;
} else {
return false;
}
}
public List<FoodModel> getAllfood() {
List<FoodModel> returnList = new ArrayList<>();
String queryString = "SELECT * FROM " + FOOD_TABLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(queryString, null);
if (cursor.moveToFirst()) {
//loop through the cursor and create a new customer object
do {
int foodID = cursor.getInt(0);
String description = cursor.getString(1);
float carbohydrates = cursor.getFloat(5);
//boolean favorite = cursor.getInt(3) == 1? true:false;
if (description == null){
description = "TEST";
}
FoodModel newFoodModel = new FoodModel(foodID, description, carbohydrates);
returnList.add(newFoodModel);
} while (cursor.moveToNext());
} else {
//no items in the list
}
cursor.close();
db.close();
return returnList;
}
}
这是一个小项目,我希望通过这个项目学到一些东西(我想创建一个小应用程序,可以计算我的碳水化合物摄入量,因为我有糖尿病)的应用程序在商店有限制或是不正确的。所以我正在尝试构建一个应用程序。:)
我用的是安卓工作室的应用程序,我想不出有什么问题。。。我不知道。我不熟悉java。
暂无答案!
目前还没有任何答案,快来回答吧!