2015-04-30 181 views
0

我想知道如何更改文本視圖的背景,因爲我從網站parse.com提取數據,但我無法做到,我正在嘗試這是做的。更改TextView的背景顏色

CODE

public class Pizzarias extends ActionBarActivity { 

// Declare Variables 
Boolean bColorStatus = true; 
TextView status; 
ListView listview; 
List<ParseObject> ob; 
ProgressDialog mProgressDialog; 
ListViewAdapterPizzarias adapter; 
private List<WorldPopulation> worldpopulationlist = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_pizzarias); 


    new RemoteDataTask().execute(); 
    //getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    status = (TextView) findViewById(R.id.status); 
    // status.setBackgroundColor(Color.BLUE); 


} 

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

    //Os metodos abaixo são para mostrar o icone do aplicativo na action bar 
    getSupportActionBar().setDisplayShowHomeEnabled(true); 
    getSupportActionBar().setLogo(R.drawable.ic_launcher); 
    getSupportActionBar().setDisplayUseLogoEnabled(true); 

    return true; 
} 


/*@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    onBackPressed(); 
    return true; 
}*/ 


//RemoteDataTask AsyncTask 
private class RemoteDataTask extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Create a progressdialog 
     mProgressDialog = new ProgressDialog(Pizzarias.this); 
     // Set progressdialog title 
     mProgressDialog.setTitle("Carregando Pizzarias"); 
     // Set progressdialog message 
     mProgressDialog.setMessage("Aguarde..."); 
     mProgressDialog.setIndeterminate(false); 
     // Show progressdialog 
     mProgressDialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // Create the array 
     worldpopulationlist = new ArrayList<WorldPopulation>(); 
     try { 
      // Locate the class table named "Country" in Parse.com 
      ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
        "GerenciarPizzariasPatos"); 
      // Locate the column named "ranknum" in Parse.com and order list 
      // by ascending, 


//here is the implementation to search for the word "closed" and change the background color. 
      query.whereStartsWith ("status" , "fechado"); 
      ob = query.find(); 
      if(query.equals("fechado")){ 
       status.setTextColor(getResources().getColor(R.color.red)); 
       bColorStatus = false; 
      } 
      else { 
       status.setTextColor(getResources().getColor(R.color.green)); 
       bColorStatus = true; 
      } 

      query.orderByAscending("nome"); 
      query.equals("status"); 
      ob = query.find(); 
      for (ParseObject country : ob) { 
       WorldPopulation map = new WorldPopulation(); 
       map.setNome((String) country.get("nome")); 
       map.setEndereco((String) country.get("endereco")); 
       map.setTelefone((String) country.get("telefone")); 
       map.setStatus((String) country.get("status")); 
       worldpopulationlist.add(map); 
      } 
     } catch (ParseException e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // Locate the listview in listview_main.xml 
     listview = (ListView) findViewById(R.id.listviewpizzarias); 
     // Pass the results into ListViewAdapter.java 
     adapter = new ListViewAdapterPizzarias(Pizzarias.this, 
       worldpopulationlist); 
     // Binds the Adapter to the ListView 
     listview.setAdapter(adapter); 
     // Close the progressdialog 
     mProgressDialog.dismiss(); 
    } 
} 
} 

ERRO

04-30 11:56:59.156 28388-28691/com.example.ronysueliton.patospizzas E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2 Process: com.example.ronysueliton.patospizzas, PID: 28388 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:300) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTextColor(int)' on a null object reference at com.example.ronysueliton.patospizzas.Pizzarias$RemoteDataTask.doInBackground(Pizzarias.java:111) at com.example.ronysueliton.patospizzas.Pizzarias$RemoteDataTask.doInBackground(Pizzarias.java:78) at android.os.AsyncTask$2.call(AsyncTask.java:288) at java.util.concurrent.FutureTask.run(FutureTask.java:237)             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)             at java.lang.Thread.run(Thread.java:818) 04-30 11:56:59.590 28388-28388/com.example.ronysueliton.patospizzas E/WindowManager﹕ android.view.WindowLeaked: Activity com.example.ronysueliton.patospizzas.Pizzarias has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{3dc64751 V.E..... R......D 0,0-513,248} that was originally added here at android.view.ViewRootImpl.(ViewRootImpl.java:375) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) at android.app.Dialog.show(Dialog.java:298) at com.example.ronysueliton.patospizzas.Pizzarias$RemoteDataTask.onPreExecute(Pizzarias.java:90) at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587) at android.os.AsyncTask.execute(AsyncTask.java:535) at com.example.ronysueliton.patospizzas.Pizzarias.onCreate(Pizzarias.java:47) at android.app.Activity.performCreate(Activity.java:5953) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388) at android.app.ActivityThread.access$800(ActivityThread.java:148) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5312) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 04-30 12:01:59.260 28388-28691/com.example.ronysueliton.patospizzas I/Process﹕ Sending signal. PID: 28388 SIG: 9

誰能幫助?

+1

這應該工作'yourTextView.setBackgroundColor(Color.RED);',但請提供更多信息。你有沒有看到任何異常或錯誤? –

+0

@BatuhanCoşkun。我試圖展示最好的方式。 –

+0

您無法訪問'doInBackground(...)'方法內的UI元素,因爲該方法未在與UI相同的線程上運行。你有使用AsyncTask的具體原因嗎?既然你正在使用Parse,你可以使用'findInBackground()'方法。 https://www.parse.com/docs/android/api/com/parse/ParseQuery.html#findInBackground%28%29 –

回答

0

看起來像你正在嘗試在後臺線程上的UI元素。您的變量status將在doInBackground()中爲空。設置這些顏色需要在onPostExecute()或致電runOnUiThread(Runnable action)完成。

+0

我做了你所說的並且消失了一部分錯誤,現在出現這個錯誤:'java.lang.NullPointerException:嘗試調用虛擬方法'void android.widget.TextView.setTextColor(int)'在一個空對象引用上' –

+0

聽起來像'status'是空的。可能要檢查是否status =(TextView)findViewById(R.id.status);'實際上是返回任何東西。 – dmarsi

+0

然後,我將不得不改變textview的價值? –