Change between activities does not work - java

I'm trying to change between activities in my Android app (2.1-update1), but it doesn't work.
Any suggestions?
The only thing that happens when I debug the app is that it stops on this part of the code in Instrumentation.java:
public void waitForIdle() {
synchronized (this) {
while (!mIdle) {
try {
wait();
} catch (InterruptedException e) {
}
}
}
}
Eclipse says that it is in Thread 1 on
Instrumentation.checkStartActivityResult(int, Object) line: 1537. If I
resume the app, the next stop is in ZygoteInit.java trying to run
Throwable cause = ex.getCause(); ... Eclipse says
ZygoteInit$MethodAndArgsCaller.run() line: 864.
Here is the source code:
HappyHomes.java
package com.example.app;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HappyHomes extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button login = (Button) findViewById(R.id.btnLogin);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ProgressDialog laddRuta = ProgressDialog.show(HappyHomes.this, "",
"Loggar in, vänligen vänta...", true);
Intent myIntent = new Intent(view.getContext(), Kategorier.class);
myIntent.
startActivity(myIntent);
}
});
}
}
Kategorier.java
package com.example.app;
import android.app.Activity;
import android.os.Bundle;
public class Kategorier extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kategorier);
}
}
Thanks for helping!

Make sure that Kategorier is registered in your AndroidManifest.xml file.

Change myIntent.startActivity(myIntent); to HappyHomes.this.startActivity(myIntent);

There is no any startActivity() method in Intent class . you must be doing wrong.
just write startActivity(myIntent)

All the Services, Broadcast Receivers and Activites must be declared in manifest file.

Related

Open Another Activity in Android

I am new to Android Development and I have this problem of opening another activity from the main activity.
The problem is that whenever I click on the button, the app closes.
Below is my Java code.
package com.Notely.SplashScreenandAccounts;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ActivityWelcome extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Button signup_btn = (Button) findViewById(R.id.signup_btn);
signup_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, ActivitySignup.class);
startActivity(intent);
}
}
I need suggestions on what is the problem and on how to rectify that.
You should add your Activity(ActivitySignup.class) in your
AndroidManifest file.
Like this..
<Activity android:name = .ActivituSignup android:theme="AppTheme"/>
i hope this will help you .
If it not works you should add this line in your ActivitySignup.java at on create before super.onCreate()
setTheme(R.style.AppCompat);
send your logcat error to understanding best.
You should take a tour of Activity in
android.developers.com web

event can't run in android ( get errors)

I recently use android studio instead of eclipse.
but somthing is wrong!
these are main java code that run currectly in eclipse but now get errors!
package com.example.amir.myapplicationeeee;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(getApplicationContext(),Main2Activity.class));
finish();
}
});
setOnClickListener is hilighed red and error ";" excepted showed when want to runnin app in virtual device
If the code you provide here is exactly the same as you have in your project, then you should just add '}' on the end of your code - it closes the MainActivity class.
You should also have evertyhing implemented inside onCreate() method.
So it should look like:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new
Intent(this,Main2Activity.class));
finish();
}
});
}
}

My Junit test doesn't run

