Why is my android application crashing? - android

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?

Related

Linking to website and ending URL with my textview

Eg. "http://www.john.com/" + "textview content from activity"
I've tried a lot of different coding, but I can't make it work. The closest I come finding the solution is a code for Google Search Query, and I've tried to modificate the code for my needs, but it doesn't work.
I have a textview named "article" which value is fetched from a database. Let's say that the output value becomes "4545", so when I click a button I want the browser to open and start at this URL "http://www.john.com/4545".
I would really appriciate the help! Thanks in advance!
MainActivity.java
package com.example.lindstreamerss.androidsqlitesearch;
import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textViewInput;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewInput = (TextView) findViewById(R.id.article);
public void onSearchClick(View v) {
try {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
String term = textViewInput.getText().toString();
intent.putExtra(SearchManager.QUERY, term);
startActivity(intent);
} catch (Exception e) {
// TODO: handle exception
}
}
}
layout_item.xml
<TextView
android:id="#+id/article"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="28dp"
android:fontFamily="#font/regular"
android:gravity="center_vertical|start"
android:text="4545"
android:textAllCaps="false"
android:textSize="19sp">
<requestFocus />
</TextView>
<Button
android:id="#+id/imageButton"
android:background="#color/white"
android:fontFamily="#font/regular"
android:drawableLeft="#drawable/ic_web"
android:paddingLeft="25dp"
android:textSize="15sp"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onSearchClick"
android:paddingRight="40dp"
android:text="Visa artikel" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lindstreamerss.androidsqlitesearch">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name="com.example.lindstreamerss.androidsqlitesearch.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
This simply works, on button click perform below code, and make necessary change to make it compatible with you.
String term = textViewInput.getText().toString();
String myUrl = "http://www.john.com/4545"+term;
String url = "https://developer.android.com/"+"jetpack";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
Solution if you want to import from other XML:
View inflatedView = getLayoutInflater().inflate(R.layout.YOURLAYOUT, null);
TextView text = (TextView) inflatedView.findViewById(R.id.YOURID); String
escapedQuery = text.getText().toString(); Uri uri =
Uri.parse("https://www.YOURWEBSITE.com/" + escapedQuery); Intent intent = new
Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
Solution if you're using a ViewHolder (in my case RecyclerView:
String example = ((TextView) recyclerView.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.YOURID)).getText().toString();
Uri uri = Uri.parse("https://www.YOURWEBSITE.com/" + example);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
Happy coding ;)

Why placing a call programatically gives error message "Internet Calling not Supported"?

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.

Moving from one activity to another Activity in Android

