用于ImageView的OnClicklistener的NullPointerException

时间:2018-04-18 00:41:24

标签: android nullpointerexception imageview onclicklistener

每次我运行App时都会遇到此错误

within(com.abc..controllers..*) ||
within(com.abc..services..*) ||
within(com.abc..daos..*)

这是我的代码,错误与addToCartLogo的OnClicklistener有关..一旦我发表评论,应用运行正常,似乎无法为它设置Onclicklistener !!甚至是cartLogo其他代码的精确副本

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.android.etihadflight.MainActivity.onCreate(MainActivity.java:29)

这是XML

public class MainActivity extends AppCompatActivity implements
        Tab1.OnFragmentInteractionListener, Tab2.OnFragmentInteractionListener, Tab3.OnFragmentInteractionListener {

    private static int cart_count = 0;
    ImageView cartLogo;
    ImageView addToCartLogo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addToCartLogo = (ImageView) findViewById(R.id.add_to_cart);
        addToCartLogo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        cartLogo = (ImageView) findViewById(R.id.cart_icon);
        cartLogo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_local_drink));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_food));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_euro));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}

那是另一个XML文件引用的是cartLogo

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:orientation="horizontal"
    android:padding="5dp">

    <LinearLayout
        android:id="@+id/item_food_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:padding="5dp">

        <ImageView
            android:id="@+id/img_food"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:src="@mipmap/ic_launcher_round" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/name_food"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Food Name"
                android:textColor="#ffffff"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/food_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Food Description Here"
                android:textColor="#ffffff" />

        </LinearLayout>




     </LinearLayout>

        <LinearLayout
            android:id="@+id/linear_layout_cartlogo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="end">

            <ImageView
                android:layout_margin="8dp"
                android:id="@+id/add_to_cart"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:src="@drawable/add_into_cart" />

        </LinearLayout>
    </LinearLayout>

0 个答案:

没有答案