2014-07-22 118 views
0

這與之前使用textView涉及應用程序崩潰(已解決)的另一個問題類似,但現在我遇到了editText問題,因爲每當我嘗試檢索editText.getvalue(),即使它在線程之外,它也會崩潰應用程序。檢索線程中的editText時,應用程序崩潰Android Studio

下面是代碼:

package awsomisoft.yemeb; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 

import awsomisoft.yemeb.R; 

public class List extends Activity { 
private Handler handler; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_list); 
    final TextView urlTextOut = (TextView) findViewById(R.id.URLtextView); 
    final EditText sear = (EditText) findViewById(R.id.editText); 
    new Thread() { 
     StringBuilder text = new StringBuilder(); 
     @Override 
     public void run() { 
      try 

      { 
       String str = ""; 
       URL url = new URL("http://mydomainname.com/"+sear.getText().toString()+"/test.meb"); 
       BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 

       while ((str = in.readLine()) != null) { 
        text.append(str); 
       } 
       in.close(); 
      } catch (MalformedURLException e1) 

      { 
      } catch (IOException e) 

      { 
      } 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        urlTextOut.setText(text); 
       } 
      }); 
     } 
    }.start(); 

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.list, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

編輯:這裏是activity_list.xml -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="awsomisoft.yemeb.List" 
android:id="@+id/List"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/URLtextView" 
    android:textSize="18dp" /> 

</RelativeLayout> 

我想,因爲有一個解決方案,我發現這裏使用EDITTEXT在使用前處理程序線程,但它並沒有解決問題。我假設有問題的解決方案,但我似乎無法找到它。我只是需要一個指南,看看我需要做的editText,以確保應用程序不會崩潰。感謝您的任何幫助。如果有簡單的解決方案,我很抱歉。

+0

請發表您的activity_list.xml代碼 – bwegs

+0

你想其他的xml文件? editText位於其他活動中。 – user3864563

+0

如果您試圖引用的editText位於另一個xml文件中,那麼這就解釋了您的應用崩潰的原因。 – bwegs

回答

1

通常情況下,您可以通過聲明變量final來允許匿名類訪問外部類變量。然而在你的情況下,因爲這是處理編輯文本,你希望這個變量能夠改變,所以final是不可接受的。

在這種情況下,最好的辦法是使線程成爲普通類而不是匿名類,然後在構造函數中傳遞變量(它必須是普通類,因爲不能有顯式聲明的構造函數在一個匿名類中)。

public class MyThread implements Runnable { 

    StringBuilder text = new StringBuilder(); 
    String editTextContents; 

    // Constructor 
    public MyThread(String editTextContents) {  // <-- Pass in the string from EditText here 
     this.editTextContents = editTextContents; 
    } 

    @Override 
    public void run() { 
     try 

     { 
      String str = ""; 
      URL url = new URL("http://mydomainname.com/"+ editTextContents +"/test.meb"); 
      BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 

      while ((str = in.readLine()) != null) { 
       text.append(str); 
      } 
      in.close(); 
     } catch (MalformedURLException e1) 

     { 
     } catch (IOException e) 

     { 
     } 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       urlTextOut.setText(text); 
      } 
     } 
    } 
} 

你可以調用線程的活動:

Runnable myRunnable = new MyThread(editTextContents); 
new Thread(myRunnable).start; 
+0

我會把它放在主要或列表活動中嗎?或者我會創建一個全新的類文件? – user3864563

+0

當我將它作爲一個單獨的類實現時,runOnUiThread無法識別。我將如何解決這個問題? – user3864563

+0

我知道它使用Intents並傳遞變量。謝謝您的幫助。 :)編輯:人們尋找我遇到的問題的解決方案,基本上當你通過intents改變活動時,只需添加intent.putExtra(key,string);在移動到下一個活動時創建Intent之後。 – user3864563

0

如果你想訪問的EditText上是不是在佈局,您使用

setContentView(R.layout.activity_list); 

然後它會設置擊發阻鐵一種觀點認爲它無法找到或(設置空):

final EditText sear = (EditText) findViewById(R.id.editText); // null 
sear.getText().toString() // will now crash your app because 
          // you're trying to access a null object 

換句話說R.id.editText需要在佈局activity_list.xml或者你需要使用它已經在當你其他的XML文件的setContentView(* .XML)

+0

如果我聲明瞭setContentView(R.layout.activity_list),它會工作嗎?從editView獲取字符串後? – user3864563

+0

不,因爲那時你的活動無處尋找你所要求的editText視圖。 – bwegs

+0

它會工作,而不是我使用Intent將包含主活動的editText值的字符串傳遞給其他活動? – user3864563