开发者

R.id cannot be resolved on android ListActivity

开发者 https://www.devze.com 2023-04-01 05:45 出处:网络
I am getting this strange error: R.id cannot be resolved on lines: WebView myWebView = (WebView) findViewById(view.R.id.webview);

I am getting this strange error:

  R.id cannot be resolved 

on lines:

 WebView myWebView = (WebView) findViewById(view.R.id.webview);
 myWebView.loadUrl(s);

I tried to clean the project and restart it. here is my code:

public class NewsActivity  extends ListActivity {
 public ReadXML ReadXML=new ReadXML();
 public  ArrayList<String> ynetList =new ArrayList<String>();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

for(int i=0;i<ReadXML.hadashotListItems.size();i++)
ynetList.add(ReadXML.hadashotListItems.get(i).title+"\n"+ReadXML.hadashotListItems.get(i).pubDate);

      setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, ynetList));
    //  setContentView(R.layout.main);
      ListView lv = getListView();
      lv.setTextFilterEnabled(true);

      lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
          // When clicked, show a toast with the TextView text
        String s=   ReadXML.hadashotListItems.get(position).link;

        WebView myWebView = (WebView) findViewById(view.R.id.webview);
        myWebView.loadUrl(s);
    //


      //Toast.makeText(getApplicationContext(((TextView)view).getText(),Toast.LENGTH_SHORT).show();
        }
      }
      );
    }}

my xml code is: list_item.xml

  <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="15sp" >
</TextView>


<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:i开发者_开发知识库d="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

</LinearLayout>

thanks for help!


The message R.id cannot be resolved means that the R class is not properly built or at least is not according to your file list_item.xml as it defines an id.

The probable causes are the following:

  • Eclipse is lost and does not manage to build your project properly (this happens sometime): try to clean and rebuild your project (menu project/clean of Eclipse).

  • Sometimes this is not sufficient either, so try to

    • clean you project
    • modify your AndroidManifest.xml by adding a blank char in it
    • Close your project in Eclipse
    • Open it again
    • Re-clean it and build it


  • Your file list_item.xml is not in the right directory. It should be in the directory [project_root]/res/layout

  • You have an Android framework installation problem. Check in the menu "Windows/Android SDK and ADT manager" of Eclipse whether everything is fine.


It should be just

WebView myWebView = (WebView) findViewById(R.id.webview); 

No view before the R


try this way

WebView myWebView = (WebView) findViewById(R.id.webview);
0

精彩评论

暂无评论...
验证码 换一张
取 消