我正在尝试转到下面的活动,但是它不起作用。在仿真器上说“项目不断停止”。 那是我尝试构建的第一个应用程序,因此,如果您能详细解释,我将不胜感激。
printf
也请参见清单下方。
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;
}
}
这是我得到的错误:
<?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>
答案 0 :(得分:0)
首先,您不应命名您的此类视图。类以大写字母开头(按照约定,不是强制性的)。同样,您不应该将您的班级命名为仅视图,如果没有视图,则不要包含任何包含视图的内容。
第二,您正在创建一个片段。如果您尝试使用此类启动活动,那么它将失败,因为活动不是片段。
有关片段here的使用方法,请参阅文档。