Button Click上的空对象引用

时间:2017-09-10 06:43:43

标签: android button nullpointerexception onclick

我有一个listview。点击listview的特定项目,我从底部打开一个布局,其中有3个按钮,点击我要拨打电话的3个按钮中的任意一个,但是当我点击任何一个按钮时,即使创建了那些按钮的对象

,我也会得到空对象引用
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);
                }
            });
        }


    }
}

查看(布局文件)

<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>

我做错了什么,请帮忙。我甚至尝试用onListItemClick方法创建对象,但我得到了同样的错误。 仅供参考, 我正在使用compile&#39; com.android.support:appcompat-v7:26。+&#39;,所以我不需要对我的按钮进行类型转换

2 个答案:

答案 0 :(得分:1)

按钮sales,servies等不属于activity_call布局,它们是对话框布局的一部分,因此您需要在对话框引用上应用findViewById

这样做

final Dialog mBottomSheetDialog = new Dialog(CallActivity.this, R.style.MaterialDialogSheet);
mBottomSheetDialog.setContentView(R.layout.view); // your custom view.

sales     =  mBottomSheetDialog.findViewById(R.id.sales);
service   =  mBottomSheetDialog.findViewById(R.id.service);
insurance  = mBottomSheetDialog.findViewById(R.id.insurance);

mBottomSheetDialog.setCancelable(true);
mBottomSheetDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);
mBottomSheetDialog.show();

而不是

    sales=findViewById(R.id.sales);
    service=findViewById(R.id.service);
    insurance=findViewById(R.id.insurance);

答案 1 :(得分:0)

您正在使用const data = { "profile": [ { "proid": { "user": "kohinoor" }, "image": "/media/profiles/DSCN3253.JPG", "shoptype": "Clothes", "city": "Ahemednagar" }, { "proid": { "user": "shrawani" }, "image": "/media/profiles/PicsArt_08-17-09.24.48_qA94ILU.jpg", "shoptype": "Cat Shop", "city": "Kopargaon" }, { "proid": { "user": "rajpal" }, "image": "/media/profiles/PicsArt_08-17-09.24.48_T3birjf.jpg", "shoptype": "Clothings", "city": "Sangamner" }, ], "post": [ { "id": 120, "content": "Old Mi Stocks or Sell !!!", "url": null, "shareproductdealer": "kohinoor", "shareproducttype": "Xiaomi Mi", "shareproductid": "68", "username": "kohinoor", "updated": "2017-09-08T10:49:11Z", "timestamp": "2017-09-08T10:49:11Z" }, { "id": 119, "content": "Hello... Miahe...", "url": null, "shareproductdealer": null, "shareproducttype": null, "shareproductid": null, "username": "shrawani", "updated": "2017-09-08T10:38:14Z", "timestamp": "2017-09-08T10:38:14Z" }, { "id": 115, "content": "hello jockey", "url": null, "shareproductdealer": "rajpal", "shareproducttype": "jockey", "shareproductid": "65", "username": "rajpal", "updated": "2017-08-16T11:22:32Z", "timestamp": "2017-08-16T11:22:32Z" } ] }; const profiles = data.profile; const posts = data.post; const profilePosts = profiles.map(item => ({ profile: item, post: posts.filter(post => post.username === item.proid.user) })); console.log(profilePosts);的自定义视图。因此,Dialog中未提供Button引用。所以你必须做这样的事情:

onCreate()