2013-08-01 226 views
1

我有一個應用程序應該從服務器獲取一些字符串,這取決於網頁,每次運行時它都會崩潰。我不明白如何調試它,我已經嘗試了很多變化。自從我搞砸了GUI之後,它開始崩潰,並增加了切換視圖的功能,如果它提供任何形式的幫助。Android應用程序開始崩潰

下面是代碼:

public class MainActivity extends Activity implements TextToSpeech.OnInitListener, OnClickListener{ 


    private WebView mWebview; 
    EditText addressBar; 
    String currentWebpage = "http://www.aljazeera.com/news/americas/2013/07/20137113200544375.html"; 
    LinearLayout viewGroup = (LinearLayout) findViewById(R.id.linearview); 
    View main = (View) findViewById(R.layout.activity_main); 
    View readOut = (View) findViewById(R.layout.read_out); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     System.out.println("Showing Activity_Main..."); 
     viewGroup.addView(View.inflate(this, R.layout.activity_main, null)); 
     super.onCreate(savedInstanceState); 
     //setContentView(R.menu.main); 
     addressBar = (EditText)findViewById(R.id.addressBar); 
     addressBar.setText(currentWebpage); 

     mWebview = (WebView)findViewById(R.id.webview); 
     mWebview.getSettings().setJavaScriptEnabled(true); // enables javascript 
     mWebview.setWebViewClient(new WebViewClient()); 

     System.out.println("Loading Webpage..."); 

