gpt4 book ai didi

java - 类 Event 中的构造函数 Event 不能应用于给定类型;找不到符号方法 getTitle()

转载 作者:行者123 更新时间:2023-12-01 16:28:50 26 4
gpt4 key购买 nike

我正在尝试将我的应用程序与 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);

}
}

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

错误:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.String java.lang.Object.toString()”

最佳答案

您收到以下错误:

cannot find symbol method getTitle()

因为在此类 com.google.firebase.events.Event 中,没有 getTitle() 方法。但是,在您的项目中,应该导入一个 Event 类。要解决这个问题,只需更改以下代码行:

import com.google.firebase.events.Event;

致:

import com.example.guideme.Event;

关于java - 类 Event<T> 中的构造函数 Event 不能应用于给定类型;找不到符号方法 getTitle(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62089530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com