2012-05-03 38 views
0

我需要能夠從一個活動進行調用。所以這是我的活動,顯示人的細節,我有一個通話按鈕。但是當我調用setOnClickListener(...)時,我得到一個nullPointerError。爲什麼?任何幫助thnks。nulPointException在按鈕

package com.AndroidApp.pagine; 

import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.HashMap; 
import java.util.HashSet; 
import java.util.Set; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 

import android.app.ListActivity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.Button; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 

import com.AndroidApp.ApplicationController; 
import com.AndroidApp.R; 
import com.AndroidApp.XMLFunctions; 
import com.AndroidApp.Login.LoginActivity; 
import com.AndroidApp.domain.Anagrafica; 



public class DettagliPagina extends ListActivity { 

    public Anagrafica anagrafica; 
    public ArrayList<HashMap<String, String>> mylist; 
    private boolean paused, newIntentSelected = false; 
    private ProgressDialog progressDialog; 
    private SharedPreferences mPreferences; 
    public ApplicationController ac; 

    private Button bCall; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     ac = (ApplicationController)getApplication(); 

     mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE); 
     String nome = mPreferences.getString("nome", "Errore"); 
     String cognome = mPreferences.getString("cognome", "Errore"); 
     setTitle("Sessione di : " + nome + " " + cognome); 

     new BackgroundAsyncTask().execute(); 
    } 


    @Override 
    protected void onStart() { 
     super.onStart(); 


     bCall = (Button) findViewById(R.id.bCall); 

     // add PhoneStateListener 
     PhoneCallListener phoneListener = new PhoneCallListener(); 
     TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); 
     telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); 

     // add button listener 
     bCall.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View view) { 

        Intent callIntent = new Intent(Intent.ACTION_CALL); 
        callIntent.setData(Uri.parse("tel:3492395504")); 
        startActivity(callIntent); 

       } 
     }); 

    } 



    @Override 
    protected void onPause() { 
     super.onPause(); 
     paused = true; 
     ac.setOraAttuale(Calendar.getInstance()); 
     ac.oraAttuale.add(Calendar.MINUTE, 1); 
     ac.setOraScadenza(ac.getOraAttuale()); 
     Log.d("DettagliPAgina scadenza", ac.getOraScadenza().getTime().toString()); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     if (ac.isKill) { 
      finish(); 
     } 
     if (paused && !newIntentSelected){ 
      if (Calendar.getInstance().after(ac.getOraScadenza())){ 
       //torna al login con toast sessione scaduta 
       ac.setSessioneTerminata(true); 
       Toast.makeText(getApplicationContext(), "Sessione Scaduta", Toast.LENGTH_SHORT).show(); 
       Intent intent = new Intent(DettagliPagina.this, LoginActivity.class); 
       intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(intent); 
      } 
     } 
    } 


    public class BackgroundAsyncTask extends 
      AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> { 

     @Override 
     protected ArrayList<HashMap<String, String>> doInBackground(
       String... paths) { 
      mylist = new ArrayList<HashMap<String, String>>(); 

      String xml = XMLFunctions.getXML(); 
      Document doc = XMLFunctions.xmlFromString(xml); 
      int numResults = XMLFunctions.numResults(doc); 

      if ((numResults <= 0)) { 
       Log.i("doInBack", "nussun risultato"); 
       DettagliPagina.this.runOnUiThread(new Runnable() { 
        public void run() { 
         Toast.makeText(getApplicationContext(), "Nessun risultato trovato", Toast.LENGTH_LONG).show(); 
        } 
       }); 

       Intent intent = new Intent(DettagliPagina.this, FiltriAnagraficaPagina.class); 
       startActivity(intent); 
      } 

      Bundle extras = getIntent().getExtras(); 
      int id = 0; 
      System.out.print(id); 
      if (extras != null) { 
       id = extras.getInt("id"); 
      } 

      Log.i("Dettagli", "D1"); 
      Set<Anagrafica> anagrafici = new HashSet<Anagrafica>(); 
      NodeList nodes = doc.getElementsByTagName("anagrafica");  

      Element e = (Element) nodes.item(id); 
      anagrafica = new Anagrafica(); 
      anagrafica.setId(XMLFunctions.getValue(e, "idAnagrafica")); 
      anagrafica.setNome(XMLFunctions.getValue(e, "nome")); 
      anagrafica.setCognome(XMLFunctions.getValue(e, "cognome")); 
      anagrafica.setIndirizzo(XMLFunctions.getValue(e, "indirizzo")); 
      anagrafica.setDataDiNascita(XMLFunctions.getValue(e, "dataDiNascita")); 
      anagrafica.setEmail(XMLFunctions.getValue(e, "email")); 
      anagrafica.setTipologiaUtente(XMLFunctions.getValue(e, "tipologieUtente")); 
      anagrafica.setAziendaCollegata(XMLFunctions.getValue(e, "aziendaCollegata")); 
      anagrafica.setTelefono(XMLFunctions.getValue(e, "telefono")); 
      anagrafica.setCellulare(XMLFunctions.getValue(e, "cellulare")); 
      anagrafica.setInteressi(XMLFunctions.getValue(e, "interessi")); 
      anagrafica.setRiferimenti(XMLFunctions.getValue(e, "riferimenti")); 
      anagrafici.add(anagrafica); 

      HashMap<String, String> map = new HashMap<String, String>(); 
      map.put("idAnagrafica",anagrafica.getId()); 
      map.put("nomeCognome",anagrafica.getNome() + " " + anagrafica.getCognome()); 
      map.put("indirizzo", "Indirizzo: " + anagrafica.getIndirizzo()); 
      map.put("dataDiNascita", "Data Nascita: " + anagrafica.getDataDiNascita()); 
      map.put("email", "Email: " + anagrafica.getEmail()); 
      map.put("tipologieUtente", "Tipologie Utente: " + anagrafica.getTipologiaUtente()); 
      map.put("aziendaCollegata", "Azienda Collegata: " + anagrafica.getAziendaCollegata()); 
      map.put("telefono", "Telefono: " + anagrafica.getTelefono()); 
      map.put("cellulare", "Cellulare: " + anagrafica.getCellulare()); 
      map.put("interessi", "Interessi: " + anagrafica.getInteressi()); 
      map.put("riferimenti", "Riferimenti" + anagrafica.getRiferimenti()); 

      mylist.add(map); 
      Log.i("Dettagli", "D2"); 
      return mylist; 
     } 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      progressDialog = new ProgressDialog(DettagliPagina.this); 
      progressDialog.setCancelable(true); 
      progressDialog.setMessage("Loading..."); 
      progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
      progressDialog.setProgress(0); 
      progressDialog.show(); 
     } 

     @Override 
     protected void onProgressUpdate(Integer... values) { 
      super.onProgressUpdate(values); 
     } 

     protected void onPostExecute(ArrayList<HashMap<String, String>> result) { 

      final ListAdapter adapter = new SimpleAdapter(DettagliPagina.this, mylist, R.layout.dettagli, 
        new String[] { "nomeCognome", "dataDiNascita", 
          "tipologieUtente", "aziendaCollegata", "email", 
          "telefono", "cellulare", "interessi", "indirizzo", 
          "riferimenti" }, 
        new int[] { R.id.tvNomeCognome, R.id.tvDataDiNascita, 
          R.id.tvTipologiaUtente, R.id.tvAziendaCollegata, 
          R.id.tvEmail, R.id.tvTelefono, R.id.tvCellulare, 
          R.id.tvInteressi, R.id.tvIndirizzo, R.id.tvRiferimenti }); 

      setListAdapter(adapter); 

      final ListView lv = getListView(); 
      lv.setTextFilterEnabled(true); 
      lv.setOnItemClickListener(new OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        @SuppressWarnings("unchecked") 
        HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position); 
        Toast.makeText(getApplicationContext(), "ID '" + o.get("idAnagrafica") + "' was clicked.", 
          Toast.LENGTH_LONG).show(); 

       } 
      }); 

      progressDialog.dismiss(); 
     } 
    } 


    public class PhoneCallListener extends PhoneStateListener { 

     private boolean isPhoneCalling = false; 

     String LOG_TAG = "LOGGING 123"; 

     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 

      if (TelephonyManager.CALL_STATE_RINGING == state) { 
       // phone ringing 
       Log.i(LOG_TAG, "RINGING, number: " + incomingNumber); 
      } 

      if (TelephonyManager.CALL_STATE_OFFHOOK == state) { 
       // active 
       Log.i(LOG_TAG, "OFFHOOK"); 

       isPhoneCalling = true; 
      } 

      if (TelephonyManager.CALL_STATE_IDLE == state) { 
       // run when class initial and phone call ended, need detect flag 
       // from CALL_STATE_OFFHOOK 
       Log.i(LOG_TAG, "IDLE"); 

       if (isPhoneCalling) { 

        Log.i(LOG_TAG, "restart app"); 

        // restart app 
        Intent i = getBaseContext().getPackageManager() 
          .getLaunchIntentForPackage(
            getBaseContext().getPackageName()); 
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(i); 

        isPhoneCalling = false; 
       } 

      } 
     } 
    } 
} 

