我需要能夠從一個活動進行調用。所以這是我的活動,顯示人的細節,我有一個通話按鈕。但是當我調用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>
您的解決方案的工作原理。謝謝! – Pheonix7
很高興知道它。不要忘記接受具有相同問題的其他用戶的答案 –