I just started to develop my first application in android.
Here i used a image button.
But when i try to run my app in device it is not launching a an errors are showing in the log cat.
can any one please help me in how to resolve this error.Thanks in advance.Hoping for your help.
This is the code i used:
ArraysActivity.java
package com.me.array;
//import com.sweans.pus.SingleListItem;
//import com.sweans.pus.SecondScreen;
//import com.me.array.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView;
public class ArraysActivity extends Activity {
ListView listView;
private ImageButton mybtn;
ArrayAdapter<String> adapter;
String[] sujith =new String[]{ "The-Birth", "Menu", "Album",
"Events", "Blog", "Press", "Reservation", "MONOMANIA",
"Contact" };
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mybtn = (ImageButton)findViewById(R.id.imgbtn);
mybtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent nextScreen = new Intent(getApplicationContext(), ZActivity.class);
startActivity(nextScreen);
}
});
listView = (ListView) findViewById(R.id.mylist);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, sujith);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// Intent nextScreen = new Intent(getApplicationContext(), SingleListItem.class);
// startActivity(new Intent(action));
if(position == 0){
Intent i = new Intent(ArraysActivity.this, SingleListItem.class);
// passing variable
i.putExtra("my.package.dataToPass","new");
//i.putExtra( "int",position);
startActivity(i);
}
else if(position == 1){
Intent i = new Intent(ArraysActivity.this, ListSample.class);
startActivity(i);
}
else if(position == 2){
Intent i = new Intent(ArraysActivity.this, MyGridView.class);
startActivity(i);
}
else if(position == 4){
Intent i = new Intent(ArraysActivity.this, MessageList.class);
startActivity(i);
}
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- <ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="278dp"
android:scaleType="fitXY"
android:src="#drawable/home_page" /> -->
<ImageButton
android:id="#+id/imgbtn"
android:layout_width="fill_parent"
android:layout_height="278dp"
android:scaleType="fitXY"
android:src="#drawable/home_page"
android:contentDescription="#string/desc"/>
<ListView
android:id="#+id/mylist"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.me.array"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/icon_andro"
android:label="#string/app_name" >
<activity
android:name=".ArraysActivity"
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=".SingleListItem"></activity>
<activity android:name=".menu"></activity>
<activity android:name=".ListSample"></activity>
<activity android:name=".SeperatedListAdapter"></activity>
<activity android:name=".MyGridView"></activity>
<activity android:name=".Message"></activity>
<activity android:name=".MessageList"></activity>
<activity android:name=".BaseFeedParser"></activity>
<activity android:name=".SeparatedListAdapter"></activity>
<activity android:name=".RssHandler"></activity>
<activity android:name=".ZActivity"></activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
The log cat is showing a NPE in line number 36,ie mybtn.setOnClickListener(new OnClickListener(){.
my log cat is
Instead of the anonymous onClikListener, try it like this:
mybtn.setOnClickListener(this);
Then, in the end of your source, implement the listener,
public void onClick(View v)
{
//do the stuff you want here
}
I would replace getApplicationContext() with "this" btw, of course in this version only.
It should work this way, I think.
OR, you might want to try it like this:
Context ctx; //define it as class variable
In the onCreate:
ctx=this;
Replace your getApplicationContext() with ctx, I think that is the thing which gives you the NullPointer.
Related
This is my code:
public String a_number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_make_the_call);
//
callButton = (ImageButton)findViewById(R.id.call_button);
aCall = (TextView)findViewById(R.id.number_a);
a_number = aCall.getText().toString();
}
public void makeCallFunction(View view) {
String temp = "";
temp = "tel:"+a_number;
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(temp));
startActivity(callIntent);
}
My XML file contains:
<ImageButton
android:id="#+id/call_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:src="#drawable/dial"
android:onClick="makeCallFunction"/>
I have added the following in my manifest file:
<uses-permission android:name="android.permission.CALL_PHONE"/>
I searched my best for answers but found nothing helpful..
EDIT:
When I give a value directly, the call gets placed now.
Eg:
callIntent.setData(Uri.parse("tel:9876543210"));
But the problem remains when I read the number from my text view and try to place call with that number.
Can someone help me out?
Thanks in advance
If there's some alternate way to implement call, that'd help too.
Disable SIP Internet Calling and VoIP on your emulator or debugging device. This is the steps for Samsung Galaxy S4 :
go to settings > call > disable internet calling(in bottom)
I don't know what you are doing wrong, but see for yourself a working example :
MakeTheCallActivity.java
package com.example.makethecall;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MakeTheCallActivity extends ActionBarActivity {
private EditText mEditText;
private Button mButton;
private final String TAG = "MakeTheCallActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_make_the_call);
mButton = (Button)findViewById(R.id.call_button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mEditText = (EditText)findViewById(R.id.phone_number);
String phoneNumber = mEditText.getText().toString();
Log.d(TAG, phoneNumber);
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
startActivity(intent);
}
});
}
}
activity_make_the_call.xml
<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=".MakeTheCallActivity"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/phone_number" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="call"
android:id="#+id/call_button" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sauravsamamt.makethecall" >
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MakeTheCallActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You can see I'm not using a hardcoded number. Make sure you enter a valid number. That's it.
I am trying to open a URL in a browser using an intent.
My code is
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.onair99.com"));
startActivity(i);
This will result in an error on any browser I have tried since the browser tries to open something like this:
http://http//onair99.com//
As far as I have tried, I only have encountered the problem with this specific URL.
Does anybody know why?
Thanks.
do as mentioned in this post
String url = "http://www.somewebsite.com";
In production level code, you may like to check if the url begins with http or https... Would be better to check
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Here's the documentation of Intent.ACTION_VIEW.
Try This Code
main.xml
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Go to link" />
</LinearLayout>
MyAndroidAppActivity.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAndroidAppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.onair99.com"));
startActivity(browserIntent);
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mkyong.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".MyAndroidAppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I'm trying to build my FIRST app, it just. Launch -> splashscreen -> display web page automatically (fullscreen / no button trigger). But, after splashscreen, it just blank. I guess something wrong on Intent part in MainActivity.java, and intent-filter in AndroidManifest.xml. But I don't know to fix this yet.
This is my code:
MainActivity.java
package com.myapp.splashscreen;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(context, WebViewActivity.class);
startActivity(intent);
}
}
SplashScreen.java
package com.myapp.splashscreen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
private static final int SPLASH_TIME = 3 * 1000;// 3 seconds
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashScreen.this,
MainActivity.class);
startActivity(intent);
SplashScreen.this.finish();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}, SPLASH_TIME);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
}
}, SPLASH_TIME);
}
#Override
public void onBackPressed() {
this.finish();
super.onBackPressed();
}
}
WebViewActivity.java
package com.myapp.splashscreen;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.webView1);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#d3d3d3"
android:orientation="vertical">
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
splashscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/splash"
android:configChanges="orientation|keyboardHidden"
android:orientation="vertical"
android:screenOrientation="portrait">
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp.splashscreen"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity
android:name=".SplashScreen"
android:theme="#android:style/Theme.Black.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
<activity
android:name=".WebViewActivity"
android:theme="#android:style/Theme.NoTitleBar" />
</application>
</manifest>
If you want you the order to be SplashActivity -> WebViewActivity
Change in SplashActivity
Intent intent = new Intent(SplashScreen.this,
MainActivity.class);
to
Intent intent = new Intent(SplashScreen.this,
WebViewActivity.class);
This will tell you application that it should start the WebViewActivity instead of the MainActivity
You haven't declared the WebViewActivity in your AndroidManifest.xml
I'm just trying to come up with a skeleton for an activity that uses a ListView as a news feed for a game my class is making.
In this activity there's simply the news feed, and a Refresh button (to update the ListView with the latest news)
My activity_news.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
<ListView
android:id="#+id/news"
android:layout_width="match_parent"
android:layout_height="320dp"
>
</ListView>
<Button
android:layout_width="fill_parent"
android:layout_height="45dp"
android:text="Refresh"
android:id="#+id/bRef"
/>
My NewsActivty.java:
package com.example.mynews;
import com.example.mynews.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class NewsActivity extends Activity {
Button refresh;
String newsfeed [] = {"latest news", "older news"};
Intent ref = new Intent(this, NewsActivity.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
refresh = (Button) findViewById(R.id.bRef);
ListView newsStuff = (ListView) findViewById(R.id.news);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, newsfeed);
newsStuff.setAdapter(adapter);
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(ref);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news, menu);
return true;
}
}
And my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mynews"
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.example.mynews.NewsActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I built everything up piece by piece and ran it as I finished something new (the layout, the array adapter, etc) and the activity started to break once I added the OnClickListener (emulator tells me "mynews has unfortunately stopped") for my Refresh button. I'm sure there's a better way to accomplish what i'm trying to do, but any assistance with what I have so far would be greatly appreciated.
Did u define a class ref.java?
if yes then you need define that activity(ref) in the manifest file.
So I know it has to do with my "startActivity( myIntent )" line of code. Here is my first Activity file
public class Commander2 extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "AccessCodeEntry";
private static final String LEVEL_ONE_ACCESS_CODE = "derp";
#Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.accesscodeentry );
}
protected void onDestroy() {
super.onDestroy();
}
public void accessCodeEntered( View v ) {
EditText loginPassword = (EditText) findViewById( R.id.editText1 );
if( loginPassword.toString().equals( LEVEL_ONE_ACCESS_CODE ) ) {
Intent myIntent = new Intent( Commander2.this, LevelOne.class );
startActivity( myIntent );
} else {
Intent myIntent = new Intent( Commander2.this, LevelZero.class );
startActivity( myIntent );
}
}
}
Here is the XML file as well.
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="accessCodeEntered"
android:text="OK" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="19dp"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText1"
android:layout_alignLeft="#+id/editText1"
android:text="Enter Access Code, or leave blank for Level 0"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
My code just exits as soon as I enter a password and press the OK button, no matter what the output. I have commented out the startActivity( myIntent ) lines and the code finishes without crashing. Does anyone know what I'm doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.eti"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name">
<activity android:name=".Commander2"
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=".LevelOne"
android:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LevelZero"
android:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
EDIT : i am updating my answer
so finally i got where you were going wrong
check your if condition
in accessCodeEntered() method
of Commander2.java Activity
if(loginPassword.toString().equals(LEVEL_ONE_ACCESS_CODE)){}
here you are not getting the value of password and try to use it
so i added "getText()" method and its working fine like this
if(loginPassword.getText().toString().equals(LEVEL_ONE_ACCESS_CODE)){}
Commander2.java
package my.eti;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Commander2 extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private static final String TAG = "AccessCodeEntry";
private static final String LEVEL_ONE_ACCESS_CODE = "derp";
private Button btnOK;
private EditText passTxt;
private String strPass;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accesscodeentry);
passTxt = (EditText) findViewById(R.id.editText1);
btnOK =(Button) findViewById(R.id.button1);
btnOK.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==btnOK){
/*... here is the problem we havent added getText() to get value from EditText...*/
if(loginPassword.getText().toString().equals(LEVEL_ONE_ACCESS_CODE)){
Intent myIntent = new Intent( Commander2.this, LevelOne.class );
Toast.makeText(getBaseContext(), "if condition is true..", Toast.LENGTH_SHORT).show();
startActivity( myIntent );
}
else{
Intent myIntent = new Intent( Commander2.this, LevelZero.class );
Toast.makeText(getBaseContext(), "else condition is true..", Toast.LENGTH_SHORT).show();
startActivity(myIntent);
}
}
}
}
try to implement this code and where you going wrong.
still you get any trouble share with me Thanks.
Please post your logcat errors, but my guess is to make sure that LevelOne and LevelZero are in your manifest file. Also, it might help to change this:
loginPassword.toString().equals( LEVEL_ONE_ACCESS_CODE )
to:
loginPassword.getText().toString().equals( LEVEL_ONE_ACCESS_CODE )
have you to register LevelOne and LevelZero in manifest file
Here is a link to a similar SO question that shows how to add the activities to your manifest.
Add a new activity to the AndroidManifest?