Java 安卓系统的项目正在崩溃

Java 安卓系统的项目正在崩溃,java,android,Java,Android,我想做一个应用程序,它将检查用户输入的字符串中的辱骂词,如果没有辱骂词,那么它将发布到其他应用程序,如果用户输入的字符串包含辱骂词,那么将生成一个弹出窗口 下面是我在普通java中的代码,滥用词检查工作正常,但在android中它只是崩溃 我给出了logcat的错误报告 06-26 14:44:27.333: W/System.err(338): at android.app.ActivityThr

我想做一个应用程序,它将检查用户输入的字符串中的辱骂词,如果没有辱骂词,那么它将发布到其他应用程序,如果用户输入的字符串包含辱骂词,那么将生成一个弹出窗口

下面是我在普通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"));
    }
}
package com.vssgatekeeper;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.HashMap;
导入java.util.HashSet;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入com.vssgatekeeper.library.DatabaseHandler;
导入com.vssgatekeeper.library.UserFunctions;
公共类主要扩展活动
{
按钮btnLogout,共享;
按钮转换;
编辑文本帖子;
/**
*在首次创建活动时调用。
*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
changepas=(按钮)findviewbyd(R.id.btchangepass);
btnLogout=(按钮)findViewById(R.id.logout);
share=(按钮)findViewById(R.id.share);
post=(EditText)findViewById(R.id.comment);
String comment=post.getText().toString();
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v)
{
BufferedReader reader=null;
尝试
{
reader=new BufferedReader(新文件阅读器(新文件(“C:\\Users\\Prateek\\Desktop\\abusivewords.txt”));
}
catch(filenotfounde异常)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
字符串inputLine=null;
HashSet dictionary=新HashSet();
尝试
{
而((inputLine=reader.readLine())!=null)
{
String[]words=inputLine.split(\\n+);
如果(inputLine.equals(“”)继续;
for(字符串字:字)
{
word=word。替换(“.”,“”);
word=word.replace(“,”,”);
添加(单词);
}
}
}
捕获(IOE异常)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
尝试
{
reader.close();
}
捕获(IOE异常)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
字符串搜索[]=null;
int exit=0;
布尔值=false;
做
{
search=post.getText().toString().split(“”);
for(字符串srch:search)
{
if(dictionary.contains(srch))
{
发现=真;
}
}
如果(!找到)
{
Intent sendIntent=新Intent();
sendIntent.setAction(Intent.ACTION\u SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,post.getText().toString().concat(“\n”).concat(“@postedbygatekeeper”);
sendIntent.setType(“文本/普通”);
startActivity(Intent.createChooser(sendIntent,post.getText().toString().concat(“\n”).concat(“@postedbygatekeeper”));
}
搜索=空;
}而(出口==0);
}
});
DatabaseHandler db=新的DatabaseHandler(getApplicationContext());
/**
*Hashmap从Sqlite数据库加载数据
**/
HashMap用户=新建HashMap();
user=db.getUserDetails();
/**
*更改密码活动已启动
**/
changepas.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图arg0)
{
Intent chgpass=新的Intent(getApplicationContext(),
更改密码(类);
星触觉(chgpass);
}
});
/**
*从清除Sqlite数据库中数据的用户面板注销
**/
btnLogout.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图arg0)
{
UserFunctions注销=新建UserFunctions();
logout.logoutUser(getApplicationContext());
Intent login=newintent(getApplicationContext(),login.class);
login.addFlags(Intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
startActivity(登录);
完成();
}
});
/**
*在文本视图中设置用户名和姓氏。
**/
最终TextView登录=(TextView)findViewById(R.id.textwelcome);
login.setText(“欢迎”+user.get(“fname”);
最终TextView lname=(TextView)findViewById(R.id.lname);
lname.setText(user.get(“lname”);
}
}
就像拉阿尔托说的那样。 错误根源如下:File sdCardRoot = Environment.getExternalStorageDirectory(); File myDir = new File(sdCardRoot, "Your file name");
reader = new BufferedReader(new FileReader(new File("C:\\Users\\Prateek\\Desktop\\abusivewords.txt")));
    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();
//associated with the path   
 File externalStorageDir = Environment.getExternalStorageDirectory();
 reader = new BufferedReader(new FileReader(new File(externalStorageDir, "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"))