因此,我在Android Studio上的课程即将结束,很不幸遇到了将数据库连接到应用程序的问题。基本上,通过使用导航抽屉布局,我将显示一个简单的片段,允许用户输入数据。该数据应该在一页上插入数据库,而在另一页上,我将能够通过数据库查看提交的数据。我的frankencode列在下面,之所以这么称呼是因为我更改了教授提供的源代码以及从其他来源看到的其他花絮。我们为数据库助手,插入页面和显示页面提供了代码,但未提供Main活动,因此我不确定为什么在尝试输入信息时会出错。任何帮助表示赞赏。
switch (menuItem.getItemId()){
case R.id.nav_program1:
fragmentManager.beginTransaction().replace(R.id.firstPage, new
Prog1()).commit();
break;
case R.id.nav_program2:
fragmentManager.beginTransaction().replace(R.id.secPage, new P
Prog2()).commit();
break;
}
MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
final FragmentManager fragmentManager = getSupportFragmentManager();
databasehelper mydb;
DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new databasehelper(this);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem)
{
switch (menuItem.getItemId()){
case R.id.nav_program1:
fragmentManager.beginTransaction().replace(R.id.firstPage, new Prog1()).commit();
break;
case R.id.nav_program2:
fragmentManager.beginTransaction().replace(R.id.secPage, new Prog2()).commit();
break;
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
DBHelper
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class databasehelper extends SQLiteOpenHelper{
public static final String database_name="Ponduce Farms Reservations";
public static final String table_name="names";
public static final String col1="id";
public static final String col2="FIRSTNAME";
public static final String col3="LASTNAME";
public static final String col4="EMAIL";
public static final String col5="PHONE";
public static final String col6="DATE";
public databasehelper(Context context) {
super(context, database_name, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("create table " + table_name +"(ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRSTNAME TEXT, LASTNAME TEXT, EMAIL TEXT, PHONE TEXT, DATE TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " +table_name);
}
public boolean insertData(String fname,String lname,String email, String phone, String date) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(col2,fname);
contentValues.put(col3,lname);
contentValues.put(col4,email);
contentValues.put(col5,phone);
contentValues.put(col6,date);
long result = db.insert(database_name,null ,contentValues);
if(result == -1)
return false;
else
return true;
}
public List<String> showallrecords()
{
List<String> recordlist = new ArrayList<String>();
SQLiteDatabase database = this.getWritableDatabase();
Cursor records = database.rawQuery("select * from names", null);
if (records.moveToFirst())
{
do
{
recordlist.add(records.getString(2)+ " "+ "FirstName: "+
records.getString(3)+" "+"LastName: "+
records.getString(4)+" "+"Email: "+
records.getString(5 )+" "+"Phone: "+
records.getString(6)+" "+"Date: "+
"\n");
}
while (records.moveToNext());
}
return recordlist;
}
}
插入(内容主内容)
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class Prog1 extends Fragment implements View.OnClickListener {
View myview;
databasehelper mydatabasehelper;
EditText fname,lname,email,phonenum;
Button submitbut;
Spinner dates;
String resDate;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
{
myview = inflater.inflate(R.layout.content_main,container,false);
mydatabasehelper = new databasehelper(getActivity());
fname=(EditText)myview.findViewById(R.id.first_input);
lname=(EditText)myview.findViewById(R.id.last_input);
email=(EditText)myview.findViewById(R.id.email_input);
phonenum=(EditText)myview.findViewById(R.id.phone_input);
submitbut=(Button)myview.findViewById(R.id.button);
dates = (Spinner)myview.findViewById(R.id.dates);
String resDate = dates.getSelectedItem().toString();
submitbut.setOnClickListener(this);
return myview;
}
@Override
public void onClick(View v)
{
boolean isIn = mydatabasehelper.insertData(fname.getText().toString(),lname.getText().toString(),email.getText().toString(),phonenum.getText().toString(),resDate);
if(isIn==true)
Toast.makeText(getActivity(),"Data Inserted",Toast.LENGTH_SHORT).show();
else
Toast.makeText(getActivity(),"Data Not Inserted",Toast.LENGTH_SHORT).show();
显示
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class Prog2 extends Fragment {
View myview;
databasehelper mydatabasehelper;
ListView recordlistview;
ArrayList<String> arraylist;
ArrayAdapter<String> adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
{
myview=inflater.inflate(R.layout.program2,container,false);
mydatabasehelper=new databasehelper(getActivity());
recordlistview=(ListView)myview.findViewById(R.id.dbinfo);
List<String> records = mydatabasehelper.showallrecords();
adapter= new ArrayAdapter<String>(getActivity(),android.R.layout.simple_expandable_list_item_1,records);
recordlistview.setAdapter(adapter);
return myview;
}
}
java.lang.IllegalArgumentException: No view found for id 0x7f0900ac (com.example.chp5:id/secPage) for fragment Prog2{7e8490c #0 id=0x7f0900ac}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1454)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
这是我得到的错误。
答案 0 :(得分:0)
您有long result = db.insert(database_name,null ,contentValues);
这是试图在表中插入一行,因为您提供了数据库名称而不是表名称。
更改为long result = db.insert(table_name,null ,contentValues);