這裏的細節XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <TextView 
       android:id="@+id/tvNomeCognome" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:drawableLeft="@drawable/icon" 
       android:drawablePadding="10dp" 
       android:padding="7dp" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textSize="24dp" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" 
      android:padding="4dp" > 

      <TextView 
       android:id="@+id/tvTipologiaUtente" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:padding="2dp" 
       android:textSize="20dp" /> 

      <TextView 
       android:id="@+id/tvDataDiNascita" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:padding="2dp" 
       android:textSize="18dp" /> 

      <TextView 
       android:id="@+id/tvEmail" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:autoLink="email" 
       android:linksClickable="true" 
       android:padding="2dp" 
       android:textSize="18dp" /> 

      <TextView 
       android:id="@+id/tvTelefono" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:autoLink="phone" 
       android:linksClickable="true" 
       android:padding="2dp" 
       android:textSize="18dp" /> 

      <TextView 
       android:id="@+id/tvCellulare" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:autoLink="phone" 
       android:linksClickable="true" 
       android:padding="2dp" 
       android:textSize="18dp" /> 

      <TextView 
       android:id="@+id/tvInteressi" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:padding="2dp" 
       android:textSize="18dp" /> 

      <TextView 
       android:id="@+id/tvAziendaCollegata" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:padding="2dp" 
       android:textSize="18dp" /> 

      <TextView 
       android:id="@+id/tvIndirizzo" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:padding="2dp" 
       android:textSize="18dp" /> 

      <TextView 
       android:id="@+id/tvRiferimenti" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:padding="2dp" 
       android:textSize="18dp" /> 

      <Button 
       android:id="@+id/bCall" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:text="@string/chiama" /> 
     </LinearLayout> 
    </LinearLayout> 

