-2
I am Writing my first code to add and fetch some data from a database .:

以下是我在 Mainactivity.java 文件中编写的代码及其给出的一些错误,例如令牌“Private”上的语法错误,“Public”也应为私有;我也附上了屏幕截图,有人可以帮忙解决这个问题。因为我是 java 和 android 代码的新手

包 com.example.ghrixdatabse;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.TableLayout;

public class MainActivity extends Activity {
    private static final EditText FindViewById = null;
    String fname,lname,email;   
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db=openOrCreateDatabase("Mydb1", MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS Student(fname VARCHAR,lname VARCHAR,email VARCHAR);" );
    }
Private void Adddata(View view) 

{  
    EditText edittext1=(EditText)findViewById(R.id.firstname);
    EditText edittext2=(EditText)findViewById(R.id.lastname);
    EditText edittext3=(EditText)findViewById(R.id.email);
    fname= edittext1.getText().toString();
    lname= edittext2.getText().toString();
    email= edittext3.getText().toString();
    db.execSQL("INSERT INTO Student VALUES('"+fname+"','"+lname+"','"+email+"');");


}
Private void Showdata(View view)    
{
    Cursor c = db.rawQuery(SELECT * FROM from",null);
    int count= c.getCount();
    c.moveToFirst();
    TableLayout tableLayout = new TableLayout(getApplicationContext () );
    tableLayout.setVerticalScrollBarEnabled(true);

    tableRow = new TableRow(getApplicationContext());
    textView = new TextView(getApplicationContext());
    textView.setText("Firstname");
    textView.setColor(Color.Red);
    textView.setTypeface(null,Typeface.Bold);
    textView.setPadding(20,20,20,20);
    tableRow.addView(textView);


    tableRow = new TableRow(getApplicationContext());
    textView = new TextView(getApplicationContext());
    textView.setText("LastName");
    textView.setColor(Color.Red);
    textView.setTypeface(null,Typeface.Bold);
    textView.setPadding(20,20,20,20);
    tableRow.addView(textView);


    tableRow = new TableRow(getApplicationContext());
    textView = new TextView(getApplicationContext());
    textView.setText("Email");
    textView.setColor(Color.Red);
    textView.setTypeface(null,Typeface.Bold);
    textView.setPadding(20,20,20,20);
    tableRow.addView(textView);

    tableLayout.addView(tableRow);
    for (Integer j=0;j < count;j++)
    {
        tableRow =new TableRow(getApplicationContext());
        textView1 = new TextView(getApplicationContext());
        textView1.setText(c.getString(c.getColoumIndex('fname')));
        textView2 = new TextView(getApplicationContext());
        textView2.setText(c.getString(c.getColoumIndex('lanme')));
        textView3 = new TextView(getApplicationContext());
        textView3.setText(c.getString(c.getColoumIndex('email')));
        textView1.setPadding(20,20,20,20);
        textView2.setPadding(20,20,20,20);
        textView3.setPadding(20,20,20,20);
        tableRow.addView(textView1);
        tableRow.addView(textView2);
        tableRow.addView(textView3);
        tablelayout.addView(tableRow);
        c.moveToNext(); 

    }
    setContentView(tableLayout);
    db.close();
}
Public void close (View view)
{
  System.exit(0);
}

}

那边的私人班级的错误

也适用于公众

第三个是图形布局

这是 XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="49dp"
        android:text="FirstName" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="61dp"
        android:text="Lastname" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_centerVertical="true"
        android:text="E-mail" />

    <EditText
        android:id="@+id/firstname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentRight="true"
        android:ems="10" />

    <EditText
        android:id="@+id/lastname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentRight="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignLeft="@+id/lastname"
        android:ems="10" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/email"
        android:layout_marginBottom="24dp"
        android:onClick="showdata"
        android:text="Show Table" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_alignRight="@+id/button3"
        android:layout_marginBottom="26dp"
        android:layout_marginRight="17dp"
        android:onClick="close"
        android:text="Close" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_toRightOf="@+id/textView3"
        android:onClick="Adddata"
        android:text="Create" />

</RelativeLayout>
4

3 回答 3

1

更改Privateprivate和。Public_ 在私人和公共应该在小情况下publicp

于 2013-07-29T10:00:14.477 回答
1

java 是区分大小写的,所以使用private而不是Private访问说明符。asPrivate不是关键字,也没有特殊含义。同样适用Public,使用public

于 2013-07-29T10:00:52.433 回答
1

愿这对您有所帮助:

您收到此错误是因为java is case sensitive,因此,更改这些行:

  • Private void Adddata(View view)
  • Private void Showdata(View view)
  • Public void close (View view)

到:

  • private void Adddata(View view)
  • private void Showdata(View view)
  • public void close (View view)

编辑:

您在这一行中也遇到错误也检查它:

  • Cursor c = db.rawQuery(SELECT * FROM from"// write here your table name,null);

将其更改为:

Cursor c = db.rawQuery("SELECT * FROM from"// write here your TableName,null);
于 2013-07-29T10:01:49.403 回答