How to open URL links within my android application - java

I am using two buttons to open two url links. When i press button it goes to browser. But I want to open these url links within my application. Can somebody help me to solve my issue. My code as below :
Activity Main :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.vkumar.myapplication.MainActivity"
android:background="#d3a7a7">
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="GOOGLE"
android:onClick="browser1"
android:id="#+id/browser1"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="#android:color/holo_blue_dark" />
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="MAIL"
android:id="#+id/browser2"
android:onClick="browser2"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="#android:color/holo_blue_dark" />
</LinearLayout>
Main Activity :
package com.example.vkumar.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button browser1 = (Button) findViewById(R.id.browser1);
Button browser2 = (Button) findViewById(R.id.browser2);
Button browser3 = (Button) findViewById(R.id.browser3);
Button browser4 = (Button) findViewById(R.id.browser4);
Button browser5 = (Button) findViewById(R.id.browser5);
}
public void browser1 (View view) {
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.google.co.in/"));
startActivity(intent);
}
public void browser2 (View v) {
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.gmail.com/"));
startActivity(intent);
}
}
Android manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.vkumar.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application android:allowBackup="true" android:icon="#mipmap/ic_launcher" android:label="MY APP" android:supportsRtl="true" android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

simple pass the url to webView on button click then show inside your application. see below example.
On Button link pass the url to webview activity
Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra("URL", "https://www.google.co.in/");
startActivity(intent);
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
String url = getIntent().getStringExtra("URL");
webView.loadUrl(url );
}
}

Related

My app keeps force closing when using 2 intents

Why do my android apps keep closing when using 2 intent? when I delete one of them, it's work. especially if I delete the "Reg" Button in the java files.
Thanks
XML Files:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="match_parent"
tools:context=".LoginAct">
<TextView
android:id="#+id/daftar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/daftar"
android:textAlignment="center"
android:textColor="#color/Linktext"
android:clickable="true"
android:focusable="true"
android:layout_marginTop="10dp">
</TextView>
<Button
android:id="#+id/pass"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:background="#color/colorPrimaryDark"
android:clickable="true"
android:focusable="true"
android:text="#string/lewat"
android:textColor="#color/textwhite" />
Java Files (If I deleted the "Reg" Button from this file, the app is working, but otherwise it will force close after the splash screen):
package com.soerja.ngalamhistory;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class LoginAct extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_login);
Button Pass = (Button) findViewById(R.id.pass);
Button Reg = (Button) findViewById(R.id.daftar);
Pass.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(LoginAct.this, MainActivity.class);
startActivity(intent);
}
});
Reg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(LoginAct.this, RegisAct.class);
startActivity(intent);
}
});
}
}
AndroidManifest Files:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.soerja.ngalamhistory">
<application
android:allowBackup="true"
android:icon="#mipmap/logo"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SplashAct"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginAct"
android:label="#string/applogin"
android:theme="#style/LogTheme"></activity>
<activity
android:name=".MainActivity"
android:label="#string/home"
android:theme="#style/AppTheme"></activity>
<activity android:name=".RegisAct"
android:label="#string/appdaftar"
android:theme="#style/LogTheme"></activity>
</application>
</manifest>
I would really appreciate your help because this is my school project :)
You are casting a TextView into a Button. Probably the problem is a invalid cast exception.
In XML, you define the "daftar" as a TextView, but in java you are trying to use it as a Button.
Change it
Button Reg = (Button) findViewById(R.id.daftar);
To it
TextView Reg = (TextView) findViewById(R.id.daftar);
Or change your XML implamentation, defining "daftar" as a Button
here you are using your text view id
android:id="#+id/daftar"
as a reference to declaring button
Button Reg = (Button) findViewById(R.id.daftar);
so i think ,that may be the reason for crash
try this method :
add this in your text xml code:
android:onClick="daftar"
add for java class use this:
public void daftar(View v)
{
Intent intent = new Intent(this, RegisAct.class);
startActivity(intent);
}
and remove the old java code you used for this text view:
i hope this helps.

All Buttons with Intent Don't work

