I want to display all the data from my database created in DB Browser, but I get these errors. I cannot find a solution. Please help me, this is very important to my project. Maybe there's a typo mistake.
05-27 13:54:22.584 32610-32610/com.example.andre.nutridian E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.andre.nutridian, PID: 32610
java.lang.RuntimeException: Unable to resume activity {com.example.andre.nutridian/com.example.andre.nutridian.Main2Activity}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v7.widget.ActionBarContainer
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3353)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3384)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v7.widget.ActionBarContainer
at android.support.v7.app.WindowDecorActionBar.init(WindowDecorActionBar.java:201)
at android.support.v7.app.WindowDecorActionBar.<init>(WindowDecorActionBar.java:172)
at android.support.v7.app.AppCompatDelegateImplV9.initWindowDecorActionBar(AppCompatDelegateImplV9.java:182)
at android.support.v7.app.AppCompatDelegateImplBase.getSupportActionBar(AppCompatDelegateImplBase.java:145)
at android.support.v7.app.AppCompatDelegateImplV9.onPostResume(AppCompatDelegateImplV9.java:267)
at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:172)
at android.app.Activity.performResume(Activity.java:6362)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3336)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3384)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
My classes are DBHelper:
public class DbHelper extends SQLiteOpenHelper {
private static String DB_PATH="";
private static String DB_NAME="test.db";
private SQLiteDatabase mDataBase;
private Context mContext=null;
public DbHelper(Context context) {
super(context, DB_NAME, null, 1);
if(Build.VERSION.SDK_INT>=16)
DB_PATH=context.getApplicationInfo().dataDir+"/databases/";
else
DB_PATH="/data/data/"+context.getPackageName()+"/databases/";
mContext=context;
}
@Override
public synchronized void close() {
if(mDataBase!=null)
mDataBase.close();
super.close();
}
private boolean checkDataBase() {
SQLiteDatabase tempDB = null;
try {
String path = DB_PATH + DB_NAME;
tempDB = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
}catch(Exception ex){}
if(tempDB!=null)
tempDB.close();
return tempDB!=null?true:false;
}
public void copyDataBase() {
try {
InputStream myInput = mContext.getAssets().open(DB_NAME);
String outputFileName=DB_PATH+DB_NAME;
OutputStream myOutput=new FileOutputStream(outputFileName);
byte [] buffer=new byte[1024];
int length;
while((length=myInput.read(buffer))>0){
myOutput.write(buffer,0,length);
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void openDataBase(){
String path=DB_PATH+DB_NAME;
mDataBase=SQLiteDatabase.openDatabase(path,null,SQLiteDatabase.OPEN_READWRITE);
}
public void createDataBase(){
boolean isDBExist=checkDataBase();
if(isDBExist)
{
}
else{
this.getReadableDatabase();
try{
copyDataBase();
}catch(Exception ex){}
}
}
public List<Aliment> getAllAlim(){
List<Aliment> temp=new ArrayList<Aliment>();
SQLiteDatabase db= this.getWritableDatabase();
Cursor c;
try {
c = db.rawQuery("SELECT * FROM Aliment", null);
if (c == null) return null;
c.moveToFirst();
do {
Aliment aliment = new Aliment(c.getString(c.getColumnIndex("aliment")), c.getString(c.getColumnIndex("calorii")), c.getString(c.getColumnIndex("cantitate")));
temp.add(aliment);
} while (c.moveToNext());
c.close();
}
catch(Exception e) {
}
db.close();
return temp;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
The MainActivity is:
public class Main2Activity extends AppCompatActivity {
List<Aliment> lstAlim=new ArrayList<Aliment>();
DbHelper dbHelper;
Button btnAfiseazaLista;
LinearLayout container;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btnAfiseazaLista=(Button) findViewById(R.id.btnAfiseazaLista);
container=(LinearLayout)findViewById(R.id.container);
dbHelper=new DbHelper(getApplicationContext());
dbHelper.createDataBase();
btnAfiseazaLista.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lstAlim=dbHelper.getAllAlim();
for(Aliment aliment:lstAlim){
LayoutInflater inflater=(LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View addView=inflater.inflate(R.layout.row,null);
TextView txtAliment=(TextView)addView.findViewById(R.id.txtAliment);
TextView txtCalorii=(TextView)addView.findViewById(R.id.txtCalorii);
TextView txtCantitate=(TextView)addView.findViewById(R.id.txtCantitate);
txtAliment.setText(aliment.getAliment());
txtCalorii.setText(aliment.getCalorii());
txtCantitate.setText(aliment.getCantitate());
container.addView(addView);
}
}
});
}
}
These 2 are the classes I use, the Logcat says in these 2 classes are the problems. I am new to this and I need a little guidance to resolve my errors. Please help me in any way. Thank you in advance
The issue has nothing to do with your SQLite Database.
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v7.widget.ActionBarContainer
The issue is that you are declaring a LinearLayout
but it's not a LinearLayout
.
Make sure that R.id.container
is actually a LinearLayout
In your xml layout.
Try changing:
container=(LinearLayout)findViewById(R.id.container);
To:
container=(LinearLayout)findViewById(R.id.action_bar_container);