-1

I want to open my app store link of my app on image click. On mainactivity file, inside the OnCreate, I have added following

    ImageView Button = (ImageView)findViewById(R.id.imageView5);

    Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=APPNAME"));
            startActivity(intent);
        }
    });

And on the layout file, here is my image;

            <ImageView
                android:id="@+id/imageView5"
                android:layout_width="232dp"
                android:layout_height="105dp"
                android:layout_marginTop="10dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:visibility="visible"
                ads:srcCompat="@drawable/write009" />

Where I am doing mistake ?

UPDATE: Here is what I got from the logcat after debug with virtual device

java.lang.RuntimeException: Unable to start activity
ComponentInfo{antivirus.android.APPNAME/com.APPNAME.activities.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void 
android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a 
null object reference
Zoe
  • 27,060
  • 21
  • 118
  • 148
user198989
  • 4,574
  • 19
  • 66
  • 95

3 Answers3

2

Without the crash log its hard to guess the problem but I think that you should not use the addCategory(Intent.CATEGORY_BROWSABLE) in your intent.

ImageView button = (ImageView)findViewById(R.id.imageView5);

button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=APPNAME"));
        startActivity(intent);
    }
});
madlymad
  • 6,367
  • 6
  • 37
  • 68
1

Try using this

<ImageView
                android:id="@+id/imageView5"
                android:layout_width="232dp"
                android:layout_height="105dp"
                android:layout_marginTop="10dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:visibility="visible"
                android:src="@drawable/write009" />

change ads:srcCompat to android:src.

Sandeep Parish
  • 1,888
  • 2
  • 9
  • 20
1

According to LogCat, your variable Button is null - findViewById() couldn't find R.id.imageView5. Are you sure you edited the right layout file? Are you loading this layout file on the beggining of your Activity onCreate() using setContentView(R.layout.your_layout_name)?

Also, you could try changing the variable name to lowercase button, maybe it clashes with class Button (I doubt this, but strange stuff happens)

Markaos
  • 659
  • 3
  • 13