I want to move from one activity to another (using virtual device). When I click on button to move, My emulator ones a dialog box showing unfortunately SMS1 has stopped working (SMS1 is my app name).
Can anybody help me in correcting my code?
MainActivity.java:
package com.example.sms1;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener
{
Button b1;
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
tv1 = (TextView) findViewById(R.id.textView1);
b1.setOnClickListener(this);
}
#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;
}
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(),NextActivity.class);
startActivity(i);
setContentView(R.layout.avtivity_next);
}
}
Here is the NextActivity
package com.example.sms1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class NextActivity extends Activity {
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.avtivity_next);
tv1 = (TextView) findViewById(R.id.textView1);
}
#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;
}
}
Manifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms1"
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.sms1.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>
</application>
</manifest>
NextActivityLayout
<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=".NextActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next activity" />
</RelativeLayout>
MainActivity Layout
<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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="80dp"
android:layout_toRightOf="#+id/textView1"
android:text="Button" />
</RelativeLayout>
First You have to use this code in MainActivity.java class
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(),NextActivity.class);
startActivity(i);
}
You can pass intent this way.
Second
add proper entry into manifest.xml file.
<activity android:name=".NextActivity" />
Now see what happens.
You haven't defined NextActivity in the AndroidManifest.xml file.
Add these lines in android manifest after</activity> tag. It should work.
<activity
android:name=".NextActivity" >
</activity>
final code will be
<application
android:allowBackup="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name" >
<activity
android:name=".MainActivity"
android:label="Main Activity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NextActivity" >
</activity>
</application>
button1 in activity2
code written in activity 2
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// starting background task to update product
Intent fp=new Intent(getApplicationContext(),activity1.class);
startActivity(fp);
}
});
This might help
Simply add your NextActivity in the Manifest.XML file
<activity
android:name="com.example.sms1.NextActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
public void onClick(View v)
{
startActivity(new Intent(getApplicationContext(), Next.class));
}
it is direct way to move second activity and there is no need for call intent
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent(Activity1.this,Activity2.class);
startActivity(intent);
}
1) place setContentView(R.layout.avtivity_next); to the next-activity's onCreate() method just like this (main) activity's onCreate()
2) if you have not defined the next-activity in your-apps manifest file then do this also, like:
<application
android:allowBackup="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name" >
<activity
android:name=".MainActivity"
android:label="Main Activity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NextActivity"
android:label="Next Activity" >
</activity>
</application>
You must have to perform the 2nd step every time you create a new activity, otherwise your app will crash
When you have to go from one page to another page in android changes made in 2 files
Intent intentSignUP = new Intent(this,SignUpActivity.class);
startActivity(intentSignUP);
add activity in androidManifest file also like
<activity android:name=".SignUpActivity"></activity>
setContentView(R.layout.avtivity_next);
I think this line of code should be moved to the next activity...
Below code is working fine with Android 4.3:
Intent i = new Intent(this,MainActivity2.class);
startActivity(i);
First you have to declare the activity in Manifest. It is important. You can add this inside application like this.
It is mainly due to unregistered activity in manifest file as "NextActivity"
Firstly register NextActivity in Manifest like
<activity android:name=".NextActivity">
then use the code in the where you want
Intent intent=new Intent(MainActivity.this,NextActivity.class);
startActivity(intent);
where you have to call the NextActivity..
Register your java class on Android manifest file
After that write this code on button click
startActivity(new intent(MainActivity.this,NextActivity.class));
You can do
Intent i = new Intent(classname.this , targetclass.class);
startActivity(i);

Application stops unexpectedly - Android

I am new to android development. After setting up an android project, I tried to get text input and passe it to another activity (screen).
When I run the project I don't get an error but when I click the application it shows the first screen then when click the button in the screen it gives the error 'application stopped unexpectedly'.
When I try the code without passing data from first screen to second screen , the application works properly.
This is MainActivity.java file:
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 MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et= (EditText) findViewById(R.id.editText1);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//intent class is used for activating another or component or an activity
Intent intent =new Intent(MainActivity.this, Second.class);
intent.putExtra("textval", et.getText().toString());
startActivity(intent);
}
});}}
Here is the code for Second.java file:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("textval"));
}
}
Here is the activitymain.xml code:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<EditText android:id="#+id/editText1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" android:layout_alignParentTop="true"
android:ems="10"
android:inputType="text"
>
<requestFocus />
</EditText>
<Button android:id="#+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1" android:layout_marginTop="28dp"
android:text="#string/button" />
</RelativeLayout>
Here is the second xml file:
<?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="match_parent"
android:orientation="vertical">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="#string/textview" />
</LinearLayout>
Please help me to find the error. Since I don't get an error notification I am not able to proceed.
Thanks in advance...
This is what you missed on your Second.java:
setContentView(R.layout.second);
put it above of your textview declaration.
In your Second java file make changes as follows
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("textval"));
}
}
Declare Second Activity in android manifest file .
<activity android:name=".Second"/>
Add setContentView(R.layout.second); in second activity.
Add entry for second activity in your Manifest file.
Put first activity as a Launcher and other as a Default.
Here I have two activity MainActivity and Player.
First I Launches MainActivity and then call other activity Player
Note- Activity name should be same as class name, so Keep same in Manifest file as well.
Also you need to specify the layout file for both activity by setContentView in onCreate function.
Here is sample code-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vt.soc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Player"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="com.vt.soc.PALYER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
- In your Second Activity you forgot to add the setContentView().
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("textval"));
}
- Please also do see that you have added this Second Activity in your Manifest.xml file.

App not launching and get crashed when using the imagebutton in android

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.

Categories

Resources