2017-02-26 9 views
2

FirebaseListAdapterを除いて、すべてがうまく見えます。envatotuts+にあるチュートリアルに基づいて、アンドロイドグループチャットアプリケーションで作業しています。プロジェクトはコンパイルを拒否します。このエラーはError:(18, 32) error: package com.firebase.ui.database does not existになります。エラーを解決した後、エラーは消え、このエラーはError:(133, 19) error: cannot find symbol constructor (DatabaseReference,Class<ChatMessage>,int,MainActivity)になりました。添付ファイルはMainActivity.javaファイルとactivityMain.xmlファイルです。FirebaseListAdapterを自分のアプリケーションで動作させることができません

MainActivity.java

package com.abdul.friendsdemo; 

import android.content.Intent; 
import android.icu.text.DateFormat; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.firebase.ui.auth.AuthUI; 
import com.firebase.ui.database.FirebaseListAdapter; 
import com.google.android.gms.tasks.OnCompleteListener; 
import com.google.firebase.auth.FirebaseAuth; 
import com.google.firebase.database.FirebaseDatabase; 

import java.util.Calendar; 


public class MainActivity extends AppCompatActivity { 

    private FirebaseListAdapter<ChatMessage> adapter; 
    private static final int SIGN_IN_REQUEST_CODE = 123; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if(FirebaseAuth.getInstance().getCurrentUser() == null){ 
      //Start sign in/sign up activity 
      startActivityForResult(AuthUI.getInstance() 
        .createSignInIntentBuilder() 
        .build(), SIGN_IN_REQUEST_CODE); 
     }else 

     { 
      // User is already signed in. Therefore, display 
      //a welcome Toast 
      Toast.makeText(this, "Welcome" 
          + FirebaseAuth.getInstance() 
          .getCurrentUser() 
          .getDisplayName(), 
        Toast.LENGTH_LONG) 
        .show(); 

      // Load chat room contents 
      displayChatMessage(); 
     } 


     FloatingActionButton fab = 
       (FloatingActionButton) findViewById(R.id.fab); 

     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       EditText input = (EditText) findViewById(R.id.input); 

       //Read the input field and push a new instance 
       //of ChatMessage to the Firebase database 
       FirebaseDatabase.getInstance() 
         .getReference().push() 
         .setValue(new ChatMessage(input.getText().toString(), 
           FirebaseAuth.getInstance() 
             .getCurrentUser() 
             .getDisplayName()) 
         ); 

       //Clear the input 
       input.setText(""); 
      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, 
            Intent data){ 
     super.onActivityResult(requestCode, resultCode, data); 

     if(requestCode == SIGN_IN_REQUEST_CODE){ 
      if(resultCode ==RESULT_OK){ 
       Toast.makeText(this, "Successfully signed in. Welcome!", 
         Toast.LENGTH_LONG).show(); 
       displayChatMessage(); 
      }else{ 
       Toast.makeText(this, "We couldn't sign you in. Please try again later.", 
         Toast.LENGTH_LONG).show(); 
       //Close the app 
       finish(); 
      } 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main_menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (item.getItemId() == R.id.sign_out) { 
      AuthUI.getInstance().signOut(this) 
        .addOnCompleteListener(new OnCompleteListener<Void>() { 
         public void onComplete(@NonNull Task<void> task) { 
          Toast.makeText(MainActivity.this, 
            "You have been signed out.", 
            Toast.LENGTH_LONG).show(); 
          startActivity(new Intent(MainActivity.this, 
            AuthUI.class)); 
          // Close activity; 
          finish(); 
         } 
        }); 

     } 
     return true; 
    } 

    private void displayChatMessage() { 

     ListView listOfMessages = (ListView) findViewById(R.id.list_of_messages); 

     adapter = new FirebaseListAdapter<Chatmessage>(this, ChatMessage.class, 
       R.layout.messages, FirebaseDatabase.getInstance().getReference()) { 
      @Override 
      protected void populateView(View v, ChatMessage model, int position) { 
       //Get references to the views of messages.xml 
       TextView messageText = (TextView) v.findViewById(R.id.message_body); 
       TextView messageUser = (TextView) v.findViewById(R.id.message_user); 
       TextView messageTime = (TextView) v.findViewById(R.id.message_time); 

       // Set their text 
       messageText.setText(model.getMessageText()); 
       messageUser.setText(model.getMessageUser()); 

       //Format the date before showing it 
       Calendar c = Calendar.getInstance(); 
       int hour = c.get(Calendar.HOUR); 
       int minute = c.get(Calendar.MINUTE); 
       messageTime.setText(hour + ":" + minute); 
      } 
     }; 

     listOfMessages.setAdapter(adapter); 
    } 

} 

