Reputation: 108
See the bottom for things that I have tried. It gives me an error anywhere where R is mentioned which as I understand it, means there is an error in the xml. I cannot find it anywhere though.
package com.example.location_deals;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class AndroidListViewCursorAdaptorActivity extends Activity {
private CountriesDbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dbHelper = new CountriesDbAdapter(this);
dbHelper.open();
//Clean all data
dbHelper.deleteAllCountries();
//Add some data
dbHelper.insertSomeCountries();
//Generate ListView from SQLite Database
displayListView();}
private void displayListView() {
Cursor cursor = dbHelper.fetchAllCountries();
//The desired columns to be bound
String[] columns = new String[] {
CountriesDbAdapter.KEY_CODE,
CountriesDbAdapter.KEY_NAME,
CountriesDbAdapter.KEY_CONTINENT,
CountriesDbAdapter.KEY_REGION};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.code,
R.id.name,
R.id.continent,
R.id.region,};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.country_info,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
//Get the state's capital from this row in the database.
String countryCode =
cursor.getString(cursor.getColumnIndexOrThrow("code"));
Toast.makeText(getApplicationContext(),
countryCode, Toast.LENGTH_SHORT).show();}
});
EditText myFilter = (EditText) findViewById(R.id.myFilter);
myFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
dataAdapter.getFilter().filter(s.toString());
}
});
dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.fetchCountriesByName(constraint.toString());
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Code: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:text="Name: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:text="Continent: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:text="Region: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/continent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_toRightOf="@+id/textView3"
android:text="TextView"/>
<TextView
android:id="@+id/region"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignLeft="@+id/continent"
android:text="TextView"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView3"
android:layout_toRightOf="@+id/textView3"
android:text="TextView" />
<TextView
android:id="@+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_alignLeft="@+id/name"
android:text="TextView"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/some_text"
android:textSize="20sp"/>
<EditText android:id="@+id/myFilter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/some_hint">
<requestFocus />
</EditText>
<ListView android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.as400samplecode" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="17" />
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@android:style/Theme.Light">
<activity android:name=".AndroidListViewCursorAdaptorActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Things I have tried: deleted gen files, cleaned it, went through XML code 4 or 5 times. I've ensured it's imported right. I've checked R file (it has not been generated). This is the only error but it must be linked to XML but I cannot find it anywhere so hopefully one of you can tell me why I'm an idiot and I'll be very grateful. Thanks
Upvotes: 0
Views: 124
Reputation: 641
I understand that if your R file is not regenerating, it must be some problem with one of your xml files. Check all of the files in the res folder, including strings, menus, dimens etc. and also the Manifest. Sometimes, even if there is an error, it won't show up and you'll have to go through it carefully, making sure there isn't any error. A common error that I find people doing is deleting one of the strings that they have used in one of their other xml files.
If this doesn't work, try installing/updating all the build tools in the SDK Manager. Cleaning the project also helps. ALso be sure to check out your console because it´ll pop out a message of the the sort "stopping R generation because of xml error" or something like that. Check out the Probems tab as well to see if there are any errors. The problem my also be because of using invalid characters in file names, like capital letters. Sometimes, if you copy-paste the R file from another project it will automatically edit itself to be like the original one.
If none of these work, right-click on the gen folder and then click on "Restore from Local History" to restore the R file to an earlier working version.
Hope this helps!
Upvotes: 0
Reputation: 659
I am not sure but Now you can some step a head.
such as
1. delete Android.R (in above in your activity )
2. or replace package.R for example , com.example.name.R
3. check you res folder or sub-folder resources .such as layout attribute right or not.
4. Delete gen and bin folder.
5. clean your project thus: project=>clean
6. restart your eclipse.
Remember that if your resource not found then show this type error.
Best of luck!
Upvotes: 1
Reputation: 7343
R is an auto-generated Java file that should not be modified (accidentally or intentionally), usually, when Eclipse can't find R, that means that you have to start the project all over again because you can't manually create the R file and Eclipse would still recognize it.
However, try to clean your project and build it once more, if that does not work, then re-create your project by creating a new android project and copy-pasting your code in.
Upvotes: 0