</ScrollView> 

回答

0

該按鈕可能不是ListView的一部分,只是該Activity的主佈局。

如果我明白你在說什麼,你有一個包含ListView和Button的主視圖。如果這肯定是正確的,那麼你需要使用一個活動,而不是ListActivity或使用:

mActivity= this ; 
Button button = new Button(this); 
button.setText("push me"); 
button.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
button.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
      Toast.makeText(mActivity, "push", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
getListView().addFooterView(button); 

務必之前setAdapter(ListAdapter adapter)

+0

您的解決方案的工作原理。謝謝! – Pheonix7

+0

很高興知道它。不要忘記接受具有相同問題的其他用戶的答案 –

1

好像你忘了打電話給setContentView(R.layout.your_layout);的onCreate方法

+0

沒有它被稱爲listAdapter - 決賽ListAdapter adapter = new SimpleAdapter(DettagliPagina.this,mylist,R.layout.dettagli,....)。我也試過用setContentView(R.layout.details),但它仍然給我同樣的錯誤。 – Pheonix7

+0

把你的完整代碼與** setContentView ** thingy – waqaslam

0

改用OnClickListener,請儘量使用onItemClikListener的。

你在ListActivity中,所以我想(也許我錯了),你想要的是打電話與選定的行。因此,除去BCALL及其onClickListener並嘗試

getListView().setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      Intent callIntent = new Intent(Intent.ACTION_CALL); 
      callIntent.setData(Uri.parse("tel:3492395504")); 
      startActivity(callIntent); 

     } 
    }); 
+0

這是爲了聽整個行上的點擊。該按鈕可能不是ListView的一部分,只是該Activity的主佈局。無論如何,我們需要更多的信息.​​.. –

0

您購買的NPE,因爲你正在試圖做到這一點:

bCall = (Button) findViewById(R.id.bCall); 

如果您還沒有設置屏幕的佈局。

該按鈕位於列表行佈局中,因此您需要在列表適配器中設置它的功能。

+0

我做到了。它在代碼中的地位。最終的ListAdapter適配器=新的SimpleAdapter(DettagliPagina.this,mylist,R.layout.dettagli,....) – Pheonix7

0

調用addFooterView(View v)如果我理解你的代碼,你蹬掉的AsyncTask in onCreate,它在其onPostExecute方法內設置listAdaper。此外,在onStart中,您試圖獲得按鈕bCall

但是,如果onStart執行的後臺任務完成之前,那麼你就還沒有設置適配器,這樣你的按鈕將不存在,您的電話(Button) findViewById(R.id.bCall)將返回null

+0

是的,可能是這種情況。我如何解決這樣的問題?即使我將按鈕直接放在新的BackgroundAsyncTask()。execute();打電話我想我仍然會得到同樣的錯誤。 – Pheonix7

+0

您可以將按鈕代碼從'onStart'移動到'onPostExecute',以便在設置適配器後運行。 –

+0

不幸的是我不能把它放在onPostExecute因爲只有UI線程可以使用/更新/創建視圖,如按鈕。 – Pheonix7