Hello.Please anyone help me with this code. I tried to look in every forum but nothing gives the right answer.
Promblem is: R cannot be resolved to a variable .
Here is my java code:
package com.example.twittersearch;
import java.util.Arrays;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
public class TwitterSearch extends Activity {
private SharedPreferences savedSearches;
TableLayout queryTableLayout;
EditText queryEditText, tagEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
savedSearches = getSharedPreferences("searchData", MODE_PRIVATE);
queryTableLayout = (TableLayout)findViewById(R.id.queryTableLayout);
queryEditText = (EditText)findViewById(R.id.queryEditText);
tagEditText = (EditText)findViewById(R.id.tagEditText);
Button saveButton = (Button)findViewById(R.id.saveButton);
saveButton.setOnClickListener(saveButtonListener);
Button clearTagsButton = (Button)findViewById(R.id.clearTagsButton);
clearTagsButton.setOnClickListener(clearTagsButtonListener);
refreshButtons(null);
}
private void refreshButtons(String newTag){
String[] tags = savedSearches.getAll().keySet().toArray(new String[0]);
Arrays.sort(tags, String.CASE_INSENSITIVE_ORDER);
if(newTag != null){
makeTagGUI(newTag, Arrays.binarySearch(tags, newTag));
}else{
for (int index = 0; index < tags.length; ++index)
makeTagGUI(tags[index], index);
}
}
private void makeTag(String query, String Tag){
String originalQuery = savedSearches.getString(tag, null);
SharedPreferences.Editor preferencesEditor = savedSearches.edit();
preferencesEditor.putString(tag, query);
preferencesEditor.apply();
if (originalQuery == null)
refreshButtons(tag);
}
private void makeTagGUI(String tag, int index){
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newTagView = inflater.inflate(R.layout.new_tag_view, null);
Button newTagButton = (Button) newTagView.findViewById(R.id.newTagButton);
newTagButton.setText(tag);
newTagButton.setOnClickListener(queryButtonListener);
Button newEditButton = (Button) newTagView.findViewById(R.id.newEditButton);
newEditButton.setOnClickListener(editButtonListener);
queryTableLayout.addView(newTagView, index);
}
private void clearButtons(){
queryTableLayout.removeAllViews();
}
public OnClickListener savedButtonListener = new OnClickListener(){
public void onClick(View v){
if(queryEditText.getText().length() > 0 && tagEdittext.getText().length() > 0){
makeTag(queryEditText.getText().toString(), tagEditText.getText().toString());
queryEditText.setText("");
tagEditText.setText("");
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(tagEditText.getWindowToken(),0);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(TwitterSearcg.this);
builder.setTitle(R.string.missingTitle);
builder.setPositiveButton(R.string.OK, null);
builder.setMessage(R.string.missingMessage);
AlertDialog errorDialog = builder.create();
errorDialog.show();
}
}
};
public OnClickListener clearTagsButtonListener = new OnClickListener(){
public void onClick(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(TwitterSearch.this);
builder.setTitle(R.string.confirmTitle);
builder.setPositiveButton(R.string.erase, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int button){
clearButtons();
SharedPreferences.Editor preferencesEditor = savedSearches.edit();
preferencesEditor.clear();
preferencesEditor.apply();
}
}
};
builder.setCancelable(true);
builder.setNegativeButton(R.string.cancel, null);
builder.setMessage(R.string.confirmMessage);
AlertDialog confirmDialog = builder.create();
confirmDialog.show();
}
};
public OnClickListener queryButtonListener = new OnClickListener(){
public void onClick(View v){
String buttonText = ((Button) v).getText().toString();
String query = savedSearches.getString(buttonText, "");
String urlString = getString(R.string.searchURL) + query;
Intent webIntent = new Intent(Intent.ACION_VIEW, Uri.parse(urlString));
startActivity(webIntent);
}
};
public OnClickListener editButtonListener = new OnClickListener(){
public void onClick(View v){
TableRow buttonTableRow = (TableRow)v.getParent();
Button searchButton = (Button)buttonTableRow.findViewById(R.id.newTagButton);
String tag = searchButton.getText().toString();
tagEditText.setText(Tag);
queryEditText.setText(savedSearches.getString(tag, ""));
}
};
}
Here is my main.xml located in res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroudColor="#FFFFFF"
android:padding="5dp"
android:stretchColumns="*"
>
<!-- tableRow0 -->
<TableRow
android:id="@+id/tableRow0"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/queryEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:hint="@string/queryPrompt"
android:imeOptions="actionNext"
android:inputType="text" >
</EditText>
</TableRow>
<!-- tableRow1 -->
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/tagEditText"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:hint="@string/tagPrompt"
android:imeOptions="actionDone"
android:inputType="text" >
</EditText>
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/save"
>
</Button>
</TableRow>
<!-- tableRow2 -->
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/glaring_blue" >
<EditText
android:id="@+id/taggedSearchesTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_span="2"
android:padding="5dp"
android:text="@string/taggedSearches"
android:textColor="@android:color/black"
android:textSize="18sp" >
</EditText>
</TableRow>
<!-- tableRow3 -->
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/glaring_blue" >
<ScrollView
android:id="@+id/queryScrollView"
android:layout_width="match_parent"
android:layout_span="2"
android:padding="5dp" >
<TableLayout
android:id="@+id/queryTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:strechColumns="*" >
</TableLayout>
</ScrollView>
</TableRow>
<!-- tableRow4 -->
<TableRow
android:id="@+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/clearTagsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_span="2"
android:text="@string/clearTags" >
</Button>
</TableRow>
</TableLayout>
and my strings located at layout/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TwitterSearch</string>
<string name="searchURL">http://search.twitter.com/search?q</string>
<string name="tagPrompt">Tag ypur query</string>
<string name="queryPrompt">Twitter search query</string>
<string name="taggedSearches">Tagged Searches</string>
<string name="edit">Edit</string>
<string name="clearTags">Clear Tags</string>
<string name="save">Save</string>
<string name="erase">Erase</string>
<string name="cancel">Cancel</string>
<string name="OK">OK</string>
<string name="missingTitle">Missing Text</string>
<string name="missingMessage">Please enter a search query and tag it.</string>
<string name="confirmTitle">Are you Sure?</string>
<string name="confirmMessage">THis will delete all saved searches</string>
</resources>
LOoking forward for your suggestions. Thank you!