【发布时间】:2017-03-09 10:39:03
【问题描述】:
编辑:自从我收到对该主题的回复以来,我已经对此进行了一些更新。这是新线程:
我一直在制作一个 Android 应用程序,您可以在其中保存自己喜欢的网站。到目前为止,这是我在“MainActivity.java”文件中得到的代码:
package cory.assignment.favoritewebsitesapp;
import java.util.ArrayList;
import java.util.Collections;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends ListActivity {
private static final String SEARCHES = "searches";
private EditText queryEditText;
private EditText tagEditText;
private SharedPreferences savedSearches;
private ArrayList<String> tags;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
queryEditText = (EditText) findViewById(R.id.queryEditText);
tagEditText = (EditText) findViewById(R.id.tagEditText);
savedSearches = getSharedPreferences(SEARCHES, MODE_PRIVATE);
tags = new ArrayList<String>(savedSearches.getAll().keySet());
Collections.sort(tags, String.CASE_INSENSITIVE_ORDER);
adapter = new ArrayAdapter<String>(this, R.layout.list_item, tags);
setListAdapter(adapter);
ImageButton saveButton = (ImageButton) findViewById(R.id.saveButton);
saveButton.setOnClickListener(saveButtonListener);
getListView().setOnItemClickListener(itemClickListener);
getListView().setOnItemLongClickListener(itemLongClickListener);
}
public OnClickListener saveButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
if (queryEditText.getText().length() > 0 && tagEditText.getText().length() > 0)
{
addTaggedSearch(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(MainActivity.this);
builder.setMessage(R.string.missingMessage);
builder.setPositiveButton(R.string.OK, null);
AlertDialog errorDialog = builder.create();
errorDialog.show();
}
}
};
private void addTaggedSearch(String query, String tag)
{
SharedPreferences.Editor preferencesEditor = savedSearches.edit();
preferencesEditor.putString(tag, query);
preferencesEditor.apply();
if (!tags.contains(tags))
{
tags.add(tag);
Collections.sort(tags, String.CASE_INSENSITIVE_ORDER);
adapter.notifyDataSetChanged();
}
}
OnItemClickListener itemClickListener = new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String tag = ((TextView) view).getText().toString();
String urlString = getString(R.string.searchURL) + Uri.encode(savedSearches.getString(tag, ""), "UTF-8");
Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlString));
startActivity(webIntent);
}
};
OnItemLongClickListener itemLongClickListener =
new OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
final String tag = ((TextView) view).getText().toString();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(getString(R.string.shareEditDeleteTitle, tag));
builder.setItems(R.array.dialog_items, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
switch (which)
{
case 0:
shareSearch(tag);
break;
case 1:
tagEditText.setText(tag);
queryEditText.setText(savedSearches.getString(tag, ""));
break;
case 2:
deleteSearch(tag);
break;
}
}
}
);
builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
}
);
builder.create().show();
return true;
}
};
private void shareSearch(String tag)
{
String urlString = getString(R.string.searchURL) + Uri.encode(savedSearches.getString(tag, ""), "UTF-8");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.shareSubject));
shareIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.shareMessage, urlString));
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent, getString(R.string.shareSearch)));
}
private void deleteSearch(final String tag)
{
AlertDialog.Builder confirmBuilder = new AlertDialog.Builder(this);
confirmBuilder.setMessage(getString(R.string.confirmMessage, tag));
confirmBuilder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
}
);
confirmBuilder.setPositiveButton(getString(R.string.delete), new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
tags.remove(tag);
SharedPreferences.Editor preferencesEditor = savedSearches.edit();
preferencesEditor.remove(tag);
preferencesEditor.apply();
adapter.notifyDataSetChanged();
}
}
);
confirmBuilder.create().show();
}
}
但是,我得到了某些行的代码错误:
第 79 行:OK 无法解析或不是字段
第 146 行:cancel 无法解析或不是字段
第 177 行:cancel 无法解析或不是字段
第 186 行:删除无法解析或不是字段
下面的链接显示了我对 activity_main.xml 的内容: Click here for the image
这些行可能有什么问题?这些行有问题,因为它们与this page 上的信息有关。
我也在使用 proram Eclipse Java EE IDE for Web Developers 来制作这个,以防你想知道我使用的是什么程序。我也在使用 API 18 来解决这个问题。
如果您想查看其他文件,请告诉我。
【问题讨论】:
-
这和 JavaScript 有关系吗?
-
我认为这些消息是不言自明的,即
Line 77: missingMessage cannot be resolved or is not a field代表builder.setMessage(R.string.missingMessage);几乎说明了一切。 -
这个问题太笼统了。你不能只是转储你的整个代码和一堆错误的清单。见how to ask a good question。此外,我们鼓励您使用 Android Studio 进行应用程序开发。不清楚你为什么使用 Eclipse Web 开发版本
-
关于
deleteSearch(final string tag)错误...string不是变量类型。 Java 区分大小写 -
我把它放在 javascript 类别中,因为我认为 '.java' 文件与此相关。无论如何,我对我的问题进行了编辑,因为我修复了大部分错误。