我想制作一个应用程序,用于检查用户输入的字符串中的滥用单词,如果没有滥用单词,则会发布到其他应用程序,如果用户输入的字符串包含滥用单词,则会生成弹出窗口
这是我在普通java中的代码滥用单词检查工作正常,但在android中它只是崩溃。
我正在给出logcat的错误报告
06-26 14:44:27.333: W/System.err(338): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-26 14:44:27.343: W/System.err(338): at java.lang.reflect.Method.invokeNative(Native Method)
06-26 14:44:27.343: W/System.err(338): at java.lang.reflect.Method.invoke(Method.java:507)
06-26 14:44:27.353: W/System.err(338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-26 14:44:27.353: W/System.err(338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-26 14:44:27.363: W/System.err(338): at dalvik.system.NativeStart.main(Native Method)
06-26 14:44:27.373: D/AndroidRuntime(338): Shutting down VM
06-26 14:44:27.373: W/dalvikvm(338): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-26 14:44:27.383: E/AndroidRuntime(338): FATAL EXCEPTION: main
06-26 14:44:27.383: E/AndroidRuntime(338): java.lang.NullPointerException
06-26 14:44:27.383: E/AndroidRuntime(338): at com.vssgatekeeper.Main$1.onClick(Main.java:62)
06-26 14:44:27.383: E/AndroidRuntime(338): at android.view.View.performClick(View.java:2485)
06-26 14:44:27.383: E/AndroidRuntime(338): at android.view.View$PerformClick.run(View.java:9080)
06-26 14:44:27.383: E/AndroidRuntime(338): at android.os.Handler.handleCallback(Handler.java:587)
06-26 14:44:27.383: E/AndroidRuntime(338): at android.os.Handler.dispatchMessage(Handler.java:92)
06-26 14:44:27.383: E/AndroidRuntime(338): at android.os.Looper.loop(Looper.java:123)
06-26 14:44:27.383: E/AndroidRuntime(338): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-26 14:44:27.383: E/AndroidRuntime(338): at java.lang.reflect.Method.invokeNative(Native Method)
06-26 14:44:27.383: E/AndroidRuntime(338): at java.lang.reflect.Method.invoke(Method.java:507)
06-26 14:44:27.383: E/AndroidRuntime(338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-26 14:44:27.383: E/AndroidRuntime(338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-26 14:44:27.383: E/AndroidRuntime(338): at dalvik.system.NativeStart.main(Native Method)
06-26 14:44:31.723: I/Process(338): Sending signal. PID: 338 SIG: 9
06-26 14:51:24.365: D/dalvikvm(376): GC_EXTERNAL_ALLOC freed 50K, 53% free 2552K/5379K, external 716K/1038K, paused 100ms
使用以下代码:
package com.vssgatekeeper;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.vssgatekeeper.library.DatabaseHandler;
import com.vssgatekeeper.library.UserFunctions;
public class Main extends Activity
{
Button btnLogout, share;
Button changepas;
EditText post;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
changepas = (Button) findViewById(R.id.btchangepass);
btnLogout = (Button) findViewById(R.id.logout);
share = (Button) findViewById(R.id.share);
post = (EditText) findViewById(R.id.comment);
String comment=post.getText().toString();
share.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(new File("C:\\Users\\Prateek\\Desktop\\abusivewords.txt")));
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String inputLine = null;
HashSet dictionary = new HashSet();
try
{
while((inputLine = reader.readLine()) != null)
{
String[] words = inputLine.split("\\n+");
if(inputLine.equals("")) continue;
for(String word: words)
{
word = word.replace(".", "");
word = word.replace(",", "");
dictionary.add(word);
}
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
reader.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String search[] = null;
int exit=0;
boolean found=false;
do
{
search=post.getText().toString().split(" ");
for(String srch : search)
{
if(dictionary.contains(srch))
{
found = true;
}
}
if(!found)
{
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, post.getText().toString().concat("\n").concat("@ Posted By GateKeeper"));
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, post.getText().toString().concat("\n").concat("@ Posted By GateKeeper")));
}
search = null;
}while(exit==0);
}
});
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
/**
* Hashmap to load data from the Sqlite database
**/
HashMap<String, String> user = new HashMap<String, String>();
user = db.getUserDetails();
/**
* Change Password Activity Started
**/
changepas.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
Intent chgpass = new Intent(getApplicationContext(),
ChangePassword.class);
startActivity(chgpass);
}
});
/**
* Logout from the User Panel which clears the data in Sqlite database
**/
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
UserFunctions logout = new UserFunctions();
logout.logoutUser(getApplicationContext());
Intent login = new Intent(getApplicationContext(), Login.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
finish();
}
});
/**
* Sets user first name and last name in text view.
**/
final TextView login = (TextView) findViewById(R.id.textwelcome);
login.setText("Welcome " + user.get("fname"));
final TextView lname = (TextView) findViewById(R.id.lname);
lname.setText(user.get("lname"));
}
}
答案 0 :(得分:0)
就像拉尔托所说的那样。 错误源于此处:
reader = new BufferedReader(new FileReader(new File(&#34; C:\ Users \ Prateek \ Desktop \ abusivewords.txt&#34;));
&#34; abusivewords.txt&#34;应该存在于您的项目中,而不是在桌面文件夹中
最佳解决方案是放置&#34; abusivewords.txt&#34;在项目的assets文件夹中,并在代码中调用它,如下所示:
AssetManager mngr = getApplicationContext()。getAssets(); InputStream = mngr.open(&#34; textdb.txt&#34;);
答案 1 :(得分:0)
将您的abusivewords.txt
文件放入SD卡并将路径从"C:\\Users\\Prateek\\Desktop\\abusivewords.txt")
更改为SD卡
您可以从以下代码获取SD卡的路径:
File sdCardRoot = Environment.getExternalStorageDirectory();
File myDir = new File(sdCardRoot, "Your file name");
您将在此处获取目录路径myDir
答案 2 :(得分:0)
reader = new BufferedReader(new FileReader(new File("C:\\Users\\Prateek\\Desktop\\abusivewords.txt")));
您的Android设备无法读取存储在计算机C盘中的文件。您需要在本地存储文件,即在您的应用程序中存储。因此,将文件放在应用程序的assets文件夹中。
然后您就可以阅读该文件:
StringBuilder buf=new StringBuilder();
InputStream json=getAssets().open("abusivewords.txt");
BufferedReader in=
new BufferedReader(new InputStreamReader(json, "UTF-8"));
String str;
while ((str=in.readLine()) != null) {
buf.append(str);
}
in.close();
答案 3 :(得分:0)
由于没有C:驱动器或者没有像您在移动设备上指定的那样的路径,因此&#34;新文件&#34;不存在,您可以用externalStorageDirectory
的路径替换该部分,如:
//associated with the path
File externalStorageDir = Environment.getExternalStorageDirectory();
reader = new BufferedReader(new FileReader(new File(externalStorageDir, "abusivewords.txt")));
答案 4 :(得分:0)
将abusivewords.txt放入资产项目文件夹并替换
reader = new BufferedReader(new FileReader(new File("C:\\Users\\Prateek\\Desktop\\abusivewords.txt")));
通过
reader = new BufferedReader(new InputStreamReader(Main.this.getAssets().open("abusivewords.txt"))