Goal:
When I press one of the buttons, you should be referred into a next intent page.
Problem:
It only works for button named "Activity 1" but not the remaining buttons although you use the same source code for button "Activity 1", "Activity 2" and "Activity 3".
What part am I missing?
Thank you!
Info:
*I'm new in android
*I'm using API 23
androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jfdimarzio.application">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".firstactivity">
</activity>
<activity android:name=".activity2">
</activity>
</application>
</manifest>
MainActivity.java
package com.jfdimarzio.application;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import static android.R.attr.value;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
if(intent != null)
{
String valuee = intent.getStringExtra("key");
if(valuee != null)
{
final TextView textViewToChange = (TextView) findViewById(R.id.txtvw_activity1);
textViewToChange.setText(valuee);
}
}
}
public void onClick(View view)
{
switch(view.getId())
{
case R.id.btn_activity1:
Intent myIntent1 = new Intent(getBaseContext(), firstactivity.class );
myIntent1.putExtra("key", "Hello! Activity 1");
startActivity(myIntent1);
break;
case R.id.btn_activity2:
Intent myIntent2 = new Intent(getBaseContext(), Activity2.class );
myIntent2.putExtra("key", "Hello! Activity 2");
startActivity(myIntent2);
break;
case R.id.btn_activity3:
Intent myIntent3 = new Intent(getBaseContext(), Activity3.class );
myIntent3.putExtra("key", "Hello! Activity 3");
startActivity(myIntent3);
break;
}
}
}
firstactivity.java
package com.jfdimarzio.application;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class firstactivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String valuee = intent.getStringExtra("key");
final TextView textViewToChange = (TextView) findViewById(R.id.txtvw_activity1);
textViewToChange.setText(valuee);
}
}
Activity3.java
package com.jfdimarzio.application;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Activity3 extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String valuee = intent.getStringExtra("key");
final TextView textViewToChange = (TextView) findViewById(R.id.txtvw_activity1);
textViewToChange.setText(valuee);
}
}
Activity2.java
package com.jfdimarzio.application;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Activity2 extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String valuee = intent.getStringExtra("key");
final TextView textViewToChange = (TextView) findViewById(R.id.txtvw_activity1);
textViewToChange.setText(valuee);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
tools:context="com.jfdimarzio.application.MainActivity">
<TextView
android:id="#+id/txtvw_activity1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn_activity1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/txtvw_first"
android:layout_marginTop="67dp"
android:onClick="onClick"
android:text="Activity 1" />
<Button
android:id="#+id/btn_activity2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/btn_activity1"
android:layout_marginTop="15dp"
android:onClick="onClick"
android:text="Activity 2" />
<Button
android:id="#+id/btn_activity3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/btn_activity2"
android:layout_marginTop="19dp"
android:onClick="onClick"
android:text="Activity 3" />
</RelativeLayout>
firstactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txt_message"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtvw_activity1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="TextView" />
</RelativeLayout>
activity2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
Looks like you have not declared all the activities in your Manifest File. Make sure you include all the activities in your Manifest.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jfdimarzio.application">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".firstactivity">
</activity>
<activity android:name=".Activity2">
</activity>
<activity android:name=".Activity3">
</activity>
</application>
you just need to declare all the activites in the manifest file and name of the file in the
<activity android:name="...."></activity> tag must to similar to the name of your .java class file name.
Your entry of Activity2.java class is
<activity android:name=".activity2">
</activity>
It should be like this
<activity android:name=".Activity2">
</activity>

app crash when i click sign in buttom

