簡體   English   中英

Android應用程序在啟動時崩潰

[英]Android App crashes right on start

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

這是代碼:

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

setContentView之后將您的視圖初始化移動到onCreate

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM