0

I am trying to move to the activity below, but it is not working. on the emulator says that "the project keeps stopping". That is my first app that i am trying to build, so i would appreciate if you could explain in details.

package com.example.project3;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


   public class view extends Fragment {
        public static view newInstance() {

            Bundle args = new Bundle();

            view fragment = new view();
            fragment.setArguments(args);
            return fragment;
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v=inflater.inflate(R.layout.activity_view,container,false);
            SQLiteDatabase db;
            db=getActivity().openOrCreateDatabase("employeeTasks.db", Context.MODE_PRIVATE,null);
            crearyllenar(db);
            listviewllenar(v, db);

            Button bfinish = (Button)v.findViewById(R.id.bfinish);
            bfinish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Spinner names = (Spinner)getView().findViewById(R.id.names);
                    Spinner dates = (Spinner)getView().findViewById(R.id.dates);

                    Spinner room = (Spinner)getView().findViewById(R.id.room);
                    Spinner cleanbed1 = (Spinner)getView().findViewById(R.id.cleanbed1);
                    Spinner brushfloor1 = (Spinner)getView().findViewById(R.id.brushfloor1);
                    Spinner mopfloor1 = (Spinner)getView().findViewById(R.id.mopfloor1);
                    Spinner cleanmirrow1 = (Spinner)getView().findViewById(R.id.cleanmirrow1);
                    Spinner cleanwindow1 = (Spinner)getView().findViewById(R.id.cleanwindow1);
                    Spinner removedust1 = (Spinner)getView().findViewById(R.id.removedust1);
                    Spinner emptybin1 = (Spinner)getView().findViewById(R.id.emptybin1);


                    EditText comments = (EditText)getView().findViewById(R.id.comments);

                    SQLiteDatabase db1;

                    db1=getActivity().openOrCreateDatabase("employeeTasks.db", Context.MODE_PRIVATE,null);
                    db1.execSQL("insert into employeeTask(names,dates,rooms,cleanbed,brushfloor,mopfloor,cleanmirrow,cleanwindow,removedust,emptybin,comments,commentsinfo) "+
                            "values('"+names.getSelectedItem().toString()+"','"+dates.getSelectedItem().toString()+"','"+room.getSelectedItem().toString()+"','"+cleanbed1.getSelectedItem().toString()+"','"+brushfloor1.getSelectedItem().toString()+"','"+mopfloor1.getSelectedItem().toString()+"','"+cleanmirrow1.getSelectedItem().toString()+"','"+cleanwindow1.getSelectedItem().toString()+"','"+removedust1.getSelectedItem().toString()+"','"+emptybin1.getSelectedItem().toString()+"','"+comments.getText().toString()+"','@@"+comments.getText().toString()+"')");

                    listviewllenar(getView(),db1);
                    names.setSelection(0);
                    dates.setSelection(0);
                    room.setSelection(0);
                    cleanbed1.setSelection(0);
                    brushfloor1.setSelection(0);
                    mopfloor1.setSelection(0);
                    cleanmirrow1.setSelection(0);
                    cleanwindow1.setSelection(0);
                    removedust1.setSelection(0);
                    emptybin1.setSelection(0);
                    comments.setText("");


                }
            });

            return v;
        }

        private int listviewllenar(View v, SQLiteDatabase db) {
            Cursor c2=db.rawQuery("select * from employeeTask where estado=1 order by _id desc",null);
            String[] from=new String[]{"names","dates","rooms","cleanbed","brushfloor","mopfloor","cleanmirrow","cleanwindow","removedust","emptybin","comments","commentsinfo","time","id"};
            int[] to=new int[]{R.id.tnames, R.id.tdates, R.id.trooms, R.id.tcleanbed,  R.id.tbrushfloor, R.id.tmopfloor, R.id.tcleanmirrow,  R.id.tcleanwindow, R.id.tremovedust, R.id.temptybin,  R.id.tcomments, R.id.tcommentsinfo,R.id.ttime, R.id.tid};

            android.widget.ListView lv=(ListView) v.findViewById(R.id.listview);
            SimpleCursorAdapter adapter= new SimpleCursorAdapter(v.getContext(),R.layout.item,c2,from,to,0);
            lv.setAdapter(adapter);
            return 0;
        }

        private int crearyllenar(SQLiteDatabase db) {
            db.execSQL("create table if not exists employeeTask (_id integer primary key autoincrement unique,"+
                    "name text not null,dates text not null,room integer not null,cleanBed text not null,"+
                    "brushFloor text not null,mopFloor text not null,cleanMirrow text not null,cleanWindow text not null,"+
                    "removeDust text not null,emptyBin text not null,comments text not null,commentsInfo text not null,"+
                    "estado integer default 1,time datetime default (datetime(current_timestamp,'localtime'))) ");
            Cursor c1=db.rawQuery("select * from employeeTasks",null);
            if (c1.getCount()<=0){
              db.execSQL("insert into employeeTask(name,dates,room,cleanBed,brushFloor,mopFloor,cleanMirrow,cleanWindow,removeDust,emptyBin,comments,commentsInfo) "+
                        "values('joe','tusday',1,'yes','yes','yes','yes','yes','yes','yes','yes','yes')");

            }
            return 0;
        }
    }

Please see below the Manifest too.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.project3">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".view"></activity>
        <activity android:name=".View_Lists" />
        <activity android:name=".ListView" />
        <activity android:name=".EmployeeTasks" />
        <activity android:name=".DatabaseHelper" />
        <activity android:name=".Selection" />
        <activity android:name=".Login" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And here is the error that i am getting:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.project3, PID: 19275
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.project3/com.example.project3.view}: java.lang.ClassCastException: com.example.project3.view cannot be cast to android.app.Activity
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7319)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
     Caused by: java.lang.ClassCastException: com.example.project3.view cannot be cast to android.app.Activity
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1243)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3172)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7319) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) 
I/Process: Sending signal. PID: 19275 SIG: 9
Application terminated.
Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71

1 Answers1

0

First, you should not name your this class view. Classes begin with an upper case character (by convention, not enforced). Also you should not name your class just view, or anything containing view if it is no view.

Secondly, you are creating a Fragment. If you try to start an activity with this class it is going to fail, because an activity is not a fragment.

See documentation for fragments here on how to use them.

DaFa
  • 61
  • 5
  • Thank you very much for the information DaFa. I checked the link but I’m still confused. As I mentioned, that’s my first app. Could you please advise me on what to do? I am trying desperately to add the last activity and finish it once and for all. That is my final project for college. I just need to make this last screen work. Thank you very much for your help. – Junior O Ceadaigh Jun 26 '19 at 00:15
  • I would appreciate a lot if you could send me your email and I could send to you more details about the issue and ever a small video that I made showing it. My email is: junior.scan16@gmail.com Than you DaFa. – Junior O Ceadaigh Jun 26 '19 at 00:24