英文:
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 ="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);
}
}
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论