here is my my signupactivity & main activity and their xml
public class SignupActivity extends AppCompatActivity {
protected EditText mUsername;
protected EditText mPassword;
protected EditText mEmail;
protected Button nbutton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
mUsername = (EditText) findViewById(R.id.usernamefield);
mPassword = (EditText) findViewById(R.id.passwordtextfield);
mEmail = (EditText) findViewById(R.id.emailtextfield);
nbutton = (Button) findViewById(R.id.signbutton);
nbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
// Retrieve the text entered from the EditText
String usernametxt = mUsername.getText().toString();
String password = mPassword.getText().toString();
String email= mEmail.getText().toString();
// Force user to fill up the form
if (usernametxt.equals("") && password.equals("")) {
Toast.makeText(getApplicationContext(),
"Please complete the sign up form",
Toast.LENGTH_LONG).show();
} else {
// Save new user data into Parse.com Data Storage
ParseUser newUser = new ParseUser();
newUser.setUsername(usernametxt);
newUser.setPassword(password);
newUser.setEmail(email);
newUser.signUpInBackground(new SignUpCallback() {
#Override
public void done(ParseException e) {
//SUCESS
if (e!= null){
AlertDialog.Builder builder=new AlertDialog.Builder(SignupActivity.this);
builder.setMessage(e.getMessage()).setTitle(R.string.signup_error_title).setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
else {
Intent intent= new Intent(SignupActivity.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
});
}
}
});
}
it was suppose to get the information abt users and go to main activity from signupactivity.the app crashes when it get in put from users and press sign in...
here is the xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.josephvarkey996gmail.test1.SignupActivity" >
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/usernamefield"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="89dp"
android:hint="#string/username_hint"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/passwordtextfield"
android:layout_centerHorizontal="true"
android:layout_below="#+id/usernamefield"
android:hint="#string/password_hint"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/emailtextfield"
android:hint="#string/email_hint"
android:layout_below="#+id/passwordtextfield"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/signup_hint"
android:id="#+id/signbutton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
here is the main activity code
package com.josephvarkey996gmail.test1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseUser;
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Parse.enableLocalDatastore(this);
Parse.initialize(this, "XFigOgliUYKi9h5RanLfLkuKU14AG2f2NFQXADKI", "sMQBPkhZV5b74MEGpP3PdQ6eePWo5Y9O8lRcQvBP");
ParseAnalytics.trackAppOpenedInBackground(getIntent());
ParseUser currentUser = ParseUser.getCurrentUser();
if(currentUser==null) {
navigatetologin();
}
if(currentUser!=null)
{
Log.i(TAG ,currentUser.getUsername());
}
// Enable Local Datastore.
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
private void navigatetologin() {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
mainactivity xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity" >
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
android manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.josephvarkey996gmail.test1" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SignupActivity"
android:label="#string/title_activity_signup" >
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
</activity>
</application>
</manifest>
We cannot help you until u post your logcat. As I don't have enough reputation to comment, I am writing this in answer.
Most common reason I saw people getting this error is:
They copy -paste this line from another class
R.id.usernamefield
So it will try to run the usernamefield of their other class which is not present here(note: eclipse will automatically import so they didn't get compilation errors). So to resolve your error you should delete the import and write proper R.id.xyz
But this is only shot in the dark until we see your logcat.

NullPointerException while creating activity

this is my codes LoginActivity.java file
package com.example.crims;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
public class LoginActivity extends Activity {
TextView screen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
screen = (TextView) findViewById(R.id.link_to_register);
// Listening to register new account link
screen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
}
}
this is my code for 'activity_login.xml' file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".LoginActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>`
And Finally this is my menifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.crims"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.crims.LoginActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterActivity"
android:label="Register New Account">
</activity>
</application>
</manifest>
All of these files are here and i want to find my error as I m new to android app development...
You have NullPointerException(NPE) here:
10-01 21:02:07.580: E/AndroidRuntime(1299):
at com.example.crims.LoginActivity.onCreate(LoginActivity.java:17)
So, check line 17 of LoginActivity.java (you can just double click on this message in the logcat view and you will be navigated to this line).
It seems, line 17 is this:
registerScreen.setOnClickListener(new View.OnClickListener() {
As this is NPE, registerScreen is null. So, you should inspect why it is null. This is because Activity can't find it in line above and it returns null instead:
TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
This can be one of two possibilities: either you do not have widget with id 'link_to_register' in activity_login.xml, or something other is wrong :)
Check this please and show your activity_login.xml file, please.
Listen brother take this code. Change it according to you.
public class MainActivity extends Activity {
TextView screen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen = (TextView) findViewById(R.id.screen);
// Listening to register new account link
screen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), Register.class);
startActivity(i);
}
});
}
}
You need to register your activity in manifest.xml file too in case if you don't know about it.

i am not able to open google maps within my application

I am creating an applicationwhich has a button which on click has to open google maps. But onclick its blank!! I have used intent, is there any other way to load google maps into my application? What is wrong with my code? what am i missing?
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:text="MAPS" />
</RelativeLayout>
MainaActivity.java
package com.stayconnected;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, MainActivity2.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MainActivity.java
package com.stayconnected;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
String url = "http://maps.google.com/maps?saddr=" + "9982878"+","+"76285774"+"&daddr="+"9992084"+","+"76286455";
Intent intent1 = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AndroiManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stayconnected"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.stayconnected.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.stayconnected.MainActivity2"
android:label="#string/app_name" >
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Do I have to download the google map key or something? Because i read on the internet that to load google maps on my application activity I have to download the key and only then will maps work! Is that why I am having this problem??
String url = "http://maps.google.com/maps?saddr=" + "9982878"+","+"76285774"+"&daddr="+"9992084"+","+"76286455";
Intent intent1 = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent1);
If you use the above your os will find the available app's to display the map. I tested the above in samsung galaxy s3. I had the option of choosing from default phone browser, chrome and Maps. All the apps were already installed on my phone.
It may be a browser or maps available in the phone. But this opens a new application from your activity.
If you want to display map in your application by customizing, then use google map api available for android.
http://www.youtube.com/watch?v=dSqLKIow7sY. You can follow the tutorial to get google map API Key. API key is required if your using Google maps API for displaying maps on your phone.
<uses-permission android:name="android.permission.INTERNET"/>//permission in manifest
After you get the key in your map.xml paste the key
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="350dp"
android:clickable="true"
android:apiKey="paste your key here" //paste key
/>
You can also use a webview. This is as good as opening the browser to display map.
public class MainActivity extends Activity {
WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv= (WebView) findViewById(R.id.wv);
wv.getSettings().setPluginsEnabled(true);
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
wv.loadUrl("http://maps.google.com/maps?saddr=" + "9982878"+","+"76285774"+"&daddr="+"9992084"+","+"76286455");
}
}
Xml Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/wv"></WebView>
</LinearLayout>

Categories