I'm new to Java, Android and JUnit. I want to learn how to write JUnit tests for an Android application. To that end, I have a very simple Android app (2 activities, 2 buttons, each button goes to the other activity). I want to test the button. This app runs fine on my phone when it's plugged in. I've been looking at the samples provided in the SDK, and I am trying to emulate them.
My problem is that when I right-click on my test project, and choose Run As -> Android JUnit test, nothing happens. I don't know why.
My test code.
package com.example.twoactivities.test;
import android.app.Instrumentation.ActivityMonitor;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest;
import android.widget.Button;
import com.example.twoactivities.MainActivity;
import com.example.twoactivities.MainActivity2;
public class ClickButton extends ActivityInstrumentationTestCase2<MainActivity> {
private Button mButton2;
private long TIMEOUT_IN_MS = 100000;
public ClickButton() {
super(MainActivity.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
final MainActivity a = getActivity();
// ensure a valid handle to the activity has been returned
assertNotNull(a);
}
#SmallTest
public void click(){
// Set up an ActivityMonitor
ActivityMonitor activityMonitor = getInstrumentation().addMonitor(MainActivity2.class.getName(), null, false);
//check if button is enabled
assertTrue("button is enabled", mButton2.isEnabled());
//click button
mButton2.performClick();
MainActivity2 MainActivity2 = (MainActivity2) activityMonitor.waitForActivityWithTimeout(TIMEOUT_IN_MS );
assertNotNull("MainActivity2 is null", MainActivity2);
// assertEquals("Monitor for MainActivity2 has not been called", 1, activityMonitor.getHits());
// assertEquals("Activity is of wrong type", MainActivity2.class, MainActivity2.getClass());
// Remove the ActivityMonitor
getInstrumentation().removeMonitor(activityMonitor);
}
// public void tearDown() {
// }
}
(I know it's really simple, but I'm just trying to get the basics down.)
My application.
package com.example.twoactivities;
import com.example.twoactivities.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void activity2(View view){
Intent intent = new Intent(this,com.example.twoactivities.MainActivity2.class);
startActivity(intent);
}
}
Activity 2 of my application.
package com.example.twoactivities;
import com.example.twoactivities.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void activity1(View view){
Intent intent = new Intent(this,com.example.twoactivities.MainActivity.class);
startActivity(intent);
}
}
Any ideas why my test class doesn't run?
Thanks,
Stephanie
You have to right click on the test itself, not the project.
According to documentation Android testing API supports JUnit 3 code style, but not JUnit 4. Try naming your test method testClick

android devolpment linking buttons to other xml pages

I'm developing an app for android. The main screen has 6 buttons. Each button leads to another screen. I'm having trouble with the code to make the buttons do anything when clicked. this is what I have:
on the main page my button id is glass the page opened when clicked is glass.xml
android:onClick="Intent i = new Intent(FirstActivity.this, SecondActivity.class);"
and my scr folder I have the java activities FirstActivity.java
package install.fineline;
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 FirstActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fine_line);
Button btnload = (Button) findViewById(R.id.glass);
btnload.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}
and SecondActivity.java
package install.fineline;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.glass);
}
}
what am i doing wrong?
On the button definition in the xml remove the onClick attribute.
The button click action is set in code so it doesn't need to be in the xml file.
There is sample code here showing how to do what you want.

Android/Java: Starting activity from class method

I'm trying to start a new activity from a non-activity class.
From the main menu:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity {
Button start, options;
GameLoop Game = new GameLoop();
#Override
public void onCreate(Bundle mainStart) {
super.onCreate(mainStart);
setContentView(R.layout.menu);
start = (Button) findViewById(R.id.bStart);
options = (Button) findViewById(R.id.bOptions);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent openStart = new Intent(Menu.this, Game.class);
startActivity(openStart);
}
});
options.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context mContext = null; //Error called for mContext to be initialized so just tried setting to null. This is most likely the error cause it would make more sense for it to be equal to "getContext()" or something like that
Game.Start(mContext);//Here
}
});
}
}
I'm trying to open an activity from the Game.Start() method.
import android.content.Context;
import android.content.Intent;
public class GameLoop extends Menu{
boolean hello = false;
public void Start(Context sContext){
Intent openOptions = new Intent(sContext, Options.class);
startActivity(openOptions);
}
}
I'm not sure if using context would be the right way of going about this but I figured it was worth a try. Im entirely new to java and android so I'm pretty much lost on where to go next. Any help in what direction to take would be throughly appreciated.
Activity extends Context, so you can just use this when inside Activity.
Game.Start(Menu.this);
I use Menu.this because you are inside inner anonymous class (View.OnClickListener) where this refers to this inner class.
Do you added the new activities to the androidmanifest.xml?

Categories