Reputation: 440
I'm pretty new on the Android development, and I'm starting my first application.
I faced my first problem on the development, and the problem is "when clicking a button, the URL doesn't open, instead of that the app crashes." .
This is MainActivity.java:
package com.rodentsmobile.app;
import android.app.*;
import android.os.*;
import android.content.*;
import android.media.*;
import android.net.*;
import android.security.*;
import android.system.*;
import android.view.*;
import android.util.*;
import java.lang.*;
import java.net.*;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void ViewAptoide(View v)
{
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
Firstly, it was public void ViewAptoide()
but I thought that's forgetting the View v
is the reason of the app crashing and not opening the URL.. But that wasn't the reason.
However, here is main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ImageView
android:layout_height="300px"
android:layout_width="700px"
android:src="@drawable/image_1"
android:layout_centerInParent="true"
android:id="@+id/mainImageView1"/>
<TextView
android:layout_height="wrap_content"
android:text="Rodents Mobile"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_below="@id/mainImageView1"
android:layout_centerInParent="true"
android:id="@+id/mainTextView1"/>
<Button
android:layout_height="wrap_content"
android:text="Aptoide"
android:layout_width="wrap_content"
android:layout_below="@id/mainTextView1"
android:layout_centerInParent="true"
android:onClick="viewAptoide"
android:translationY="15dp"/>
</RelativeLayout>
Thanks!
Upvotes: 0
Views: 109
Reputation: 714
In xml you have used
android:onClick="viewAptoide " .
And in activity you used
public void ViewAptoide (View v)
{
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
Replace it with
public void viewAptoide (View v)
{
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
Check xml on click name and method name should be same.
Upvotes: 3
Reputation: 233
The button's onClick attribute is set to
android:onClick="viewAptoide"
Change your method name to
public void viewAptoide
By convention, method names should start with a lowercase character in Java.
Also, since you are accessing the internet using ACTION_VIEW intent, make sure you have the following permission in your AndroidManifest.xml file
<uses-permission android:name="android.permission.INTERNET" />
Upvotes: 2
Reputation: 451
Your button calls the method
android:onClick="viewAptoide"
While your method name is
public void ViewAptoide
Please ensure the naming is consistent otherwise your method is not found
Upvotes: 3