2013-07-29 53 views
-2
I am Writing my first code to add and fetch some data from a database .: 

以下是我在Mainactivity.java文件中編寫的代碼及其給出的一些錯誤 如令牌「Private」上的語法錯誤,私人預期也是「Public」; 我附上了屏幕截圖以及有人可以幫助關於此。因爲我是新來的Java和Android代碼Android代碼錯誤:語法錯誤令牌「私人」,私人預計也「公共」;

package 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); 
} 

} 

error for the private class over there

For Public as well

Third one is the graphical layout

下面是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> 
+5

使用小寫'p'爲'private' – Reimeus

+0

語法錯誤..使用小寫並且在rawquery無報價.. – Tarun

回答

1

變化PrivateprivatePublicpublicp私有和公共應該在小案例

+0

(EditText)findViewById(R.id.firstname); 這是錯誤,因爲「無法從視圖轉換爲EditText」 –

+0

@RanbirSingh檢查您的xml文件。看起來有點不對勁 – Jaguar

+0

我也附加了XML文件 –

1

java是區分大小寫的,因此使用private而不是Private訪問說明符。由於Private不是關鍵字,因此沒有特別的意義。同樣適用於Public,請使用public

+0

(EditText)findViewById(R.id.firstname);這是錯誤,因爲「無法從視圖轉換爲EditText」會導致什麼錯誤 –

+0

@RanbirSingh Error說明了這一切,您不能將視圖轉換爲編輯文本。我對android並不瞭解。我回答你的原始問題。對於以下問題,您可以創建單獨的問題。 –

+0

:)感謝您的幫助 –

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); 
+0

(EditText)findViewById(R.id.firstname);這是錯誤,因爲「無法從視圖轉換爲EditText」它會是什麼? –

+0

@RanbirSingh:正確檢查你的xml文件好友..問題在於它.. –

+0

我附上了XML以及你可以看看請 –