     mWebview.loadUrl(currentWebpage); 

    } 

    public void speakOut(String text) { 
     TextToSpeech tts = new TextToSpeech(this, this); 
     System.out.println("Speaking"); 
     if(tts.isLanguageAvailable(Locale.ENGLISH) != -1){ 
      tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 
     } 
    } 

    public int speakFull(String text){ 
     switchToRead(); 
     String[] sentences = text.split("\n|\\.(?!\\d)|(?<!\\d)\\."); // Regex that splits the body of text into the sentences of that body which are stored in a String array. 
     for(int i = 0; i < sentences.length; i++){ 
      speakOut(sentences[i]); 
      if(i == sentences.length - 1){ 
       return 1; 
      } 
     } 
     return 0; 
    } 

    public String getText(String webPage) throws ParseException, IOException{ 
     HttpResponse response = null; 
     try {   
       HttpClient client = new DefaultHttpClient(); 
       HttpGet request = new HttpGet(); 
       request.setURI(new URI("http://someserver.net:8080/" + webPage)); 
       response = client.execute(request); 
      } catch (URISyntaxException e) { 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     String responseBody = "No text found on webpage."; 
     int responseCode = response.getStatusLine().getStatusCode(); 
     switch(responseCode) { 
     case 200: 
     HttpEntity entity = response.getEntity(); 
      if(entity != null) { 
       responseBody = EntityUtils.toString(entity); 
      } 
     } 
     System.out.println("Returning Response.."); 
     System.out.println(responseBody); 
     return responseBody; 
} 


    @Override 
    public void onInit(int status) { 
     // TODO Auto-generated method stub 

    } 

private class tts extends AsyncTask<String, Void, String>{//Async http request to get text 

    @Override 
    protected String doInBackground(String... arg0) { 
     try { 
      System.out.println("Running seperate thread for TTS."); 
      int complete = 0; 
      while(complete == 0){ 
       System.out.println("Speaking full.."); 
       complete = speakFull(getText(mWebview.getUrl())); 
      } 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    protected void onPostExecute(String result){ 

    } 

} 

public void clickPlay(View v){ 
    new tts().execute(""); 
} 

public void clickGo(View v){ 
    if(addressBar.getText() != null){ 
     currentWebpage = addressBar.getText().toString(); 
     System.out.println("Current webpage changed to: " + currentWebpage); 
     mWebview.loadUrl(currentWebpage); 
    } 
} 

public void clickPause(View v){ 
    System.out.println("Clicked pause."); 
} 

@Override 
public void onClick(DialogInterface dialog, int which) { 
    // TODO Auto-generated method stub 

} 

public void switchToRead(){// Switches to the reading view which displays the text that the tts engine reads off. 
    System.out.println("Swtiching view to Read."); 
    viewGroup.removeView(main); 
    viewGroup.addView(View.inflate(this, R.layout.read_out, null)); 
} 

public void switchToMain(){ 
    System.out.println("Switching view to Main."); 
    viewGroup.removeView(readOut); 
    viewGroup.addView(View.inflate(this, R.layout.activity_main, null)); 
} 

} 

而且這裏有許多錯誤啓動我的應用程序,當我遇到:

08-01 14:53:10.210: E/Trace(812): error opening trace file: No such file or directory (2) 
08-01 14:53:10.600: D/AndroidRuntime(812): Shutting down VM 
08-01 14:53:10.631: W/dalvikvm(812): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
08-01 14:53:10.660: E/AndroidRuntime(812): FATAL EXCEPTION: main 
08-01 14:53:10.660: E/AndroidRuntime(812): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.webview/com.example.webview.MainActivity}: java.lang.NullPointerException 
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106) 
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
08-01 14:53:10.660: E/AndroidRuntime(812): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-01 14:53:10.660: E/AndroidRuntime(812): at android.os.Looper.loop(Looper.java:137) 
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.main(ActivityThread.java:5041) 
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.reflect.Method.invokeNative(Native Method) 
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.reflect.Method.invoke(Method.java:511) 
08-01 14:53:10.660: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
08-01 14:53:10.660: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
08-01 14:53:10.660: E/AndroidRuntime(812): at dalvik.system.NativeStart.main(Native Method) 
08-01 14:53:10.660: E/AndroidRuntime(812): Caused by: java.lang.NullPointerException 
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.Activity.findViewById(Activity.java:1839) 
08-01 14:53:10.660: E/AndroidRuntime(812): at com.example.webview.MainActivity.<init>(MainActivity.java:39) 
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.Class.newInstanceImpl(Native Method) 
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.Class.newInstance(Class.java:1319) 
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.Instrumentation.newActivity(Instrumentation.java:1054) 
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 
08-01 14:53:10.660: E/AndroidRuntime(812): ... 11 more 
08-01 14:53:27.439: D/AndroidRuntime(860): Shutting down VM 
08-01 14:53:27.439: W/dalvikvm(860): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
08-01 14:53:27.449: E/AndroidRuntime(860): FATAL EXCEPTION: main 
08-01 14:53:27.449: E/AndroidRuntime(860): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.webview/com.example.webview.MainActivity}: java.lang.NullPointerException 
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106) 
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
08-01 14:53:27.449: E/AndroidRuntime(860): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-01 14:53:27.449: E/AndroidRuntime(860): at android.os.Looper.loop(Looper.java:137) 
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.main(ActivityThread.java:5041) 
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.reflect.Method.invokeNative(Native Method) 
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.reflect.Method.invoke(Method.java:511) 
08-01 14:53:27.449: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
08-01 14:53:27.449: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
08-01 14:53:27.449: E/AndroidRuntime(860): at dalvik.system.NativeStart.main(Native Method) 
08-01 14:53:27.449: E/AndroidRuntime(860): Caused by: java.lang.NullPointerException 
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.Activity.findViewById(Activity.java:1839) 
08-01 14:53:27.449: E/AndroidRuntime(860): at com.example.webview.MainActivity.<init>(MainActivity.java:39) 
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.Class.newInstanceImpl(Native Method) 
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.Class.newInstance(Class.java:1319) 
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.Instrumentation.newActivity(Instrumentation.java:1054) 
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 
08-01 14:53:27.449: E/AndroidRuntime(860): ... 11 more 
+0

爲了清晰你可以發佈你的佈局xml也 – Raghunandan

+0

什麼是這個'查看主=(查看)findViewById(R.layout.activity_main)'你應該使用'findviewById'來查找你的意見的ID。建議您發佈您的xml佈局。還簡要說明你想要什麼。 – Raghunandan

回答

4

後,將裏面onCreate視圖初始化setContentView

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.mylayout); 
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.linearview); 
...// rest of the code 
+0

要澄清爲什麼你看到錯誤,Man Person,你正在接收'NullPointException'。你可以在你的堆棧跟蹤中看到一個原因。由...引發:java.lang.NullPointerException,後面跟着程序失敗的行和方法。 – jlindenbaum