我想製作一個應用程序,將檢查用戶輸入的字符串中的濫用詞,如果沒有辱罵的話,那麼它會張貼到其他應用程序,如果用戶介入的字符串包含辱罵詞,然後會產生一個彈出窗口。項目崩潰在Android
這是我的代碼在正常的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"));
}
}
提示更換
:您的Android設備不具有'C:'路徑。 – laalto
想要解決此問題, – user3770417
將文件路徑更改爲設備上存在的文件。 – Apoorv