Java Android应用程序一开始就崩溃

Java Android应用程序一开始就崩溃,java,android,http,user-interface,Java,Android,Http,User Interface,我有一个应用程序,它应该根据网页从服务器获取一些字符串,每次我运行它时它都会崩溃。我甚至不知道如何调试它,我已经尝试了许多更改。自从我弄乱GUI后,它就开始崩溃,并添加了切换视图的功能,如果这提供了任何帮助的话 代码如下: public class MainActivity extends Activity implements TextToSpeech.OnInitListener, OnClickListener{ private WebView mWebview; Ed

我有一个应用程序,它应该根据网页从服务器获取一些字符串,每次我运行它时它都会崩溃。我甚至不知道如何调试它,我已经尝试了许多更改。自从我弄乱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));
}

}
public类MainActivity扩展活动实现TextToSpeech.OnInitListener、OnClickListener{
私有网络视图;
编辑文本地址栏;
字符串currentWebpage=”http://www.aljazeera.com/news/americas/2013/07/20137113200544375.html";
LinearLayout视图组=(LinearLayout)findViewById(R.id.linearview);
视图main=(视图)findViewById(R.layout.activity\u main);
视图读数=(视图)findViewById(R.layout.read_out);
@凌驾
创建时的公共void(Bundle savedInstanceState){
System.out.println(“显示活动”_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(当前网页);
mWebview=(WebView)findViewById(R.id.WebView);
mWebview.getSettings().setJavaScriptEnabled(true);//启用javascript
setWebViewClient(新的WebViewClient());
System.out.println(“加载网页…”);
mWebview.loadUrl(当前网页);
}
公共void扬声器(字符串文本){
TextToSpeech tts=新TextToSpeech(this,this);
System.out.println(“说”);
如果(tts.IsLanguage可用(Locale.ENGLISH)!=-1){
tts.speak(text,TextToSpeech.QUEUE\u FLUSH,null);
}
}
public int speakFull(字符串文本){
开关读取器();

String[]句=text.split(“\n | \.(?!\\d)|)(?将视图初始化移动到
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

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

要澄清您看到错误的原因,Man Person,您将收到一个
NullPointException
。您可以在堆栈跟踪中看到由以下原因引起的原因:
:java.lang.NullPointerException
,其后是程序失败的行和方法。要澄清您看到错误的原因,Man Person,您将收到一个
NullPointException
。您可以在堆栈跟踪中看到由以下原因引起的原因:
:java.lang.NullPointerException
,后面是程序失败的行和方法。为清楚起见,您还可以发布布局xml这是什么
视图main=(视图)findViewById(R.layout.activity\u main)
您应该使用
findviewById
查找视图的ID。建议您发布xml布局。同时简要说明您需要什么。为清晰起见,您还可以发布布局xml。这是什么
View main=(View)findviewById(R.layout.activity\u main)
您应该使用
findviewById
来查找视图的ID。建议您发布xml布局。同时简要说明您需要什么。