My application is stopped when executed. I'm starting to program for android, just put a background and buttons, but it seems that I have problems creating the manifest. Before this project had many problems creating a new eclipse I put an activity together with the library and I started appcontainer_v7 errenter code hereored. To use the SDK 19 I decided to do everything manually.
CLASS:
package com.progra.cubebreaker;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Menu extends Activity implements OnClickListener{
Button Jugar, Puntuaciones, Instrucciones;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Jugar.findViewById(R.id.btJugar);
Puntuaciones.findViewById(R.id.btInstrucciones);
Instrucciones.findViewById(R.id.btInstrucciones);
Jugar.setOnClickListener(this);
Puntuaciones.setOnClickListener(this);
Instrucciones.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btJugar:
break;
case R.id.btPuntuaciones:
Toast text = Toast.makeText(this, "Pendiente...", Toast.LENGTH_SHORT);
text.show();
break;
case R.id.btInstrucciones:
Toast text1 = Toast.makeText(this, "Pendiente...", Toast.LENGTH_SHORT);
text1.show();
break;
}
}
}
LAYOUT:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/fondo1" >
<Button
android:id="#+id/btInstrucciones"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/btPuntuaciones"
android:layout_marginBottom="89dp"
android:text="#string/instrucciones"
android:textSize="40dp"
android:textStyle="italic|normal|bold" />
<Button
android:id="#+id/btPuntuaciones"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btInstrucciones"
android:layout_centerHorizontal="true"
android:layout_marginBottom="60dp"
android:text="#string/puntuaciones"
android:textSize="40dp"
android:textStyle="italic|normal|bold" />
<Button
android:id="#+id/btJugar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btPuntuaciones"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp"
android:text="#string/jugar"
android:textColorHint="#android:color/background_dark"
android:textSize="40dp"
android:textStyle="italic|normal|bold" />
</RelativeLayout>
MANIFEST:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.progra.cubebreaker"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Menu"
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 are not mapping the layout objects to java class objects thats y ur application is crashing.
You need to map the java button by converting id of button or any other attribute to (Button) etc.
Modify your code :
Jugar = (Button) findViewById(R.id.btInstrucciones);
Puntuaciones= (Button) findViewById(R.id.btPuntuaciones);
Instrucciones = (Button) findViewById(R.id.btInstrucciones);
Hope that helps.
just replace this code,
Jugar.findViewById(R.id.btJugar);
Puntuaciones.findViewById(R.id.btInstrucciones);
Instrucciones.findViewById(R.id.btInstrucciones);
by
Jugar = (Button) findViewById(R.id.btJugar);
Puntuaciones = (Button) findViewById(R.id.btPuntuaciones);
Instrucciones = (Button) findViewById(R.id.btInstrucciones);
Your application is crashing as you are not initializing the widgets in your code.
Here is the error:
Jugar.findViewById(R.id.btJugar);
Puntuaciones.findViewById(R.id.btInstrucciones);
Instrucciones.findViewById(R.id.btInstrucciones);
Change it to:
Jugar = (Button) findViewById(R.id.btJugar);
Puntuaciones = (Button) findViewById(R.id.btPuntuaciones);
Instrucciones = (Button) findViewById(R.id.btInstrucciones);
Related
I just wanted to click on a button in the MainActivity. After I clicked on this button a new activity appears. In this activity is also a button and when I click on this button a text appears. My Problem now is, that the text doesn't appear, when I click on the button. My code for clicking on a button so that a text appears is actually working. But not if I use it in a new Activity/Layout/ page. So there must be s.th. wrong with the Connection between the first and the second activity?!
MainActivity.java:
package com.example.xxx;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;}
{}
public void page2 (View view){
setContentView(R.layout.pagetwo);
}}
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/BtnKlick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="178dp"
android:text="Button"
android:onClick="page2"/>
</RelativeLayout>
Pagetwo.java:
package com.example.xxx;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Pagetwo extends Activity implements OnClickListener {
public Button btn1;
public TextView tw1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pagetwo);
btn1 = (Button) findViewById(R.id.BtnKlick);
tw1 = (TextView) findViewById(R.id.Text);
btn1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tw1.setText("Hallo");
}}
pagetwo.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" >
<Button
android:id="#+id/BtnKlick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Klick" />
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="87dp" />
</RelativeLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxx"
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.xxx.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>
Update
All you seem to be doing is swapping your layouts within PageOne activity.
Change
public void page2 (View view){
setContentView(R.layout.pagetwo);
}
to
public void page2 (View view){
Intent i = new Intent(this, Pagetwo.class);
startActivity(i);
}
This will actually launch your pagetwo activity rather than just swapping layouts within pageone activity
Make that change and all should be good. You will most likely have to add some imports, at least for the intent class
Also Activities must be registered in the manifest. To add the activity to your manifest check the code below
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.xxx.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="Pagetwo"
android:label="#string/app_name" >
</activity>
</application>
Change the label to something more appropriate than app name once you have it working
End update
Try renaming your xml view id's to meet coding standards by using all lower case letters and separating words with underscores. The Android platform may well be getting confused as Text is a reserved word.
so
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:textSize="70sp" />
becomes
<TextView
android:id="#+id/tv_hallo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:textSize="70sp" />
and your code becomes
...
public TextView tw;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.eclipse);
btn = (Button) findViewById(R.id.BtnKlick);
tw = (TextView) findViewById(R.id.tv_hallo);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tw.setText("Hallo");
}
...
Not sure if this is going to have much of an effect but it is just possible android is getting the word Text confused
Also check your logcat output to see if there are any errors.
I have developed an app for page navigation . when a button is clicked the page should navigate to the second page. when i run the project and opened my app its showing unfortunately API demo has stopped. I have posted the entire code of my app . PLS HELP ME OUT...
//MAIN ACTIVITY
<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="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginLeft="73dp"
android:layout_marginTop="14dp"
android:text="Click" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="34dp"
android:text="Main Activity" />
</RelativeLayout>
//SECONDSCREEN ACTIVITY
<?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:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sceond Screen Activity"
tools:context=".SecondScreenActivity"/>
</LinearLayout>
//JAVA CODE FOR MAIN ACTIVITY
package com.example.navigate;
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 {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addButtonOnClickEventListener();
}
public void addButtonOnClickEventListener()
{
Button button = (Button)findViewById(R.id.button1);
final Context context = this;
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,SecondScreenActivity.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;
}
}
// JAVA CODE FOR SECOND SCREEN ACTIVITY
package com.example.navigate;
import android.app.Activity;
public class SecondScreenActivity extends Activity {
}
// MANIFEST FILE
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.navigate"
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.navigate.SecondScreenActivity"
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>
First problem MainActivity is your launcher page here but you define the secondactivity as luncher in manifest.change it first.
Add an oncreate() to secondactivity otherwise there is nothing to show when the button click.
Third change your MainActivity Context to activity which is MainActivity.this not local
Other things are fine , and hope you understand your small problems..
Change your manifest to something like this:
// MANIFEST FILE
<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.navigate.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 = ".SecondScreenActivity" />
</application>
And in your SecondScreenActivity add an oncreate method with the contet view like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity_layout);
}
And also in your mainactivity change the context to MainActivity.this . Here you define the context inside onclicklistener which links to your local method not the class.
My Application name is timepass
The below file is MainActivity.java
package com.example.timepass;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
int counter;
Button add, sub;
TextView Display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add= (Button) findViewById(R.id.badd);
sub=(Button) findViewById(R.id.bsub);
Display=(TextView) findViewById(R.id.tvdisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
Display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
Display.setText("Your total is " + counter);
}
});
}
#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;
}
}
The below file is activity_main.xml which is realted to the above file MainActivity.java
<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"
android:background="#drawable/pic_two">"
<Button
android:id="#+id/badd"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvdisplay"
android:layout_alignTop="#+id/bsub"
android:text="#string/but1"
android:textSize="20sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvdisplay"
android:layout_alignRight="#+id/tvdisplay"
android:text="#string/hello_world"
android:textSize="#dimen/asa" />
<TextView
android:id="#+id/tvdisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:gravity="center"
android:text="#string/str1"
android:textColor="#layout/activity_main"
android:textSize="30sp" />
<Button
android:id="#+id/bsub"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tvdisplay"
android:layout_below="#+id/tvdisplay"
android:layout_marginRight="38dp"
android:layout_marginTop="23dp"
android:text="#string/but2"
android:textSize="20sp" />
</RelativeLayout>
Then I have created another activity which is below which is Splash.java
package com.example.timepass;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity
{
#Override
protected void onCreate(Bundle tpsavedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(tpsavedInstanceState);
setContentView(R.layout.splash);
Thread timer =new Thread()
{
public void run()
{
try
{
sleep(5000);
}
catch(InterruptedException e)
{ {
e.printStackTrace();
}
finally
{
Intent openMainActivity =new Intent("com.example.timepass.MAINACTIVITY");
startActivity(openMainActivity);
}
}
};
timer.start();
}
}
Then I have created Xml file related to Splash.java which is splash.xml
<?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"
android:background="#drawable/pic_background">
</LinearLayout>
NOW I WANT TO RUN FIRST Splash.java file then MainActivity.java file so I have make some changes in Androidmanifest.xml file
here are the changes, and after running my program I get error "Unfortunately timepass has stopped" where timepass is my app name . please solve this issue
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timepass"
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.timepass.Splash"
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.example.timepass.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.timepass.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
//just do it like this
Intent openMainActivity =new Intent(this,MainActivity.class);
startActivity(openMainActivity);
instead of
Intent openMainActivity =new Intent("com.example.timepass.MAINACTIVITY");
startActivity(openMainActivity);
MainActivity in your manifest is used in two places. In one place it is in fully capital letter. Change that and give a try
You have an extra { before e.printStackTrace(); in your Splash.java. Remove that and you'll be good to go.
Also post log for furthur errors in the future
I have edited your AndroidManifest.xml update your code after your application work properly.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timepass"
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.timepass.Splash"
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.example.timepass.MainActivity"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
change your code in splash.java
Intent i = new Intent(splash.this, MainActivity.class);
startActivity(i);
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
This is the scr java file TestingbankaiActivity.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class TestingbankaiActivity extends Activity implements OnClickListener {
Button b;
TextView tv;
// Class c,c1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.testingactivity);
initialize();
}
private void initialize() {
// TODO Auto-generated method stub
b = (Button) findViewById(R.id.b1);
tv = (TextView) findViewById(R.id.tvb1);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.b1:
Intent i = new Intent(android.content.Intent.ACTION_DIAL,
Uri.parse("tel:9642777959"));
startActivity(i);
break;
case R.id.tvb1:
Intent i1 = new Intent(android.content.Intent.ACTION_DIAL,
Uri.parse("tel:9642777959"));
startActivity(i1);
break;
}
}
}
this is the 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" >
<Button
android:id="#+id/b1"
android:layout_width="70dip"
android:layout_height="70dip"
android:text="hhhh" />
<TextView
android:id="#+id/tvb1"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="hha" />
</LinearLayout>
This is the manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testing.bankai"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestingbankaiActivity"
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>
In the end i am getting the screen with a text view and a button. What i want is if the user presses the textview or the button it should that to the action dial intent.
You need to link the "click the button/textview" event to your code. All you have done is created the buttons, but you've not set up the OnClickListener. There are 2 ways to do this:
1) Add the following to the layout.xml for your Button and TextView:
android:onClick="onClick"
2) Add the following code to your initialize() method:
b.setOnClickListener(this);
tv.setOnClickListener(this);
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.