无法解析方法也无法解析符号

时间:2020-04-12 23:02:35

标签: java android-studio android-adapter

我的项目的适配器部分目前有2个错误,我似乎无法弄清楚。它说无法解析符号EarthquakeAsyncTask,也无法解析方法execute。(java.lang.String)在下面的本节中:

EarthquakeAsyncTask task = new EarthquakeAsyncTask();
task.execute(USGS_REQUEST_URL);  

此活动的其余代码如下。让我知道你们是否有任何想法或需要更多项目代码,因为我还是android的新手。到目前为止,我已经尝试使缓存无效并重新启动。

package com.example.cs449project;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.example.cs449project.adapter.EarthquakeAdapter;
import com.google.android.gms.common.Feature;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

private static final String USGS_REQUEST_URL =
        "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&orderby=time&minmag=4&limit=10";

private EarthquakeAdapter mAdapter;

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

    ListView earthquakeListView = (ListView) findViewById(R.id.list);


    mAdapter = new EarthquakeAdapter(this, new ArrayList<Earthquake>());


    earthquakeListView.setAdapter(mAdapter);


    earthquakeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            Earthquake currentEarthquake = mAdapter.getItem(position);

            Uri earthquakeUri = Uri.parse(currentEarthquake.getUrl());

            Intent eqIntent = new Intent(Intent.ACTION_VIEW, earthquakeUri);
            startActivity(eqIntent);
        }
    });

    // Start the AsyncTask to fetch the earthquake data
    EarthquakeAsyncTask task = new EarthquakeAsyncTask();
    task.execute(USGS_REQUEST_URL);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater acc_inflater = getMenuInflater();
    acc_inflater.inflate(R.menu.signout, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    FirebaseAuth acc_a = FirebaseAuth.getInstance();
    switch(item.getItemId()){
        case R.id.Sign_out:
            acc_a.signOut();
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
            finish();
            break;
    }
    return super.onOptionsItemSelected(item);
}

}

这里是我的EarthquakeAdapter.java:

package com.example.cs449project.adapter;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import androidx.core.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.cs449project.Earthquake;
import com.example.cs449project.R;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class EarthquakeAdapter extends ArrayAdapter<Earthquake> {

private static final String LOCATION_SEPARATOR = " of ";

public EarthquakeAdapter(Context context, List<Earthquake> earthquakes) {
    super(context, 0, earthquakes);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.earthquake_list_item, parent, false);
    }

    Earthquake currentEarthquake = getItem(position);

    TextView magnitudeView = (TextView) listItemView.findViewById(R.id.magnitude);

    String formattedMagnitude = formatMagnitude(currentEarthquake.getMagnitude());

    magnitudeView.setText(formattedMagnitude);

    GradientDrawable magnitudeCircle = (GradientDrawable) magnitudeView.getBackground();

    int magnitudeColor = getMagnitudeColor(currentEarthquake.getMagnitude());

    magnitudeCircle.setColor(magnitudeColor);

    String originalLocation = currentEarthquake.getLocation();

    String primaryLocation;

    String locationOffset;

    if (originalLocation.contains(LOCATION_SEPARATOR)) {

        String[] parts = originalLocation.split(LOCATION_SEPARATOR);

        locationOffset = parts[0] + LOCATION_SEPARATOR;

        primaryLocation = parts[1];
    } else {
        locationOffset = "Near this";

        primaryLocation = originalLocation;
    }

    TextView primaryLocationView = (TextView) listItemView.findViewById(R.id.primary_location);

    locationOffsetView.setText(locationOffset);

    Date dateObject = new Date(currentEarthquake.getTimeInMilliseconds());

    TextView dateView = (TextView) listItemView.findViewById(R.id.date);

    String formattedDate = formatDate(dateOject);

    timeView.setText(formattedTime);

    return listItemView;
}

private int getMagnitudeColor(double magnitude) {
    int magnitudeColorResourceId;
    int magnitudeFloor = (int) Math.floor(magnitude);
    switch (magnitudeFloor) {
        case 0:
        case 1:
            magnitudeColorResourceId = R.color.magnitude1;
            break;
        case 2:
            magnitudeColorResourceId = R.color.magnitude2;
            break;
        case 3:
            magnitudeColorResourceId = R.color.magnitude3;
            break;
        case 4:
            magnitudeColorResourceId = R.color.magnitude4;
            break;
        case 5:
            magnitudeColorResourceId = R.color.magnitude5;
            break;
        case 6:
            magnitudeColorResourceId = R.color.magnitude6;
            break;
        case 7:
            magnitudeColorResourceId = R.color.magnitude7;
            break;
        case 8:
            magnitudeColorResourceId = R.color.magnitude8;
            break;
        case 9:
            magnitudeColorResourceId = R.color.magnitude9;
            break;
        default:
            magnitudeColorResourceId = R.color.magnitude10plus;
            break;
    }

    return ContextCompat.getColor(getContext(), magnitudeColorResourceId);
}

/**
 * Return the formatted magnitude string showing 1 decimal place (i.e. "3.2")
 * from a decimal magnitude value.
 */
private String formatMagnitude(double magnitude) {
    DecimalFormat magnitudeFormat = new DecimalFormat("0.0");
    return magnitudeFormat.format(magnitude);
}

/**
 * Return the formatted date string (i.e. "Mar 3, 1984") from a Date object.
 */
private String formatDate(Date dateObject) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("LLL dd, yyyy");
    return dateFormat.format(dateObject);
}

/**
 * Return the formatted date string (i.e. "4:30 PM") from a Date object.
 */
private String formatTime(Date dateObject) {
    SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm a");
    return timeFormat.format(dateObject);
}
}

1 个答案:

答案 0 :(得分:1)

我没有看到import的{​​{1}},并且编译器也没有看到它,因此我和编译器告诉您“无法解析符号EarthquakeAsyncTask并且无法解析方法执行” 。导入类以解决问题。