2017-04-05 61 views
-3

這是錯誤我得到爲什麼會出現「錯誤:(57,21)錯誤:到達文件的末尾,而解析」

Error:(57, 21) error: reached end of file while parsing

而使用這個代碼

package com.bilalbenzine.hotel; 

import android.content.ContentValues; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.PersistableBundle; 
import android.support.annotation.Nullable; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.appindexing.Thing; 
import com.google.android.gms.common.api.GoogleApiClient; 

import io.realm.Realm; 

/** 
* Created by hmito on 04/04/2017. 
*/ 

public class add_hotel extends AppCompatActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.add_hotel); 

     final EditText name=(EditText)findViewById(R.id.name_hotel); 
     final EditText place=(EditText)findViewById(R.id.place_hotel); 
     final EditText stars=(EditText)findViewById(R.id.stars); 
     final EditText description=(EditText)findViewById(R.id.description); 

     final Button addhotel=(Button)findViewById(R.id.btn_add_hotel); 
     addhotel.hasOnClickListeners(new View.OnClickListener(){ 

     @Override 
      public void onClick(View view) { 
      hotel hotel = new hotel(); 
      hotel.setName_hotel(name.getText().toString()); 
      hotel.setName_hotel(place.getText().toString()); 
      hotel.setName_hotel(stars.getText().toString()); 
      hotel.setName_hotel(description.getText().toString()); 

      Realm realm = Realm.getInstance(getApplicationContext()); 
      realm.beginTransaction(); 
      realm.copyToRealmOrUpdate(hotel); 
      realm.commitTransaction(); 


      Toast.makeText(add_hotel.this, "The hotel has been successfully added", Toast.LENGTH_SHORT).show(); 
     } 
      ); 
     } 
    } 
+3

修復你的間距,你的問題會很明顯 - 你的{}和()對不匹配。 –

+0

您可以使用Android Studio來格式化您的代碼。請在發佈您的問題之前做到這一點 –

+0

數字匹配 –

回答

0

你錯過一雙 {}。 請使用以下代碼。

package com.bilalbenzine.hotel; 

import android.content.ContentValues; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.PersistableBundle; 
import android.support.annotation.Nullable; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.appindexing.Thing; 
import com.google.android.gms.common.api.GoogleApiClient; 

import io.realm.Realm; 

/** 
* Created by hmito on 04/04/2017. 
*/ 

public class add_hotel extends AppCompatActivity 
{ 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.add_hotel); 

     final EditText name=(EditText)findViewById(R.id.name_hotel); 
     final EditText place=(EditText)findViewById(R.id.place_hotel); 
     final EditText stars=(EditText)findViewById(R.id.stars); 
     final EditText description=(EditText)findViewById(R.id.description); 

     final Button addhotel=(Button)findViewById(R.id.btn_add_hotel); 
     addhotel.setOnClickListener(new View.OnClickListener(){ 

      @Override 
      public void onClick(View view) { 
       hotel hotel = new hotel(); 
       hotel.setName_hotel(name.getText().toString()); 
       hotel.setName_hotel(place.getText().toString()); 
       hotel.setName_hotel(stars.getText().toString()); 
       hotel.setName_hotel(description.getText().toString()); 

       Realm realm = Realm.getInstance(getApplicationContext()); 
       realm.beginTransaction(); 
       realm.copyToRealmOrUpdate(hotel); 
       realm.commitTransaction(); 


       Toast.makeText(add_hotel.this, "The hotel has been successfully added", Toast.LENGTH_SHORT).show(); 
      } 
      } 
      ); 
     } 
    } 
+0

問題依然存在,,,! –

+0

@ZizouMadrid你也使用hasOnClickListeners,但它可能是setOnClickListener。檢查我的答案,我使用setOnClickListener而不是hasOnClickListeners。 – user7676575

+0

非常感謝。問題已得到解決 –

0

1.

hasOnClickListenerssetOnClickListener

2.

「Error:(57, 21) error: reached end of file while parsing」

你在代碼中缺少一個右括號 「}」 的地方。

Android工作室強調錯誤,將鼠標懸停在紅色帶下劃線的代碼上,並按照建議操作。

+0

雖然它遵循Java和Android命名約定。類遵循'UpperCamelCase'風格,活動應該以'Activity'結束。在你的例子中,它將是'AddHotelActivity'。它使代碼更具可讀性。 –

+0

非常感謝。問題已解決 –

+0

@ZizouMadrid歡迎來到StackOverflow,請閱讀[關於接受答案](http://stackoverflow.com/help/accepted-answer)。 –