I got a runtime error when I press the button that should change the activity:
package com.example.LocationTracker;
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 LocationTracker extends Activity{ /** Called when the activity is first created. */
Button btn_Tracker;
Button btn_Display_Map;
Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext();
btn_Tracker = (Button)findViewById(R.id.btn_Tracker);
btn_Tracker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//setContentView(R.layout.trackeractivity);
Intent myIntent1 = new Intent(view.getContext(), TrackerActivity.class);
context.startActivity(myIntent1);
}});
}
class TrackerActivity extends Activity {
//Your member variable declaration here
// Called when the activity is first created.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trackeractivity);
}
}
I added everything right in the maniefest file
<activity android:name=".TrackerActivity" android:label="#string/app_name"/>
<activity android:name=".DisplayMapActivity" android:label="#string/app_name"/>
</application>
Any idea?
I think TrackerActivity needs to be public, which means it will need to be in its own file as well.
You shouldn't be using getApplicationContext() to start activities. Every activity is a context, so having a member instance of Context should not be necessary. Try re-writing the onClick method of your OnClickListener like this
public void onClick(View view) {
Intent myIntent1 = new Intent(LocationTracker.this, TrackerActivity.class);
LocationTracker.this.startActivity(myIntent1);
}});
Also, refer to this documentation for when to use the application context.
Related
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
I can make an Intent to open other Activity with writing the code in MainActivity.java.
Then I try to make an Intent using a class and called it in MainActivity.java. But it becomes error.
How to solve this problem?
When I write startActivity(numberIntent); in MainActivity.java there is no error but when I move this line of code to NumbersClickListener.java
Errors come:
error: cannot find symbol method startActivity(Intent)
error: not an enclosing class: MainActivity
This my code
In MainActivity.java
package com.example.android.*****;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumbersClickListener clickListener = new NumbersClickListener();
TextView numbers = (TextView)findViewById(R.id.numbers);
numbers.setOnClickListener(clickListener);
}
in NumbersClickListener.java
package com.example.android.*****;
import android.content.Intent;
import android.view.View;
android.widget.Toast first
import android.widget.Toast;
OnClickListener should be written in capital letter
public class NumbersClickListener implements View.OnClickListener {
#Override
public void onClick(View view) {//.makeText(view.getContext(),
"open the list of numbers", Toast.LENGTH_SHORT).show();
Intent numberIntent = new Intent(MainActivity.this,
NumbersActivity.class);
startActivity(numberIntent);
}
}
error: cannot find symbol method startActivity(Intent)” in a class of Listener?
Because if startActivity(Intent) is a method of activity and its required call from context
If you want call startActivity(Intent) outside activity you need to use
Context.startActivity(numberIntent);
Use this
view.getContext().startActivity(numberIntent);
instead of this
startActivity(numberIntent);
SAMPLE CODE
public class NumbersClickListener implements View.OnClickListener {
#Override
public void onClick(View view) {
Intent numberIntent = new Intent(view.getContext(),
NumbersActivity.class);
view.getContext().startActivity(numberIntent);
}
}
You are defining NumbersClickListener in a separate java file. It has no way compiler will know that when u call startActivity you are referring to the Activity.startActivity
Unless you have a deeper purpose for NumbersClickListener.java, simply do inline declaration of View.Listener will do
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumbersClickListener clickListener = new NumbersClickListener();
TextView numbers = (TextView)findViewById(R.id.numbers);
numbers.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent numberIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(numberIntent);
}
});
}
In place MainActivity.this, use its context.
Intent numberIntent = new Intent(context, NumbersActivity.class);
startActivity(numberIntent);
Notice the changes I have made
MainActivity.java
package com.example.android.*****;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumbersClickListener clickListener = new NumbersClickListener(MainActivity.this); // Context while creating ClickListener Object
TextView numbers = (TextView)findViewById(R.id.numbers);
numbers.setOnClickListener(clickListener);
}
NumbersClickListener.java
package com.example.android.*****;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;
public class NumbersClickListener implements View.OnClickListener {
Context context;
NumbersClickListener(Context c){
this.context = c;
}
#Override
public void onClick(View view) {
Intent numberIntent = new Intent(context, NumbersActivity.class);
startActivity(numberIntent);
}
}
To startActivity you need Context.
It will be like this context.startActivity()
In MainActivity it is not giving error because Activity internally extends Context.
NumbersClickListener is not extended Context.
So, you can start activity using View context
Replace startActivity(numberIntent) with
view.getContext().startActivity(numberIntent);
inside your NumberClickListener class you can do the following
Context context = view.getContext();
Intent numberIntent = new Intent (context, NumberActivity.class);
context.startActivity(numberIntent);
By using this code you can use your NumberClickListener with any other activity.
happy codding :)
I built a web app, and I am making a wrapper to put it on the app store. I have a main page, or start page for the app, and the submit button successfully loads my webview page, and the app works well.
I wanted to add a small webview above the submit button where I could display updated news before users enter the app, such as new terms of use.
I followed examples to get the webview to load on the main page, and it works - the content is displayed, but when I add the webview, the submit button doesn't work any longer.
Eclipse throws an error "Duplicate Method OnCreate(Bundle) in type TOS, so I tried changing OnCreate to OnStart or OnCreateView for the button. It renders from the layout xml, but there is no functionality. I'm certain it is a novice syntax error, so I've just posted the Tos.java code:
package com.packagename.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Tos extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tos);
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Mapp.class);
startActivity(intent);
}
});
}
private WebView TermswebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tos);
WebView TermswebView = (WebView) findViewById(R.id.webView);
TermswebView.loadUrl(someURL");
};
};
So you need only one onCreate method for each Activity not object, which is where you're thinking(I assume) that you need multiple onCreate methods.
So.. just merge the two you have...
public class Tos extends Activity {
//UI ELEMENTS
private Button button;
private WebView TermswebView;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tos);
TermswebView = (WebView) findViewById(R.id.webView);
TermswebView.loadUrl("http://www.barglance.com/assets/tos/tos.php");
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Mapp.class);
startActivity(intent);
}
});
}
I hope this helps. For more information about activities and what onCreate actually does and when it's called, refer to Android Docs - Activity
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?
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.