Constructor Event in class Event<T> cannot be applied to given types; cannot find symbol method getTitle()

huangapple go评论72阅读模式
英文:

Constructor Event in class Event<T> cannot be applied to given types; cannot find symbol method getTitle()

问题

我正在尝试将我的应用程序与Firebase数据库连接但是它给了我一个致命的异常错误:(我没有发布之前实现的其余代码因为它之前运行得很好在下面的代码实现之后我遇到了一个错误

package com.example.guideme;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.common.api.Status;
import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.widget.Autocomplete;
import com.google.android.libraries.places.widget.AutocompleteActivity;
import com.google.android.libraries.places.widget.model.AutocompleteActivityMode;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.example.guideme.Event;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    EditText locationEditText, titleEditText,descriptionEditText;
    Button publish, inviteFriends;
    TextView tvDate;
    ListView listView;
    private static final String TAG ="MainActivity";
    private DatabaseReference mDatabase;
    private DatePickerDialog.OnDateSetListener mDateSetListener;

    public String title, description, date, location;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //hooks to all elements in activity_main.xml
        locationEditText = findViewById(R.id.locationEditText);
        titleEditText = (EditText) findViewById(R.id.titleEditText);
        descriptionEditText = (EditText) findViewById(R.id.descriptionEditText);
        publish = (Button) findViewById(R.id.publish);
        tvDate = (TextView) findViewById(R.id.tvDate);
        inviteFriends = (Button) findViewById(R.id.inviteFriends);
        listView = (ListView) findViewById(R.id.listView);

        mDatabase = FirebaseDatabase.getInstance().getReference();

        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                getAllTasks(dataSnapshot);
            }

            @Override
            public void onCancelled(DatabaseError error) {
                //Failed to read value
               Log.w("events", "Failed to read value.", error.toException());
            }
        });

                publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                title = titleEditText.getText().toString();
                date = tvDate.getText().toString();
                description = descriptionEditText.getText().toString();
                location = locationEditText.getText().toString();

                String id = title.replaceAll("\\s","");

                writeNewUser(id, title, date, location, description);

                titleEditText.setText("");
                descriptionEditText.setText("");
                tvDate.setText("");
                locationEditText.setText("");

            }
        });
        
    private void writeNewUser (String eventId, String title, String location, String date, String description){
            Event event = new Event (title, location, date, description);
            mDatabase.child(eventId).setValue(event);
        }

        private void getAllTasks (DataSnapshot dataSnapshot){
        ListView listView = (ListView) findViewById(R.id.listView);
            ArrayList<String> aEvents = new ArrayList<String>();

            for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
                Event event = singleSnapshot.getValue(Event.class);
                aEvents.add(event.getTitle());
            }

            ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, aEvents);
            listView.setAdapter(arrayAdapter);
        }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if ( requestCode == 100 && resultCode == RESULT_OK) {
            Place place = Autocomplete.getPlaceFromIntent(data);
            locationEditText.setText(place.getAddress());

        }else if (resultCode == AutocompleteActivity.RESULT_ERROR){
            Status status = Autocomplete.getStatusFromIntent(data);
            Toast.makeText(getApplicationContext(),status.getStatusMessage(),Toast.LENGTH_SHORT).show();
        }
    }
    //set the Gmail SDK
    public void open (View view) {
        Intent sendIntent = new Intent ();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(new StringBuilder()
                .append("<body><p>Content</p></body>")
                .toString()));
        sendIntent.setType("text/html");
        startActivity(sendIntent);

    }
}

我应该有一个空对象引用不存在),我正在尝试将其转换为字符串但我无法看到这个问题谢谢

error:java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
英文:

I am trying to connect my app with the Firebase database but it gives me a Fatal exception error: ( i did not post the rest of the code that I implemented before because it was working perfectly well. Just after the implementation of the code bellow I got an error )

package com.example.guideme;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.common.api.Status;
import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.widget.Autocomplete;
import com.google.android.libraries.places.widget.AutocompleteActivity;
import com.google.android.libraries.places.widget.model.AutocompleteActivityMode;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.example.guideme.Event;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
public class MainActivity extends AppCompatActivity {
EditText locationEditText, titleEditText,descriptionEditText;
Button publish, inviteFriends;
TextView tvDate;
ListView listView;
private static final String TAG =&quot;MainActivity&quot;;
private DatabaseReference mDatabase;
private DatePickerDialog.OnDateSetListener mDateSetListener;
public String title, description, date, location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//hooks to all elements in activity_main.xml
locationEditText = findViewById(R.id.locationEditText);
titleEditText = (EditText) findViewById(R.id.titleEditText);
descriptionEditText = (EditText) findViewById(R.id.descriptionEditText);
publish = (Button) findViewById(R.id.publish);
tvDate = (TextView) findViewById(R.id.tvDate);
inviteFriends = (Button) findViewById(R.id.inviteFriends);
listView = (ListView) findViewById(R.id.listView);
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
getAllTasks(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError error) {
//Failed to read value
Log.w(&quot;events&quot;, &quot;Failed to read value.&quot;, error.toException());
}
});
publish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
title = titleEditText.getText().toString();
date = tvDate.getText().toString();
description = descriptionEditText.getText().toString();
location = locationEditText.getText().toString();
String id = title.replaceAll(&quot;\\s&quot;,&quot;&quot;);
writeNewUser(id, title, date, location, description);
titleEditText.setText(&quot;&quot;);
descriptionEditText.setText(&quot;&quot;);
tvDate.setText(&quot;&quot;);
locationEditText.setText(&quot;&quot;);
}
});
private void writeNewUser (String eventId, String title, String location, String date, String description){
Event event = new Event (title, location, date, description);
mDatabase.child(eventId).setValue(event);
}
private void getAllTasks (DataSnapshot dataSnapshot){
ListView listView = (ListView) findViewById(R.id.listView);
ArrayList&lt;String&gt; aEvents = new ArrayList&lt;String&gt;();
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Event event = singleSnapshot.getValue(Event.class);
aEvents.add(event.getTitle());
}
ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, aEvents);
listView.setAdapter(arrayAdapter);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ( requestCode == 100 &amp;&amp; resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
locationEditText.setText(place.getAddress());
}else if (resultCode == AutocompleteActivity.RESULT_ERROR){
Status status = Autocomplete.getStatusFromIntent(data);
Toast.makeText(getApplicationContext(),status.getStatusMessage(),Toast.LENGTH_SHORT).show();
}
}
//set the Gmail SDK
public void open (View view) {
Intent sendIntent = new Intent ();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(new StringBuilder()
.append(&quot;&lt;body&gt;&lt;p&gt;Content&lt;/p&gt;&lt;/body&gt;&quot;)
.toString()));
sendIntent.setType(&quot;text/html&quot;);
startActivity(sendIntent);
}
}

I should have a null object reference (non-existant) that I am trying to convert to string, but I can not see this problem. Thank you.

error:java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

答案1

得分: 0

你会得到以下错误:

> 找不到符号方法getTitle()

因为在这个类com.google.firebase.events.Event中,没有getTitle()方法。然而,在你的项目中,有一个应该被导入的Event类。为了解决这个问题,只需将以下代码行进行修改:

import com.google.firebase.events.Event;

为:

import com.example.guideme.Event;
英文:

You get the following error:

> cannot find symbol method getTitle()

Because in this class com.google.firebase.events.Event, there is no getTitle() method. However, in your project, there is an Event class that should be imported. To solve this, simply change the following lines of code:

import com.google.firebase.events.Event;

To:

import com.example.guideme.Event;

huangapple
  • 本文由 发表于 2020年5月29日 23:32:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/62089530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定