I have a button bar that links different xml layouts, but I can only get to one and not return to the others when I try to click on a different button. Sorry, in advance for the hassle, still bit of a novice. This is my first post here, but been referencing this site a lot. Thanks in advance.
activity_main.xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:stretchColumns="*"
android:background="#6B1414">
<TableRow
android:id="#+id/tableRow1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<Button
android:id="#+id/btn1"
android:text="#string/Str"
android:textStyle="bold"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:padding="18dip"
android:layout_weight="1"
android:background="#424242"
android:textColor="#ffffff"
android:gravity="center"/>
<Button
android:id="#+id/btn2"
android:text="#string/Agl"
android:textStyle="bold"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:padding="18dip"
android:layout_weight="1"
android:background="#424242"
android:textColor="#ffffff"
android:gravity="center"/>
<Button
android:id="#+id/btn3"
android:text="#string/Int"
android:textStyle="bold"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:padding="18dip"
android:layout_weight="1"
android:background="#424242"
android:textColor="#ffffff"
android:gravity="center"/>
<Button
android:id="#+id/btn4"
android:text="#string/Misc"
android:textStyle="bold"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#424242"
android:textColor="#ffffff"
android:padding="18dip"/>
</TableRow>
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 =(Button)findViewById(R.id.btn1);
Button btn2 =(Button)findViewById(R.id.btn2);
Button btn3 =(Button)findViewById(R.id.btn3);
Button btn4 =(Button)findViewById(R.id.btn4);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.activity_main);
return;
}
});
btn2.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.agil_main);
return;
}
});
btn3.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.int_main);
return;
}
});
btn4.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.misc_main);
return;
}
});
}}
Update. The XML looks good, thanks. I'm still stuck with the same problem I had before. I added activities for each layout, but I'm still having the same problem of not being about to cycle through the layouts. Each java file looks the same as this.
MainActivity.java (update with new .class Intents)
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 =(Button)findViewById(R.id.btn1);
Button btn2 =(Button)findViewById(R.id.btn2);
Button btn3 =(Button)findViewById(R.id.btn3);
Button btn4 =(Button)findViewById(R.id.btn4);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent = new Intent(getApplicationContext(), MainActivity.class);
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.activity_main);
return;
}
});
btn2.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent = new Intent(getApplicationContext(), MainAgil.class);
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.agil_main);
return;
}
});
btn3.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent = new Intent(getApplicationContext(), MainInt.class);
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.int_main);
return;
}
});
btn4.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent = new Intent(getApplicationContext(), MainMisc.class);
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.misc_main);
return;
}
});
}}
Define your button bar in a separate XML layout. In every XML you need that bar include that button bar using <include/> tag. And in every activity in which XML you have included the button bar handle the onclick of the buttons separately.
Related
I'm building an app which has 3 functions a converter, a calculator and a notes section. When I click on the converter button on the home page it brings me to the converter activity / page. But when I click on the calculator button on the home page it won't open. Here is the code below. Any reason as to why? Thanks in advance.
MainActivity
package com.qub.buildersbuddy;
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.Toast;
public class MainActivity extends Activity {
Button buttonConverter;
Button buttonCalculator;
Button buttonNotePad;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button ConvertBtn = (Button) findViewById(R.id.butonConverter);
ConvertBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,CentInch.class);
startActivity(intent);
}
});
}
public void setupConverterButton(){
buttonConverter = (Button) findViewById(R.id.butonConverter);
// Button messageButton = (Button) findViewById(R.id.butonConverter);
}
public void CentToInch(){
buttonConverter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//opening the
try{
Class centClass = Class
.forName("com.qub.buildersbuddy.CentInch");
Intent myintent = new Intent(MainActivity.this,centClass);
startActivity(myintent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
});
}
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button CalcBtn = (Button) findViewById(R.id.buttonCalc);
CalcBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Calculator.class);
startActivity(intent);
}
});
}
public void setupCalculatorButton(){
buttonCalculator = (Button) findViewById(R.id.buttonCalc);
// Button messageButton = (Button) findViewById(R.id.butonConverter);
}
public void Calculator(){
buttonCalculator.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//opening the
try{
Class calcClass = Class
.forName("com.qub.buildersbuddy.Calculator");
Intent myintent = new Intent(MainActivity.this,calcClass);
startActivity(myintent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
});
}
}
activity_main:
<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=".CentInch" >
<Button
android:id="#+id/butonConverter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:text="Converter" />
<Button
android:id="#+id/buttonCalc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/butonConverter"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="Calculator" />
<Button
android:id="#+id/buttonNotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonCalc"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:text="Notes" />
</RelativeLayout>
Is this the exact code you're using? Your #onCreate1 method will never get called. #onCreate gets called because it overrides a method from the class you extended (Activity), and something in Activity calls the method when the activity first starts. Move your calculator button logic into the first #onCreate method.
I have a main activity which should load a small splash xml file for 2 or 3 seconds when the app is opened. I tried this snippet of code in the oncreate before adding it to my major project. Keep in mind, both apps worked seperately but for some reason, i get a null pointer exception when running the app..... help?
MainActivity:
package com.Depauw.dpuhelpdesk;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button knowledgeBase, submitRequest, helpme, faq, technician, call;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
//display the logo during 5 secondes,
new CountDownTimer(2000,1000){
public void onTick(long millisUntilFinished){}
public void onFinish(){
//set the new Content of your activity
MainActivity.this.setContentView(R.layout.activity_main);
}
}.start();
Initialize();
}
private void Initialize(){
knowledgeBase = (Button) findViewById(R.id.knowledgebase1);
submitRequest = (Button) findViewById(R.id.submitrequest1);
helpme = (Button) findViewById(R.id.helpButton);
faq = (Button) findViewById(R.id.faqButton);
technician = (Button) findViewById(R.id.submitrequest2);
call = (Button) findViewById(R.id.callButton);
knowledgeBase.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent knowledgeIntent = new Intent(MainActivity.this, knowledgebase1_activity.class);
startActivity(knowledgeIntent);
}
});
submitRequest.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent requestIntent = new Intent(MainActivity.this, submitRequest_activity.class);
startActivity(requestIntent);
}
});
helpme.setOnClickListener(new OnClickListener(){ //dialog box
public void onClick(View arg0){
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Help");
builder.setMessage("This app allows you to access the IT KnowledgeBase from DePauw's website." + " " +
"If you experience any issues using our app, please send us an email to helpdesk#depauw.edu or call 765-658-4294");
builder.setCancelable(true);
builder.setIcon(R.drawable.ic_launcher);
builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
builder.setOnCancelListener(null);
}
});
builder.create().show(); // create and show the alert dialog
}
});
faq.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent requestIntent = new Intent(MainActivity.this, activity_main_faq.class);
startActivity(requestIntent);
}
});
technician.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent requestIntent = new Intent(MainActivity.this, submit_technician_request.class);
startActivity(requestIntent);
}
});
call.setOnClickListener(new OnClickListener(){ //dialog box
public void onClick(View arg0){
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Complete the call?");
builder.setMessage("Click yes to connect your call. Otherwise, click no.");
builder.setCancelable(true);
builder.setIcon(R.drawable.ic_launcher);
//saying yes completes the call. This way, we don't get accidently calls as often
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:17656584294"));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("Calling a Phone Number", "Call failed", activityException);
}
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = builder.create();
// show it
alertDialog.show();
}
});
}
#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;
}
}
splash.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#raw/splash2" />
main_activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".basic_activity1"
tools:ignore="MergeRootFrame"
android:background="#000000" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/header"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#EAC117"
android:textSize="35sp" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#raw/firstintentlogo"
android:layout_weight="1" android:contentDescription="#string/headLogoName"/>
</LinearLayout>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/knowledgebase1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/knowledgebase1"
android:textColor="#EAC117" />
<Button
android:id="#+id/submitrequest1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/submitrequest1"
android:textColor="#EAC117" />
<Button
android:id="#+id/submitrequest2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/submitTechnician"
android:textColor="#EAC117" />
<Button
android:id="#+id/helpButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/helpButton"
android:textColor="#EAC117" />
<Button
android:id="#+id/faqButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/faqButton"
android:textColor="#EAC117" />
<Button
android:id="#+id/callButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/callButton"
android:textColor="#EAC117" />
</LinearLayout>
</ScrollView>
</FrameLayout>
Initialize() is getting called immediately.
This is happening before MainActivity.this.setContentView(R.layout.activity_main) is called.
I have a small app that when button presses navigates when moving from main screen to next screen this works fine, but when I added a button on the next page (to go back) it breaks.
Fun.java
package com.forcetechnology.OptusApp;
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;
public class Fun extends Activity {
OnClickListener backListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fun);
Button backButtonf = (Button)findViewById(com.forcetechnology.OptusApp.R.id.backtoMainf);
backListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.OptusAppMain");
startActivity(i);
}
};
backButtonf.setOnClickListener(backListener);
}
}
Fun.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" >
<ImageButton
android:id="#+id/backtoMainf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/val5" />
</LinearLayout>
Main.xml
<ImageButton
android:id="#+id/funbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/val5"
android:src="#drawable/val5" />
<ImageButton
android:id="#+id/executionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/funbutton"
android:layout_alignParentRight="true"
android:background="#drawable/val2"
android:src="#drawable/val2" />
<ImageButton
android:id="#+id/performancebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/executionbutton"
android:layout_toLeftOf="#+id/funbutton"
android:background="#drawable/val3"
android:src="#drawable/val4" />
<ImageButton
android:id="#+id/innovationbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/executionbutton"
android:layout_alignParentLeft="true"
android:background="#drawable/val3"
android:src="#drawable/val3" />
<ImageButton
android:id="#+id/peoplebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/innovationbutton"
android:layout_toLeftOf="#+id/executionbutton"
android:background="#drawable/val1"
android:src="#drawable/val1" />
</RelativeLayout>
OPtusAppMain.java
package com.forcetechnology.OptusApp;
import android.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.TextView;
import android.widget.Toast;
public class OptusAppMain extends Activity
{
OnClickListener funListener,executionListener,innovationListener,peopleListener,performanceListener;;
TextView testView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.forcetechnology.OptusApp.R.layout.main);
ImageButton funButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.funbutton);
ImageButton executionButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.executionbutton);
ImageButton innovationButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.innovationbutton);
ImageButton peopleButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.peoplebutton);
ImageButton performanceButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.performancebutton);
funListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Fun");
startActivity(i);
}
};
executionListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Execution");
startActivity(i);
}
};
innovationListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Innovation");
startActivity(i);
}
};
peopleListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.People");
startActivity(i);
}
};
performanceListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Performance");
startActivity(i);
}
};
funButton.setOnClickListener(funListener);
executionButton.setOnClickListener(executionListener);
innovationButton.setOnClickListener(innovationListener);
peopleButton.setOnClickListener(peopleListener);
performanceButton.setOnClickListener(performanceListener);
}
}
Edit: I have traced the error to this line Button backButtonf = (Button)findViewById(com.forcetechnology.OptusApp.R.id.backtoMainf); in fun.java.
In the onClick() of backListener, just call finish() to go back to the previous activity.
Lets take an example: You have two activities "A" & "B", You start your "B" activity from "A" that means your "A" activity is in stack so there is no need to start it again, just finish your "B" activity with "finish()" method.
public class A extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
public void onAbuttonClick()
{
startActivity(new Intent(A.this,B.class));
}
}
}
public class B extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
public onBbuttonclick(View v)
{
finish();
}
}
}
Can some one tell me were i am going wrong. i have three activity's that i want to connect togever. This code is my first java file and first xml file. i think my onclick code some where is not right. my end result is that all 3 activity connect with 3 image buttons...thank
java 1.code
package my.hope;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.content.Intent;
public class NewhopeActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView myImage = (ImageView) findViewById(R.id.imageButton1);
myImage.setOnClickListener(new OnClickListener() {
intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
);
}
}
xml.code
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bt" />
<ImageButton
android:id="#id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:onClick="Act2"/>
You have to put the startActivity() into the actual onClick() method.
myImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
}
}
Change
ImageView myImage = (ImageView) findViewById(R.id.imageButton1);
myImage.setOnClickListener(new OnClickListener() {
intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
to
ImageView myImage = (ImageView) findViewById(R.id.imageButton1);
myImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
}
}
I have 3 buttons in total. And it seemed that, if I don't press the first button, and instead second or third button right after I startup, my application will forcestop.
My code for part1 : http://pastebin.com/udkEdF3E
package project.ernest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Part1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button a = (Button) findViewById(R.id.Disclaimer);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent b = new Intent(getBaseContext(), Part2.class);
Part1.this.startActivity(b);
final Button c = (Button) findViewById(R.id.Orientation);
c.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent d = new Intent(getBaseContext(), Part3.class);
Part1.this.startActivity(d);
final Button e = (Button) findViewById(R.id.Course);
e.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent f = new Intent(getBaseContext(), Part4.class);
Part1.this.startActivity(f);
}
});
};
});
};
});
};
}
my code for main.xml : http://pastebin.com/SxfxfJ4a
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/myColor" android:weightSum="1">
<LinearLayout android:orientation="vertical" android:id="#+id/linearLayout1" android:weightSum="1" android:layout_width="wrap_content" android:layout_height="404dp">
<TextView android:text="#string/HospName" android:id="#+id/textView1" android:textSize="20mm" android:layout_width="324dp" android:layout_height="142dp"></TextView>
<Button android:text="#string/Disclaimer" android:layout_weight="0.37" android:textSize="5mm" android:layout_height="75dp" android:layout_width="fill_parent" android:id="#+id/Disclaimer" android:drawingCacheQuality="high" android:onClick="#string/DisclaimerHandler"></Button>
<Button android:text="#string/Orientation" android:textSize="5mm" android:layout_height="75dp" android:layout_weight="0.37" android:layout_width="fill_parent" android:id="#+id/Orientation" android:onClick="#string/OrientationHandler"></Button>
<Button android:text="#string/Course" android:textSize="5mm" android:layout_height="75dp" android:layout_weight="0.37" android:layout_width="fill_parent" android:id="#+id/Course" android:onClick="#string/CourseHandler"></Button>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_weight="0.19" android:layout_height="wrap_content" android:id="#+id/linearLayout2" android:weightSum="1">
<Button android:layout_weight="0.23" android:text="#string/Search" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/SearchHandler" android:id="#+id/Search"></Button>
<Button android:layout_weight="0.23" android:text="#string/Contact" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/ContactHandler" android:id="#+id/Contact"></Button>
<Button android:onClick="#string/LinkHandler" android:layout_height="75dp" android:layout_width="75dp" android:layout_weight="0.23" android:textSize="1.9mm" android:text="#string/Link" android:id="#+id/Link"></Button>
<Button android:layout_weight="0.23" android:text="#string/Copyright" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/CopyrightHandler" android:id="#+id/Copyright"></Button>
</LinearLayout>
</LinearLayout>
The error I get is either:
java.lang.IllegalStateException: Could not find a method CourseHandler(View) in activity class project.ernest.Part1 or...
java.lang.IllegalStateException: Could not find a method OrientationHandler(View) in activity class project.ernest.Part1 or...
Please help me out!
You've placed initialization of buttons 2 and 3 inside the first button's onClickListener(), means they won't be initialized before you click the first button. Fix your code.
this is the format.
public class Part1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button a = (Button) findViewById(R.id.Disclaimer);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent b = new Intent(getBaseContext(), Part2.class);
Part1.this.startActivity(b);
}
});
final Button c = (Button) findViewById(R.id.Orientation);
c.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent d = new Intent(getBaseContext(), Part3.class);
Part1.this.startActivity(d);
}
});
final Button e = (Button) findViewById(R.id.Course);
e.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent f = new Intent(getBaseContext(), Part4.class);
Part1.this.startActivity(f);
}
});
}
}