activityMain.xmlファイル

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    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="com.abdul.friendsdemo.MainActivity"> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:clickable="true" 
     android:src="@drawable/ic_send_black_24dp" 
     android:tint="@android:color/white" 
     app:fabSize="mini" /> 

    <EditText 
     android:id="@+id/input" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_toLeftOf="@id/fab" /> 

    <ListView 
     android:id="@+id/list_of_messages" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@id/fab" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginBottom="@dimen/activity_horizontal_margin" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="@dimen/activity_horizontal_margin" /> 
</RelativeLayout> 

app:build gradle

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.0" 
    defaultConfig { 
     applicationId "com.abdul.friendsdemo" 
     minSdkVersion 16 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:design:25.0.1' 
    compile 'com.android.support:appcompat-v7:25.0.1' 
    compile 'com.firebaseui:firebase-ui-auth:1.2.0' 
    compile 'com.google.firebase:firebase-core:10.2.0' 
    compile 'com.firebaseui:firebase-ui:1.2.0' 
    compile 'com.google.firebase:firebase-auth:10.2.0' 
    compile 'com.google.firebase:firebase-database:10.2.0' 
    testCompile 'junit:junit:4.12' 
} 




apply plugin: 'com.google.gms.google-services' 

私は他の質問の多くを試してみましたが、彼らのために働いていた解決策ではありません私のために働く。

+0

https://github.com/firebase/FirebaseUI-Android#installation –

+0

あなたはGradleのは、ファイルアプリを投稿することができますか? – BlackHatSamurai

+0

@BlackHatSamuraiの質問にgradleファイルを追加しました –

答えて

0

あなたのGradleファイルと同期して

compile 'com.firebaseui:firebase-ui:x.x.x' 

を追加することがありますか?

は、あなたがあなたの宣言の間違った順序を持って

+0

そのバージョンは古くなっていますか? –

+0

ああ申し訳ありませんが、私の悪い、それは例だった私のポイントは、それらの依存関係を追加しています –

+0

私は、そのチュートリアルのリンクは、そのバージョンを使用するので、おそらくそれで答えることは安全でしょう –

1

Firebase UI Installationを参照してください。この:

adapter = new FirebaseListAdapter<Chatmessage>(this, ChatMessage.class, 
      R.layout.messages, FirebaseDatabase.getInstance().getReference())... 

は次のようになります。

adapter = new FirebaseListAdapter<Chatmessage>(FirebaseDatabase.getInstance().getReference(), ChatMessage.class, 
      R.layout.messages,this) 

これは、ドキュメントhereに基づいています。コンストラクターは次のとおりです。public FirebaseListAdapter(Query mRef, Class<T> mModelClass, int mLayout, Activity activity) {

参照は最初のパラメーターではなく最後のパラメーターにあります。そして活動は最後にするべきです。

+0

コードを再配置しましたが、エラーはまだそこにあります –

+0

これはエラーメッセージです:エラー:(133,19)エラー:シンボルコンストラクタを見つけることができません(DatabaseReference、Class 、int、MainActivity) '@BlackHatSamurai –

+0

私は既にこの問題を発見しました。populateViewメソッドのChatMessage.classのメソッドの1つを省略しました。エラーが消えたことを修正した後 –

1

誰でもこの問題が発生した場合は、これを使用してください。

Query query = FirebaseDatabase.getInstance().getReference(); 
FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>() 
      .setQuery(query, ChatMessage.class) 
      .build(); 
adapter = new FirebaseListAdapter<ChatMessage>(options) { 
     @Override 
     protected void populateView(View v, ChatMessage model, int position) { 

     } 
    }; 
関連する問題