2013-08-01 6 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

무엇을 이것은'View main = (View) findViewById (R.layout.activity_main)'입니다. 뷰의 ID를 찾으려면 findviewById를 사용해야합니다. 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

왜 사람이 오류가 발생하는지 명확하게하기 위해, 당신은'NullPointException'을 받고 있습니다. 스택 추적에서 이유를 볼 수 있습니다.'Caused by : java.lang.NullPointerException' 다음에 프로그램이 실패한 행과 메소드가옵니다. – jlindenbaum

관련 문제