2014-06-26 120 views
-1

我想製作一個應用程序,將檢查用戶輸入的字符串中的濫用詞,如果沒有辱罵的話,那麼它會張貼到其他應用程序,如果用戶介入的字符串包含辱罵詞,然後會產生一個彈出窗口。項目崩潰在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")); 
    } 
} 
+7

提示更換

reader = new BufferedReader(new FileReader(new File("C:\\Users\\Prateek\\Desktop\\abusivewords.txt"))); 

:您的Android設備不具有'C:'路徑。 – laalto

+0

想要解決此問題, – user3770417

+0

將文件路徑更改爲設備上存在的文件。 – Apoorv

回答

0

它像laalto說。 從這裏的誤差根:

讀者=新的BufferedReader(新的FileReader(新文件( 「C:\用戶\ Prateek \桌面\ abusivewords.txt」)));

的「abusivewords.txt」應該在項目中存在的,不是你的桌面文件夾內

最好的解決辦法是地方「abusivewords.txt」項目的資產裏面的文件夾並將其命名爲在你的代碼是這樣的:

AssetManager mngr = getApplicationContext()。getAssets(); InputStream is = mngr.open(「textdb.txt」);

+0

這是行不通的sir – user3770417

+0

仍然在logcat中顯示相同的錯誤 – user3770417

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

+1

但隨機用戶如何/爲什麼將文件放在他的SD卡中。他將資產存儲起來會不會更好? –

0
reader = new BufferedReader(new FileReader(new File("C:\\Users\\Prateek\\Desktop\\abusivewords.txt"))); 

您的android設備無法讀取存儲在C驅動器中的文件電腦的電子郵件。您需要在本地存儲文件,即在您的應用程序中。因此,將該文件放在應用程序的資產文件夾中。

然後,您就可以讀取該文件:

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(); 
0

由於沒有C:驅動器或像你在移動,「新文件」指定的一個沒有這樣的路徑沒有按」牛逼的存在,你可以通過externalStorageDirectory像路徑替換部分:

//associated with the path 
File externalStorageDir = Environment.getExternalStorageDirectory(); 
reader = new BufferedReader(new FileReader(new File(externalStorageDir, "abusivewords.txt"))); 

欲瞭解更多信息:
Storage Options - Android Developers
Saving files

0

放置濫用詞。txt中置入資產項目文件夾和

reader = new BufferedReader(new InputStreamReader(Main.this.getAssets().open("abusivewords.txt"))