J'ai un listview
.On cliquez sur l'élément particulier de listview
, j'ouvre une mise en page du bas qui a 3 boutons et sur un clic de l'un des 3 boutons je veux faire un appel, mais quand je clique sur l'un des bouton je reçois référence d'objet nul, même après la création d'objets de ces boutonRéférence de l'objet nul sur le bouton Cliquez sur
public class CallActivity extends ListActivity {
private Button sales,service,insurance;
private static final String[] items={"Dahisar Brach","Borivali West ASC","Nagpada ASC"
,"Borivali East ASC","Kandivali West Workshop","Kandivali West"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_call);
sales=findViewById(R.id.sales);
service=findViewById(R.id.service);
insurance=findViewById(R.id.insurance);
setListAdapter(new ArrayAdapter<String>(this,
R.layout.list_items,
items));
}
@Override
public void onListItemClick(ListView parent, View v, int position,
long id) {
final Dialog mBottomSheetDialog = new Dialog(CallActivity.this, R.style.MaterialDialogSheet);
mBottomSheetDialog.setContentView(R.layout.view); // your custom view.
mBottomSheetDialog.setCancelable(true);
mBottomSheetDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);
mBottomSheetDialog.show();
if(position==0){
sales.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri call = Uri.parse("tel:" + "02228921114");
Intent callNumber = new Intent(Intent.ACTION_DIAL, call);
callNumber = Intent.createChooser(callNumber, "Call Number");
startActivity(callNumber);
}
});
service.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri call = Uri.parse("tel:" + "101");
Intent callNumber = new Intent(Intent.ACTION_DIAL, call);
callNumber = Intent.createChooser(callNumber, "Call Number");
startActivity(callNumber);
}
});
insurance.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri call = Uri.parse("tel:" + "100");
Intent callNumber = new Intent(Intent.ACTION_DIAL, call);
callNumber = Intent.createChooser(callNumber, "Call Number");
startActivity(callNumber);
}
});
}
}
}
vue (fichier layout)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="To whom you want to call?"
android:gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Sales"
android:gravity="center"
android:id="@+id/sales"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Service"
android:gravity="center"
android:id="@+id/service"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Insurance"
android:id="@+id/insurance"
android:gravity="center"
/>
</LinearLayout>
Qu'est-ce que je fais mal, S'il vous plaît help.I même essayé de créer l'objet dans onListItemClick
metho d, mais je reçois la même erreur. Pour votre information, J'utilise la compilation « com.android.support:appcompat-v7:26.+ », donc je ne dois pas cataloguée mon bouton
référence Votre bouton est disponible dans 'onCreate'. C'est pourquoi c'est nul. Vois ma réponse. –