Type mismatch: cannot convert from String to R.string

I'm a newbie, so forgive my primitive question, I really don't get it. I just create this array in my main activity by running a tutorial and I get an error

package com.TaskReminder; import android.R.string; import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter; public class ReminderListActivity extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.reminder_list); string[] items = new string[]{"aa","bb"}; ArrayAdapter<string> adapter = new ArrayAdapter<string>(this,R.layout.reminder_row,R.id.text1,items); setListAdapter(adapter); } 

and an error in my array of strings:

Several markers on this line - Linear breakpoint: ReminderListActivity [line: 14] - OnCreate (Bundle)

- type mismatch: cannot convert from String to R.string

what's going on here?

+2
source share
4 answers

Simple fix, string type should be uppercase.

 string[] items = new string[]{"aa","bb"}; ArrayAdapter<string> adapter = new ArrayAdapter<string>(this,R.layout.reminder_row,R.id.text1,items); 

becomes:

 string[] items = new string[]{"aa","bb"}; ArrayAdapter<string> adapter = new ArrayAdapter<string>(this,R.layout.reminder_row,R.id.text1,items); 

@gt_ebuddy is right, delete this line:

import android.R.string;

Using Ctrl + Shift + O is the easiest way to automatically import classes, but sometimes it leads you astray when it tries to import something from your R package file.

+6
source

Delete import. and be happy.

 import android.R.string; 

And note that

  • It must be a String . Not a String
  • String class is defined in the java.lang , and you will never need to import this package because they are automatically imported into every java program. The Java Doc says "Package java.lang : Provides classes that are fundamental to the design of the Java programming language."
+4
source

Remove import android.R.string; . I think you did this because of the error shown on this line, the line should be in your R.java file, which it is automatically generated, do not modify it. Clean your project and re-create. Hope it works !!!!

+1
source
 string[] items = new string[]{"aa","bb"}; ArrayAdapter<string> adapter = new ArrayAdapter<string>(this,R.layout.reminder_row,R.id.text1,items); 

change it to

 String[] items = new String[]{"aa","bb"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.reminder_row,R.id.text1,items) 
0
source

Source: https://habr.com/ru/post/1493935/


All Articles