I have an Android program using the IntelliJ 12 Community Edition IDE. I need to navigate between the different layouts (.xml) I made with the use of buttons. But, whenever I run it using the emulator, it only opens the main.xml screen, whenever I click the buttons, it says that: "Unfortunately, ITax has stopped working."
This is my code for the MyActivity.java class:
package com.example.ITax;
import android.app.Activity;
import android.content.Intent;
android.os.Bundle;
android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.btn_info);
Button button2 = (Button)findViewById(R.id.btn_tutorial);
Button button3 = (Button)findViewById(R.id.btn_calc);
Button button4 = (Button)findViewById(R.id.btn_back_from_calcu);
Button button5 = (Button)findViewById(R.id.btn_next_from_calcu);
Button button6 = (Button)findViewById(R.id.btn_back_from_monthlyorannual);
Button button7 = (Button)findViewById(R.id.btn_next_from_monthlyorannual);
Button button8 = (Button)findViewById(R.id.btn_back_from_civilstatus);
Button button9 = (Button)findViewById(R.id.btn_next_from_civilstatus);
Button button10 = (Button)findViewById(R.id.btn_back_from_inputamount_monthly);
Button button11 = (Button)findViewById(R.id.btn_compute_from_monthly);
Button button12 = (Button)findViewById(R.id.btn_back_from_outputamount_monthly);
Button button13 = (Button)findViewById(R.id.btn_home_from_outputamount_monthly);
}
public void Open_BasicInfo()
{
Intent i = new Intent(this, OpenBasicInfo.class);
startActivity(i);
}
public void Open_Tutorial()
{
Intent i = new Intent(this, OpenTutorial.class);
startActivity(i);
}
public void Open_Calculator()
{
Intent i = new Intent(this, OpenCalculator.class);
startActivity(i);
}
public void Open_MonthlyOrAnnually()
{
Intent i = new Intent(this, OpenMonthlyOrAnnually.class);
startActivity(i);
}
public void Open_CivilStatus()
{
Intent i = new Intent(this, OpenCivilStatus.class);
startActivity(i);
}
public void Open_InputAmountsFromMonthly()
{
Intent i = new Intent(this, OpenInputAmountsFromMonthly.class);
startActivity(i);
}
public void Open_OutputAmountsFromMonthly()
{
Intent i = new Intent(this, OutputAmountsFromMonthly.class);
startActivity(i);
}
public void Open_Main()
{
Intent i = new Intent(this, MyActivity.class);
startActivity(i);
}
}
I already created a class for each layout and declared in the Android manifest like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ITax"
android:versionCode="1"
android:versionName="ITax 1.0">
<uses-sdk android:minSdkVersion="1"/>
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity android:name=".MyActivity"
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=".OpenCalculator">
</activity>
<activity android:name=".OpenCivilStatus">
</activity>
<activity android:name=".OpenInputAmountsFromMonthly">
</activity>
<activity android:name=".OutputAmountsFromMonthly">
</activity>
<activity android:name=".OpenTutorial">
</activity>
<activity android:name=".OpenBasicInfo">
</activity>
<activity android:name=".OpenMonthlyOrAnnually">
</activity>
<activity android:name=".OpenMain">
</activity>
</application>
</manifest>
I already put the code android:onClick = "name of method" in all of the buttons.
Help! what seems to be the wrong I did here? :(
My application in the emulator stops working whenever I already clicked the buttons. >.<
In order to comply with the xml android:onclick="MethodName". Your activity must implement method that looks like this...
public void MethodName(View v){}
All of your onclick events are missing passing the View parameter.
Personally I don't like using the xml, because if you make a spelling mistake in your code. Your activity will crash because it can't inflate the xml. This imo leaves you vulnerable to silly mistakes. Here a link in more details about all the ways to do a button onclick event In the end it's up to you.
Related
My manifest.xml raises error for ".RoleActivity". But If I replace my ".roleActivity" with others for checking, they all are okay. Here is my manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zobaed.androidlogin" >
<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=".RoleActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".DoctorLoginActivity">
</activity>
<activity android:name=".PatientLoginActivity">
</activity>
</application>
</manifest>
Here is my RoleActivity. Tried to write switch case here.
public class RoleActivity extends AppCompatActivity {
private Button btnPatient;
private Button btnDoctor;
private Button btnGuest;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log_in_role);
btnPatient = (Button) findViewById(R.id.btpatient);
btnDoctor = (Button) findViewById(R.id.btdoctor);
btnGuest = (Button) findViewById(R.id.btguest);
btnPatient.setOnClickListener((View.OnClickListener) this);
btnDoctor.setOnClickListener((View.OnClickListener) this);
btnGuest.setOnClickListener((View.OnClickListener) this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btdoctor: {
Intent i = new Intent(getApplicationContext(), DoctorLoginActivity.class);
startActivity(i);
break;
}
case R.id.btpatient: {
Intent i = new Intent(getApplicationContext(), PatientLoginActivity.class);
startActivity(i);
break;
}
}
}
}
implement onClickListner in RoleActivity class and change code to
btnPatient.setOnClickListener( this);
btnDoctor.setOnClickListener(this);
btnGuest.setOnClickListener(this);
Your activity is not implementing View.OnClickListener. Unless you implement View.OnClickListener on your activity you cannot cast the activity as an OnClickListener. That is why you are getting error, probably a ClassCastException
btnPatient.setOnClickListener((View.OnClickListener) this);
btnDoctor.setOnClickListener((View.OnClickListener) this);
btnGuest.setOnClickListener((View.OnClickListener) this);
implement View.OnClickListener on your activity. Change
public class RoleActivity extends AppCompatActivity
to
public class RoleActivity extends AppCompatActivity implements View.OnClickListener
and then you can remove that casting
btnPatient.setOnClickListener(this);
btnDoctor.setOnClickListener(this);
btnGuest.setOnClickListener(this);
if you are not implemeting View.OnClickListener on your activity you can add the click listener as an anonymous inner class to handle clicks on views
enter code here
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newuser.applicationtwo">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:roundIcon="#mipmap/ic_launcher_round"
android:screenOrientation="landscape"
android:supportsRtl="true"
>
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:theme="#style/AppTheme"
android:label="#string/app_name">
</activity>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</application>
<activity android:name=".SecondActivity" />
<activity android:name=".ThirdActivity" />
<activity android:name=".FourthActivity" />
<activity android:name=".FifthActivity" />
<activity android:name=".SixthActivity" />
</manifest>
-----------------------------------------------------------------------
package com.example.newuser.applicationtwo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import static android.content.ContentValues.TAG;
public class MainActivity extends Activity implements View.OnClickListener
{
private ImageButton playBtn, multiplayerBtn, settingsBtn;
public MainActivity(ImageButton playBtn, ImageButton multiplayerBtn) {
this.playBtn = playBtn;
this.multiplayerBtn = multiplayerBtn;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.playBtn:
Intent i = new Intent(getApplicationContext(),
SecondActivity.class);
startActivity(i);
onClick(playBtn);
Log.d(TAG, "Play Button Move");
break;
case R.id.TopicBtn8:
Intent j = new Intent(getApplicationContext(),
ThirdActivity.class);
startActivity(j);
onClick(multiplayerBtn);
Log.d(TAG, "Multiplayer Button Move");
break;
case R.id.settingsBtn:
Intent k = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(k);
onClick(settingsBtn);
Log.d(TAG, "Settings Button Move");
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playBtn = (ImageButton) findViewById(R.id.playBtn);
multiplayerBtn = (ImageButton) findViewById(R.id.multiplayerBtn);
settingsBtn = (ImageButton) findViewById(R.id.settingsBtn);
playBtn.setOnClickListener(MainActivity.this);
multiplayerBtn.setOnClickListener(MainActivity.this);
settingsBtn.setOnClickListener(MainActivity.this);
------------------------------------------------------------------------
package com.example.newuser.applicationtwo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import static android.content.ContentValues.TAG;
class SecondActivity extends AppCompatActivity implements
View.OnClickListener {
private Button topic1Btn, topic2Btn, topic3Btn, topic4Btn, topic5Btn,
topic6Btn, topic7Btn, topic8Btn;
public SecondActivity(Button topic1Btn, Button topic2Btn, Button topic3Btn,
Button topic4Btn,
Button topic5Btn, Button topic6Btn, Button topic7Btn,
Button topic8Btn) {
this.topic1Btn = topic1Btn;
this.topic2Btn = topic2Btn;
this.topic3Btn = topic3Btn;
this.topic4Btn = topic4Btn;
this.topic5Btn = topic5Btn;
this.topic6Btn = topic6Btn;
this.topic7Btn = topic7Btn;
this.topic8Btn = topic8Btn;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.topic1Btn:
Intent i = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(i);
onClick(topic1Btn);
Log.d(TAG, "Topic 1 Button Clicked");
break;
case R.id.topic2Btn:
Intent j = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(j);
onClick(topic2Btn);
Log.d(TAG, "Topic 2 Button Clicked");
break;
case R.id.topic3Btn:
Intent k = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(k);
onClick(topic3Btn);
Log.d(TAG, "Topic 3 Button Move");
break;
case R.id.topic4Btn:
Intent l = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(l);
onClick(topic4Btn);
Log.d(TAG, "Topic 4 Button Move");
break;
case R.id.topic5Btn:
Intent m = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(m);
onClick(topic5Btn);
Log.d(TAG, "Topic 5 Button Clicked");
break;
case R.id.topic6Btn:
Intent n = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(n);
onClick(topic6Btn);
Log.d(TAG, "Topic 6 Button Clicked");
break;
case R.id.topic7Btn:
Intent o = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(o);
onClick(topic7Btn);
Log.d(TAG, "Topic 7 Button Move");
break;
case R.id.topic8Btn:
Intent p = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(p);
onClick(topic8Btn);
Log.d(TAG, "Topic 8 Button Move");
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
topic1Btn = (Button) findViewById(R.id.topic1Btn);
topic2Btn = (Button) findViewById(R.id.topic2Btn);
topic3Btn = (Button) findViewById(R.id.topic3Btn);
topic4Btn = (Button) findViewById(R.id.topic4Btn);
topic5Btn = (Button) findViewById(R.id.topic5Btn);
topic6Btn = (Button) findViewById(R.id.topic6Btn);
topic7Btn = (Button) findViewById(R.id.topic7Btn);
topic8Btn = (Button) findViewById(R.id.topic8Btn);
topic1Btn.setOnClickListener(SecondActivity.this);
topic2Btn.setOnClickListener(SecondActivity.this);
topic3Btn.setOnClickListener(SecondActivity.this);
topic4Btn.setOnClickListener(SecondActivity.this);
topic5Btn.setOnClickListener(SecondActivity.this);
topic6Btn.setOnClickListener(SecondActivity.this);
topic7Btn.setOnClickListener(SecondActivity.this);
topic8Btn.setOnClickListener(SecondActivity.this);
Intent intent = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(intent);
}
}
----------------------------------------------------
I wonder if anyone can help? I am trying to build an quiz application and I
am having a problem with the main activity in the manifest file. It seems
to be asking for a constructor and when I run the code it is telling me the
following:Error running SecondActivity: The activity 'SecondActivity' is
not declared in AndroidManifest.xml . Not sure how I would change this as
MainActivity or what else to add for the SecondActivity, unfortunately
reading through similar questions on MainActivity I cannot resolve it.
I attach the Manifest file and both the MainActivity and the SecondActivity
Move
</application>*
right before
</manifest>
because now only first activity is in < application > scope
First remove all constructor which is not needed here and you are not adding all activity under application tag in manifest also you defining the launcher filter out of activity tag add it under activity tag, change manifest as below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newuser.applicationtwo">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:roundIcon="#mipmap/ic_launcher_round"
android:screenOrientation="landscape"
android:supportsRtl="true"
>
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:theme="#style/AppTheme"
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=".SecondActivity" />
<activity android:name=".ThirdActivity" />
<activity android:name=".FourthActivity" />
<activity android:name=".FifthActivity" />
<activity android:name=".SixthActivity" />
</application>
</manifest>
I also recommend you to go through basic android development tutorial
https://developer.android.com/training/index.html
activity tags belong inside the application tag.
This should work:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newuser.applicationtwo">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:roundIcon="#mipmap/ic_launcher_round"
android:screenOrientation="landscape"
android:supportsRtl="true">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:theme="#style/AppTheme"
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=".SecondActivity" />
<activity android:name=".ThirdActivity" />
<activity android:name=".FourthActivity" />
<activity android:name=".FifthActivity" />
<activity android:name=".SixthActivity" />
</application>
</manifest>
Appart the fakt that all the activities you declare in your androidmanifest file, should be included in the <application> </application> tag, also if you want to start one exact activity first (meaning that when the app launches that exact activity will start first, for example if you want a logo image, create an activity for that) , you can change the category name to LAUNCHER, and if you want it not to be the first activity starting when the application launches, put it to DEFAULT like this:
<category android:name="android.intent.category.DEFAULT" />
i have two activity associated with two buttons
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".Subactivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:taskAffinity="com.example.start_cs.sub">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".sub"
>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" android:taskAffinity="com.example.start_cs.main"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".main"
>
</activity>
</application>
MainActivity code
package com.example.start_cs.myapp;
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.main_text_view);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, main.class);
startActivity(intent);
}
});
}
layout code
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/main"
android:background="#drawable/main"
android:layout_centerHorizontal="true"
android:id="#+id/main_text_view"
android:layout_marginTop="17dp"
/>
in this situation
Subactivity button opened sub
but MainActivity button did not open main
but if i put MainActivity above Subactivity
Subactivity button did not open sub
but MainActivity button opened main
ok, based on your responses to my comments, I guess all I can say is that you need to follow a tutorial on how to create an app for android. There are excellent tutorials available all over the internet.
You have two activities, so you should have two layout files. You only posted one.
You should also have two source files, one for each activity. You only posted one.
Every android element in a layout that you would like to reference in your code needs an ID. your code references R.id.main_text_view, but your layout file has no such id in it. I'm surprised your code even compiles.
However, to answer your specific question, what you need is the following:
1) The <name> tags in your manifest file must match the name of your java class source files for each of your activities. So your activity class files appear to be called "MainActivity" and "Subactivity" according to your manifest file. However, see my comment on your onClickListener code below.
Also, your manifest indicates that both of your activies are "LAUNCHER" activities. You only need that tag for activities which you want to be able to launch from the Android application launcher (i.e. the list of all the apps installed on your phone). Seems like you would only want this on your main activity, but you could specify more than one if you like.
2) Your activities are duals of each other (i.e. they sound like they do exactly the same thing - each has a button that launches the other) so the code will be very similar. Your code for MainActivity should look something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.main_text_view); //<-- This tries to find a button in this activity (using the activity's layout file that was used in the call to setContentView() in onCreate(). However, the id you specify doesn't exist in your layout file. This should either not compile or return null.
//This is fine.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, main.class); //<--"main.class" doesn't match either of the activity names declared in your manifest. It should match one of the names declared in the <name> tag of one of your <activity> tags.
startActivity(intent);
}
});
}
Your layout file needs to contain an id for the button that wish to find using findViewById(). Modify your layout file as follows (and create one for each activity - although you could, technically, reference the same layout for each activity. But for now, it is conceptually easier to have separate files).
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/main"
android:background="#drawable/main"
android:layout_centerHorizontal="true"
android:id="#+id/main_text_view" <!-- Here is the line that identifies the button for your app. The format is "#+id/some_name", and is reference as "R.id.some_name" in your code. -->
android:layout_marginTop="17dp"
/>
Now you must do the same in your sub-activity code, but your onClickListener will call the Main Activity instead of your subactivity. So the onClickListener code looks like this for your MainActivity (which launches your subactivity):
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subactivity.class);
startActivity(intent);
}
});
and like this in your subactivity (which launches your Main Activity)
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
}
});
then when i press f8 a i get : ZygoteInit$methodandargscaller.run() source not found
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.copyup"
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.copyup.MainActivity"
android:label="copy up" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Game"
android:label="Copy Up">
</activity>
<activity
android:name=".Rules"
android:label="Copy Up!">
</activity>
<activity
android:name=".Scores"
android:label="Copy Up!">
</activity>
<activity
android:name=".LearnCircle"
android:label="Copy Up!">
</activity>
<activity
android:name=".LearnHoriz"
android:label="Copy Up!">
</activity>
<activity
android:name=".LearnVert"
android:label="Copy Up!">
</activity>
<activity
android:name=".LearnMenu"
android:label="Copy Up!">
</activity>
</application>
</manifest>
and my code:
package com.example.copyup;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button start, rules, hs, learn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linktoxml();
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, Game.class);
MainActivity.this.startActivity(myIntent);
}
});
/*
rules.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, Rules.class);
MainActivity.this.startActivity(myIntent);
}
});
hs.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, Scores.class);
MainActivity.this.startActivity(myIntent);
}
});
learn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, LearnMenu.class);
MainActivity.this.startActivity(myIntent);
}
});*/
//show shape, take reading after 4 seconds, compare with other, if true next, if false, end game
//dont make it complex with time reduce yet!!
}
private void linktoxml() {
start = (Button) findViewById(R.id.bcstart);
rules = (Button) findViewById(R.id.brules);
hs = (Button) findViewById(R.id.bhs);
learn = (Button) findViewById(R.id.blearn);
}
}
I have tried cleaning the project, re-writing the manifest and everything i can possible think of. It works if i comment out the links to the buttons in the code and leave them in the manifest but the moment i uncommnet them i get these errors, please help!!!
The problem is that the activity throw exception.
eclipse looks for the source code in android SDK and can't find it.
Just debug your code and find out where the code throw exception.
solved it, feeling pretty stupid now, needed bstart not bcstart when the button is defined.
Bear with me, this is my first Android project.
I've been making a simple Java class, Event, and made the CRUD functions for it. Now I'm doing the layout and can't figure out how I launch these activities after a button is clicked. This is the code I'm working with.
activity_main.xml;
<Button
android:id="#+id/newEventButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="New Event" />
MainActivity.java;
public void addEvent(MenuItem item) {
Intent i = new Intent(this, AddEventActivity.class);
startActivity(i);
}
AndroidManifest.xml;
<activity
android:name="com.example.eventmanager.AddEventActivity"
android:label="#string/title_activity_add_event"
android:parentActivityName="com.example.eventmanager.MainActivity"
android:noHistory="true">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.eventmanager.MainActivity" />
</activity>
What do I need to include into these segments that will link the newEventButton to launch AddEventActivity.class?
In your main activity in onCreate after setContentView add the following code
// get newEventButton
Button addEvent = (Button) findViewById(R.id.newEventButton);
// Listen for button click and start AddEventActivity
addEvent.setOnClickListener(new onClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(this, AddEventActivity.class);
}
});
You need to add an OnClickListener to the Button in your Activity's Java code. Normally you do this in onCreate().
public class MainActivity extends Activity implements OnClickListener {
public void onCreate(Bundle saved) {
super.onCreate(saved);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.newEventButton);
button.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.newEventButton:
Intent i = new Intent(this, AddEventActivity.class);
startActivity(i);
break;
}
}
}
<activity
android:name="com.example.eventmanager.MainActivity"
android:label="#string/title_activity_add_event" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.eventmanager.AddEventActivity"
android:label="#string/title_activity_add_event"
<intent-filter>
<action android:name="android.intent.action.ADDEVENTACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Here u can work like this also
EDIT
Dont you set your onclick listener if no copy this in oncreate
Button b = (Button) findViewById(R.id.newEventButton);
b.setOnClickListener(new onClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent("android.intent.action.ADDEVENTACTIVITY");
startActivity(i);
}
});
}