我想了解一些Firebase以便在學生項目中使用簡單的數據庫。現在我遇到了Ive花了幾個小時數,並仍似乎沒有弄清楚Firebase:「構造函數Message(String,String)未定義
我從火力使用一個簡單的例子:寫列表數據和讀取列表數據:https://www.firebase.com/docs/managing-lists.html
所以伊夫粘貼的例子,修改了代碼以符合我自己的數據庫,但eclipse不斷抱怨這行的構造函數:newPushRef.setValue(new Message(「wilma」,「Hello」)); 錯誤說:構造函數Message(String,String)未定義。它想要導入「android.os.Message;」我不知道我是否應該,但無論如何沒有任何東西似乎解決它。建議說構造函數應該是空的。 我在這裏錯過了什麼?
我已將最新的Java SDK導入到libs文件夾中,單擊「構建路徑」。 與簡單登錄庫相同。
我是新來的java,請謙虛。
編輯: 與「System.out.println(m.getUser_id());」相同的問題; 「的方法getUser_id()是未定義的類型信息」
這是我StartActivity:
package com.mah.phli_xjobb_textpushmess_25mars;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.firebase.client.Query;
import com.firebase.simplelogin.SimpleLogin;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class StartActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
//---FIREBASE--//
// First we get a reference to the location of the user's name data:
Firebase listRef = new Firebase("https://listtest.firebaseio.com/message_list");
// Generate a reference to a new location with push()
Firebase newPushRef = listRef.push();
// Set some data to the generated location
newPushRef.setValue(new Message("wilma", "Hello"));
// Get the name generated by push
String pushedName = newPushRef.getName();
listRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildRemoved(DataSnapshot arg0) {
// TODO Auto-generated method stub
}
@Override
public void onChildMoved(DataSnapshot arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onChildChanged(DataSnapshot arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildName) {
Message m = snapshot.getValue(Message.class);
System.out.println(m.getUser_id());
}
@Override
public void onCancelled(FirebaseError arg0) {
// TODO Auto-generated method stub
}
});
//---END FIREBASE----//
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.start, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_start,
container, false);
return rootView;
}
}
}//End class
這似乎很可能是Eclipse配置問題。我已經添加了標籤;希望有人幫助! :) – Kato