Я использую ImageView
для извлечения данных из TextView
и использую его для вызова ACTION_DIAL
.Всякий раз, когда приложение запускается, оно не может запустить номеронабиратель для отображения номера.Это показывает, что getPackageManger
является нулевым.Я добавил код с XML ниже.Если я удалю оператор else ниже, приложение вылетит.
Код:
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
public class PostDetailActivity extends AppCompatActivity {
TextView mTitleTv, mDetailTv, mCategoryTv, mCallTv;
ImageView mImageIv;
private static final String TAG = "PostDetailActivity";
String contact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_detail);
//Action Bar
ActionBar actionBar = getSupportActionBar();
// ActionBar title
actionBar.setTitle("Coupon Details");
// Set Back Button
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
// Initialise
mTitleTv = findViewById(R.id.titleTv);
mDetailTv = findViewById(R.id.descriptionTv);
mImageIv = findViewById(R.id.imageView);
mCategoryTv = findViewById(R.id.categoryTv);
mCallTv = findViewById(R.id.callTv);
// get data from intent
String image = getIntent().getStringExtra("image");
String title = getIntent().getStringExtra("title");
String desc = getIntent().getStringExtra("description");
String cate = getIntent().getStringExtra("category");
String call = getIntent().getStringExtra("contact");
contact = call;
// Set Data to views
mTitleTv.setText(title);
mDetailTv.setText(desc);
mCategoryTv.setText(cate);
mCallTv.setText(call);
Picasso.get().load(image).into(mImageIv);
}
// Handle onBack pressed to go back to the previous activity
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
public void dialNumber(View view) {
TextView textView = (TextView) findViewById(R.id.callTv);
// Use format with "tel:" and phone number to create phoneNumber.
String phoneNumber = String.format("",
textView.getText().toString().trim());
// String phoneNumber = "1234567890";
// Create the intent.
Intent dialIntent = new Intent(Intent.ACTION_DIAL);
// Set the data for the intent as the phone number.
dialIntent.setData(Uri.parse(phoneNumber));
// If package resolves to an app, send intent.
if (dialIntent.resolveActivity(getPackageManager()) != null) {
startActivity(dialIntent);
} else {
Log.e(TAG, "Can't resolve app for ACTION_DIAL Intent.");
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#fff"
app:cardCornerRadius="5dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="true"
app:contentPadding="10dp"
tools:context=".PostDetailActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="64dp"
android:layout_height="64dp"
android:background="@color/cardview_light_background" />
<TextView
android:id="@+id/titleTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/imageView"
android:layout_marginStart="81dp"
android:layout_marginLeft="73dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Title"
android:textColor="#000"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:id="@+id/descriptionTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/imageView"
android:layout_alignTop="@id/titleTv"
android:layout_marginStart="81dp"
android:layout_marginLeft="73dp"
android:layout_marginTop="35dp"
android:text="This will be the description of the post" />
<TextView
android:id="@+id/categoryTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/imageView"
android:layout_alignTop="@id/descriptionTv"
android:layout_marginStart="81dp"
android:layout_marginLeft="73dp"
android:layout_marginTop="35dp"
android:text="This will be the description of the post" />
<TextView
android:id="@+id/callTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/imageView"
android:layout_alignTop="@id/descriptionTv"
android:layout_marginStart="81dp"
android:layout_marginLeft="73dp"
android:layout_marginTop="35dp"
android:text="This will be the description of the post" />
<ImageView
android:id="@+id/imgCall"
android:layout_width="19dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="false"
android:layout_marginStart="83dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="255dp"
android:clickable="true"
android:onClick="dialNumber"
app:srcCompat="@drawable/ic_call_black" />
</RelativeLayout>
</android.support.v7.widget.CardView>