简体   繁体   中英

Android cannot be resolved or is not a field

Alright so before you go and start stating, go look around as your title matches many questions asked here. And in that case, you're correct, but... Every single thing I've tried on these asked questions didn't help me at all.

So I'm actually trying to build an RssFeed application for Android devices (Still Learning). My problem comes in the MainActivity, mind you. All my classes, xmls, and other content are properly coded with no issues or errors. People are saying that you should get rid of the imports that contain "android.r" and I've looked throught and haven't found any imports that contain the said quotation.

My MainActivity code:

package com.ascendapps.licknriff;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.ascendapps.licknriff.R;

public class MainActivity extends Activity {

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


        try {

            RssReader rssReader = new RssReader("http://www.licknriff.com/feed/");
            ListView Items = (ListView)findViewById(R.id.listView1);
            // create a list adapter
            ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this.android.R.layout.simple_list_item_1, RssReader.getItems());
            // --
            Items.setAdapter(adapter);
            // --
            Items.setOnItemClickListener(new ListListener(RssReader.getItems(), this));     }catch (Exception e){
            Log.e("SimpleRssReader", e.getMessage());

        }
    }
}

Change this:

ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this.android.R.layout.simple_list_item_1, RssReader.getItems());

to this:

ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this, android.R.layout.simple_list_item_1, RssReader.getItems());

this should be passed as context in constructor of ArrayAdapter class.

Replace

ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this.android.R.layout.simple_list_item_1, RssReader.getItems());

with

ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this, android.R.layout.simple_list_item_1, RssReader.getItems());

android is the namespace of the Android R class. It doesn't belong to your activity class.

The first parameter is of Context type, so you activity is a suitable parameter as it inherits from Context .

You have a full-stop, but you need a comma, after this here -

// ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(
//     this.android.R.layout.simple_list_item_1, RssReader.getItems());
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(
    this, android.R.layout.simple_list_item_1, RssReader.getItems());

I think you have typo error. While writing code, you simply type . ( dot ) instead of ',' ( comma ). Just replace . with , after this like below,

ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this, android.R.layout.simple_list_item_1, RssReader.getItems());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM