I am writing a simple about android app that posts a status on your Facebook wall, and I am getting a crash caused by setContentView. I set up the Facebook API correctly I believe, but I am still getting a crash. Below is all of the code.
activity_main.java:
Java:
package com.rakesh.itijgm;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
public class MainActivity extends Activity {
private static String APP_ID = "443967745743818";
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); [COLOR="Red"][B]//THE APP CRASHES HERE[/B][/COLOR]
Button btn_fb_login = (Button) findViewById(R.id.btn_fb_login);
facebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);
btn_fb_login.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View view) {
loginToFacebook();
}
});
}
//fb_login
public void loginToFacebook(){
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null){
facebook.setAccessToken(access_token);
}
if (expires != 0){
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()){
facebook.authorize(this,
new String[]{"email", "publish_stream"},
new Facebook.DialogListener() {
[user=439709]@override[/user]
public void onComplete(Bundle values) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires",facebook.getAccessExpires());
editor.commit();
}
[user=439709]@override[/user]
public void onFacebookError(FacebookError fberror) {
}
[user=439709]@override[/user]
public void onError(DialogError error) {
}
[user=439709]@override[/user]
public void onCancel() {
}
});
}}
[user=439709]@override[/user]
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;
}
[user=439709]@override[/user]
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I also have my Stack trace:
PHP:
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.facebook.widget.LoginButton" on path: DexPathList[[zip file "/data/app/com.rakesh.itijgm-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.rakesh.itijgm-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.view.LayoutInflater.createView(LayoutInflater.java:559)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
at android.view.LayoutInflater.inflate(Native Method)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)
at android.app.Activity.setContentView(Activity.java:1936)
at com.rakesh.itijgm.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5313)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2276)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
I made sure that the findViewById is referred to the correct element, and that is good, so I can't decipher the problem here.
PHP:
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.facebook.widget.LoginButton" on path: DexPathList[[zip file "/data/app/com.rakesh.itijgm-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.rakesh.itijgm-1, /vendor/lib, /system/lib]]
You are reading the 1st line of the exception right ? That tells you everything you probably need to ? This comment is not meaning to be sarcastic, just it's pretty hard to miss what it's saying lol
Anyways in short you are missing com.facebook.widget.LoginButton from whatever lib I assume you are using it from ... facebook maybe
deanwray said:
PHP:
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.facebook.widget.LoginButton" on path: DexPathList[[zip file "/data/app/com.rakesh.itijgm-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.rakesh.itijgm-1, /vendor/lib, /system/lib]]
You are reading the 1st line of the exception right ? That tells you everything you probably need to ? This comment is not meaning to be sarcastic, just it's pretty hard to miss what it's saying lol
Anyways in short you are missing com.facebook.widget.LoginButton from whatever lib I assume you are using it from ... facebook maybe
Click to expand...
Click to collapse
*facepalm* Thanks, do you know how I can integrate the facebook library properly, I followed the instructions on developers.facebook,com, but apparently, they don't work.
rakeshdas said:
*facepalm* Thanks, do you know how I can integrate the facebook library properly, I followed the instructions on developers.facebook,com, but apparently, they don't work.
Click to expand...
Click to collapse
well depends on build system and ide you're using, could be that the ide has reference but the build system does not... and I would not touch facebook with...well...any appendage or item!
deanwray said:
well depends on build system and ide you're using, could be that the ide has reference but the build system does not... and I would not touch facebook with...well...any appendage or item!
Click to expand...
Click to collapse
Well I am using Android Studio and well FB is using Eclipse in the tutorial, that is an eye sore, should I try switch over to eclipse to see if that fixes the problem?
rakeshdas said:
Well I am using Android Studio and well FB is using Eclipse in the tutorial, that is an eye sore, should I try switch over to eclipse to see if that fixes the problem?
Click to expand...
Click to collapse
well you need to work out why and what is not finding it... just do it logically as if for any possible missing component, and make sure compile order is correct and that gradle knows what it needs... like I say I have little experience with fb, but libs and modules are quite simple as a rule
deanwray said:
well you need to work out why and what is not finding it... just do it logically as if for any possible missing component, and make sure compile order is correct and that gradle knows what it needs... like I say I have little experience with fb, but libs and modules are quite simple as a rule
Click to expand...
Click to collapse
Do know of anyone who has a tutorial on integrating the FB API using Android Studio?
rakeshdas said:
Do know of anyone who has a tutorial on integrating the FB API using Android Studio?
Click to expand...
Click to collapse
Just download the FB SDK and unzip it. Then do a import module (File > Import module) in Android Studio and point to the unzipped sdk folder and it should work.
Related
Hey all,
Finally I have started learning android app and now I need to make a Preference screen.I have made an xml called info.xml in res/layout and have also added codes to it.Now I created a new class called Info and the code contains:
Code:
package com.test.info;
import android.os.Bundle;
//import android.app.Activity;
import android.view.Menu;
import android.preference.PreferenceActivity;
public class Info extends PreferenceActivity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.info);
addPreferencesFromResource(R.layout.info);
}
[user=439709]@override[/user]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.info, menu);
return true;
}
}
But I get
Code:
The method addPreferencesFromResource(int) from the type PreferenceActivity is deprecated
I know the solution is pretty easy but I cannot find the solution .Hope anybody helps me soon...
My app has min sdk 4.0.3,max sdk 4.2 and target sdk 4.2.Thanks.
Regards,
Vijai
EDIT:As I said,Its indeed easy fix and I solved it .Requested to lock the thread
Closed on OP's request
I have a viewpager with four webview fragments. Im trying to get a GoBack Action to my WebView which is inside of a fragment. But i cant implement it right into my code here is the code of my webview in my fragments. I also can't seem to get a downloadListener to work either. Sorry I am kinda new to fragments and trying to understand how they work.
Code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
public class Fragment1 extends Fragment {
private static final String WebSettings = null;
private WebView webView;
private Bundle webViewBundle;
[user=439709]@override[/user]
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.fragment_1,
container, false);
webView = (WebView) ll.findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setLoadWithOverviewMode(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setRenderPriority(RenderPriority.HIGH);
if (webViewBundle == null) {
try{
webView.loadUrl("http://www.google.com");
}catch (Exception e){
e.printStackTrace();}
} else {
webView.restoreState(webViewBundle);
}
return ll;
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
[user=439709]@override[/user]
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
webView.saveState(webViewBundle);
}
}
dfuse06 said:
I have a viewpager with four webview fragments. Im trying to get a GoBack Action to my WebView which is inside of a fragment. But i cant implement it right into my code here is the code of my webview in my fragments. I also can't seem to get a downloadListener to work either. Sorry I am kinda new to fragments and trying to understand how they work.
Code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
public class Fragment1 extends Fragment {
private static final String WebSettings = null;
private WebView webView;
private Bundle webViewBundle;
[user=439709]@override[/user]
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.fragment_1,
container, false);
webView = (WebView) ll.findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setLoadWithOverviewMode(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setRenderPriority(RenderPriority.HIGH);
if (webViewBundle == null) {
try{
webView.loadUrl("http://www.google.com");
}catch (Exception e){
e.printStackTrace();}
} else {
webView.restoreState(webViewBundle);
}
return ll;
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
[user=439709]@override[/user]
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
webView.saveState(webViewBundle);
}
}
Click to expand...
Click to collapse
You will need to override the onKeyUp method in the Activity. Then search for the fragment there and call a method of your subclass of Fragment which you want to be executed when the back key is pressed.
nikwen said:
You will need to override the onKeyUp method in the Activity. Then search for the fragment there and call a method of your subclass of Fragment which you want to be executed when the back key is pressed.
Click to expand...
Click to collapse
So I can't actually do this inside the fragment?
Sent from my SCH-I535 using Tapatalk 2
dfuse06 said:
So I can't actually do this inside the fragment?
Sent from my SCH-I535 using Tapatalk 2
Click to expand...
Click to collapse
I do not think so.
nikwen said:
I do not think so.
Click to expand...
Click to collapse
That must be all my errors then.
Sent from my SCH-I535 using Tapatalk 2
nikwen said:
You will need to override the onKeyUp method in the Activity. Then search for the fragment there and call a method of your subclass of Fragment which you want to be executed when the back key is pressed.
Click to expand...
Click to collapse
could you post a example on how it should look. This is driving me nuts nothing is working
dfuse06 said:
could you post a example on how it should look. This is driving me nuts nothing is working
Click to expand...
Click to collapse
Google!!!
Check those:
http://stackoverflow.com/questions/12210906/fragment-activity-catch-onkeydown-and-use-in-fragment
http://stackoverflow.com/questions/10631425/android-webview-inside-fragment-how-to-add-goback
nikwen said:
Google!!!
Check those:
http://stackoverflow.com/questions/12210906/fragment-activity-catch-onkeydown-and-use-in-fragment
http://stackoverflow.com/questions/10631425/android-webview-inside-fragment-how-to-add-goback
Click to expand...
Click to collapse
lol!!! I have googled it. Looked on stackoverflow posted on stack
http://stackoverflow.com/questions/16379973/goback-and-download-listener-in-webview-fragment
tried all kinda codes I found from there and no luck!
dfuse06 said:
lol!!! I have googled it. Looked on stackoverflow posted on stack
http://stackoverflow.com/questions/16379973/goback-and-download-listener-in-webview-fragment
tried all kinda codes I found from there and no luck!
Click to expand...
Click to collapse
Post your code (or the important parts of it) so that we can find your errors.
Having the same issue
dfuse06 said:
lol!!! I have googled it. Looked on stackoverflow posted on stack
tried all kinda codes I found from there and no luck!
Click to expand...
Click to collapse
I'm having the same issue. Did you find resolution? If so, please post it.
Hi there,
I would like some advice on what I should look for if I want to implement the following basic scenario:
the app starts with an activity (call it Activity_1) which contains a button
on clicking the button, takes me to a different activity (call it Activity_2)
next time i start the app, it takes me directly to Activity_2
and Activity_1 won't be seen again
So how can I change the startup activity?
What's happening is that Android is not closing your app in between starts, it's only backgrounding it. One thing you could do is to override the onResume() method in your Activity_2 to call finish() if for instance you have set some variable, let's call it appPaused and it would act just like you had pressed the back button (which will also take you back to Activity_1).
Like below:
Code:
package com.test.example;
import android.app.Activity;
import android.os.Bundle;
public class Activity_2 extends Activity
{
boolean appPaused = false;
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
[user=439709]@override[/user]
public void onPause()
{
appPaused = true;
}
[user=439709]@override[/user]
public void onResume()
{
if(appPaused)
finish();
}
}
thanks for the answer. i want to consider that the app is closed, like after restarting the device. i don't want it to be able to start with Activity_1 unless, let's say, i re-install the app.
i also had an idea about having a 3rd activity as the main one defined in the manifest, which on create changes to one of the other activities. but i don't know how that would be implemented. would it work?
octobclrnts said:
What's happening is that Android is not closing your app in between starts, it's only backgrounding it. One thing you could do is to override the onResume() method in your Activity_2 to call finish() if for instance you have set some variable, let's call it appPaused and it would act just like you had pressed the back button (which will also take you back to Activity_1).
Like below:
Code:
package com.test.example;
import android.app.Activity;
import android.os.Bundle;
public class Activity_2 extends Activity
{
boolean appPaused = false;
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
[user=439709]@override[/user]
public void onPause()
{
appPaused = true;
}
[user=439709]@override[/user]
public void onResume()
{
if(appPaused)
finish();
}
}
Click to expand...
Click to collapse
Oh, I'm sorry, I misunderstood your question the first time. If you don't want an activity to be shown except for say the first time after an install (maybe it's a login screen or similar), I would in my main activity, use the SharedPreferences class to see if I have a preference set like below:
Code:
package com.test.example;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
public class Activity_Start extends Activity
{
boolean appPaused = false;
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.contains("loggedIn"))
{
//run activity only if never run before
startActivity(new Intent(this,Activity_Logon.class));
}
else
{
//do something else every other time
}
}
}
Code:
package com.test.example;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
public class Activity_Logon extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
doLogon();
finish();
}
private void doLogon()
{
//logon code
SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
edit.putBoolean("loggedIn",true);
}
}
oh, ok. so from what I get SharedPreferences is a db where you can quickly save variables between sessions. great, I'll try it
octobclrnts said:
Oh, I'm sorry, I misunderstood your question the first time. If you don't want an activity to be shown except for say the first time after an install (maybe it's a login screen or similar), I would in my main activity, use the SharedPreferences class to see if I have a preference set like below:
Code:
package com.test.example;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
public class Activity_Start extends Activity
{
boolean appPaused = false;
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.contains("loggedIn"))
{
//run activity only if never run before
startActivity(new Intent(this,Activity_Logon.class));
}
else
{
//do something else every other time
}
}
}
Code:
package com.test.example;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
public class Activity_Logon extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
doLogon();
finish();
}
private void doLogon()
{
//logon code
SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
edit.putBoolean("loggedIn",true);
}
}
Click to expand...
Click to collapse
K-RAD said:
oh, ok. so from what I get SharedPreferences is a db where you can quickly save variables between sessions. great, I'll try it
Click to expand...
Click to collapse
That's absolutely right. You can store Strings and primitive values indexed by String keys.
octobclrnts said:
That's absolutely right. You can store Strings and primitive values indexed by String keys.
Click to expand...
Click to collapse
in the second activity, doLogon() needed an edit.apply() at the end. it works somehow, but the problem is when i press the back button, it still takes me to the first activity, and i don'twant to see it anymore after i press the button. here's my code
Code:
package com.example.onetimeactivity;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.contains("loggedIn"))
{
//run activity only if never run before
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
[user=439709]@override[/user]
public void onClick(View arg0) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
else
{
//do something else every other time
startActivity(new Intent(this,SecondActivity.class));
}
}
}
Code:
package com.example.onetimeactivity;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
public class SecondActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
doLogon();
}
private void doLogon()
{
//logon code
SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
edit.putBoolean("loggedIn",true);
edit.apply();
}
}
i managed to resolve it by adding android:noHistory="true" to the manifest. however, now i really can't see the first activity, not even if i rebuild the code ))
K-RAD said:
in the second activity, doLogon() needed an edit.apply() at the end. it works somehow, but the problem is when i press the back button, it still takes me to the first activity, and i don'twant to see it anymore after i press the button. here's my code
Click to expand...
Click to collapse
K-RAD said:
in the second activity, doLogon() needed an edit.apply() at the end. it works somehow, but the problem is when i press the back button, it still takes me to the first activity, and i don'twant to see it anymore after i press the button. here's my code
Click to expand...
Click to collapse
Thanks for catching my mistake. You're right that it does need an apply (or before API level 9, commit()) call.
If you don't want to see the activity MainActivity anymore, then you have to make some changes to your structure. You should make an Activity that will be the one that always shows when the user opens the app. That activity is the one who should check the SharedPreferences for your entry. If the entry does not exist, it should call the one-time-activity. Then the one-time-activity should do it's work and save the entry and call finish() to go back to the main activity (the one you usually want to be shown). This way the activity stack will be correct for the back key functionality.
what it sounds like you want is something like a splash/logon screen
like previously said use sharedprefs.
in the splash/logon call the sharedpref and make the default false (I use booleans, you can use ints or strings):
so it would be like this
Code:
SharedPreferences settings;
SharedPreferences.Editor editor;
boolean splash;
[user=439709]@override[/user]
public void onCreate() {
settings = getSharedPreferences("settings", 0);
splash = settings.getboolean("splash", false);
editor = settings.edit();
if(splash == false){
setContentView(//the view you are using);
//do everything else you need to do
button.setonclicklistener(new OnClickListener(){
editor.putboolean("splash", true);
editor.commit();
finish();
});
}else{
//open up your second activity
finish();
}
if its a login screen, when the user clicks logout you would do the editor and change "splash" to false.
Basically I created a button on my app which links it to another java class. I already setted the layout for the java class using the setContentView method.
But whenenever I click on the button, it doesn't take me to that java class. Im still practising right now but ive gone through the whole project nd couldn't find whats going wrong. I literally cant find any errors.
This is what I did to link the button:
Code:
package com.tarikh.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class splash extends Activity{
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button tut1 = (Button) findViewById(R.id.button1);
tut1.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.tarikh.thebasics.TUTORIALONE"));
}
});
}
as you can see, the tutorialone class has the following layout:
Code:
public class TUTORIALONE extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
and the tutorial1.xml layout consists of a radiobutton (this is just rough). so whenever I click on the button, it should take me to the tutorialone class right?
Try:
Code:
Intent intent = new Intent(splash.this, TUTORIALONE.class);
startActivity(intent);
TwilightLoz said:
Basically I created a button on my app which links it to another java class. I already setted the layout for the java class using the setContentView method.
But whenenever I click on the button, it doesn't take me to that java class. Im still practising right now but ive gone through the whole project nd couldn't find whats going wrong. I literally cant find any errors.
This is what I did to link the button:
Code:
package com.tarikh.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class splash extends Activity{
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button tut1 = (Button) findViewById(R.id.button1);
tut1.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.tarikh.thebasics.TUTORIALONE"));
}
});
}
as you can see, the tutorialone class has the following layout:
Code:
public class TUTORIALONE extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
and the tutorial1.xml layout consists of a radiobutton (this is just rough). so whenever I click on the button, it should take me to the tutorialone class right?
Click to expand...
Click to collapse
zalez said:
Try:
Code:
Intent intent = new Intent(splash.this, TUTORIALONE.class);
startActivity(intent);
Click to expand...
Click to collapse
Nope, that doesn't work. I don't know why but I have a feeling its something got to do with my emulator. I think its not set up right..
Is there any output from the logcat?
zalez said:
Is there any output from the logcat?
Click to expand...
Click to collapse
Nope, that's the weird thing. I think there is something wrong with the emulator since it does lag alot when I run it and also I cant use the home button, back button and the search button on the emulator...
I would start by deleting your emulator and recreating it. Then if that doesn't work, I would backup any code you want to keep and start deleting your ide environment and android sdk and just start over.
Ive actually done that yesterday since something went wrong with the saving of my project so my project was deleted. Today I started over. Guess im going to have to start from fresh. Oh by the way, can you have a look over here and answer my question:
http://forum.xda-developers.com/showthread.php?t=2343814
actually I just checked the logcat and I do infact have 1 error. I attached the screenshot of it:
TwilightLoz said:
actually I just checked the logcat and I do infact have 1 error. I attached the screenshot of it:
Click to expand...
Click to collapse
This should not be an error of your app.
nikwen said:
This should not be an error of your app.
Click to expand...
Click to collapse
You mean that the error has nothing to do with my app? But now it wont let me run it since im getting that error!! I actually deleted EVERYTHING again and now im going to start fresh again (at least its good practise).
Could you give me the links to get the eclipese and the androis sdk/adt? Since ive downloaded all of them 3 times...
TwilightLoz said:
You mean that the error has nothing to do with my app? But now it wont let me run it since im getting that error!! I actually deleted EVERYTHING again and now im going to start fresh again (at least its good practise).
Could you give me the links to get the eclipese and the androis sdk/adt? Since ive downloaded all of them 3 times...
Click to expand...
Click to collapse
Of course, here you go: http://developer.android.com/sdk/installing/installing-adt.html
You say that it does not let you run the app. Can you install it? I guess that you cannot. Even if you could, there would be a bigger log output/stacktrace.
There should be errors shown in your IDE (Eclipse). Is there any red sign in your project code? (If there is one, there will be a red X in the Package Explorer view.)
I think that the problem is in your code.
Check this.
nikwen said:
Of course, here you go: http://developer.android.com/sdk/installing/installing-adt.html
You say that it does not let you run the app. Can you install it? I guess that you cannot. Even if you could, there would be a bigger log output/stacktrace.
There should be errors shown in your IDE (Eclipse). Is there any red sign in your project code? (If there is one, there will be a red X in the Package Explorer view.)
I think that the problem is in your code.
Check this.
Click to expand...
Click to collapse
I just deleted everything apart from my project before reading your reply...
I'll download it again and run my project again. I didnt get any red x by the way.
TwilightLoz said:
Basically I created a button on my app which links it to another java class. I already setted the layout for the java class using the setContentView method.
But whenenever I click on the button, it doesn't take me to that java class. Im still practising right now but ive gone through the whole project nd couldn't find whats going wrong. I literally cant find any errors.
This is what I did to link the button:
Code:
package com.tarikh.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class splash extends Activity{
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button tut1 = (Button) findViewById(R.id.button1);
tut1.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.tarikh.thebasics.TUTORIALONE"));
}
});
}
as you can see, the tutorialone class has the following layout:
Code:
public class TUTORIALONE extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
and the tutorial1.xml layout consists of a radiobutton (this is just rough). so whenever I click on the button, it should take me to the tutorialone class right?
Click to expand...
Click to collapse
First of all, I would make the Button variables as a "Field" Rather than a 1-time instance of your onCreate() method. A Field is a variable defined at the root of your Class. Example:
Code:
package com.tarikh.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class splash extends Activity{
[COLOR="Lime"]Button tut1;[/COLOR]
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
[COLOR="lime"]tut1 [/COLOR]= (Button) findViewById(R.id.button1);
tut1.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
[COLOR="lime"] if (v.getId() == R.id.button1) {
startActivity(new Intent("com.tarikh.thebasics.TUTORIALONE"));
}[/COLOR]
}
});
}
The Lime-Green colored text is the only thing that needs to be change.
The 1st is now a Field, where that variable will be seen throughout the whole class, not just onCreate().
Then take away the Button declaration of tut1 in your onCreate() Method. this will make the tut1 Field (Button) = (your_id_here)
Edit: sorry, I misread. Disregard the "if " statement.
Cheers
1. Try it on a device if you can. I never use AVDs because I can run and debug an app about 8 times before my AVD even loads. Plus yours seems buggy.
2. Does it force close or just not work? if it FCs make sure you declared the second activity in your manifest.
3. Does the main xml button have an id?-- android:id="@+id/button1" ?
EDIT. Wait, your button should be in the activity_main.xml, not tutorialone. if its in tutorial one it weould try to reload that same activity.
actually it wouldnt even do anything probably, because your calling a button that isnt where you are.
Thanks for all your suggestions. I think I'm going to start fresh since Ive got more knowledge now. I'll take all your advices on board and if things go wrong, i'll post my problem here.
nikwen said:
Of course, here you go: http://developer.android.com/sdk/installing/installing-adt.html
You say that it does not let you run the app. Can you install it? I guess that you cannot. Even if you could, there would be a bigger log output/stacktrace.
There should be errors shown in your IDE (Eclipse). Is there any red sign in your project code? (If there is one, there will be a red X in the Package Explorer view.)
I think that the problem is in your code.
Check this.
Click to expand...
Click to collapse
Im going to download this as this has everything bundled together:
http://developer.android.com/sdk/index.html
Thanks guys for your co-operation, I just created the splash page and linked the button to the other layout. Got error but fixed them. It was good practice.
Didn't know running the app from your device instead of the emulator would be soo easy!! And not to mention less laggy.
TwilightLoz said:
Thanks guys for your co-operation, I just created the splash page and linked the button to the other layout. Got error but fixed them. It was good practice.
Didn't know running the app from your device instead of the emulator would be soo easy!! And not to mention less laggy.
Click to expand...
Click to collapse
Great.
Hey guys, im on the process of building my app. Basically, so far, this is what should happen:
The user presses a button and a dialogbox appears. In the dialogbox, the user selects the days in which the reminder shall remind him. (If that makes sense...).
This is my Java code:
Code:
package yCM.medireminder;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ADD extends Activity implements OnClickListener{
private AlertDialog.Builder dialogBuilder;
private void days()
{
//Declaring the Variables
final ArrayList weekdays = new ArrayList();
dialogBuilder = new AlertDialog.Builder(this);
final String[] dayss = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
//Starting the Process
dialogBuilder.setTitle("Check the Days");
dialogBuilder.setMessage("Check the days when you will shall be reminded");
dialogBuilder.setMultiChoiceItems(dayss, null, new DialogInterface.OnMultiChoiceClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
weekdays.add(which);
}else if(weekdays.contains(which))
{
weekdays.remove(Integer.valueOf(which));
}
}
});
dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Your days have been set", Toast.LENGTH_LONG);
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Your days have not been set", Toast.LENGTH_LONG);
}
});
// OUTPUT
AlertDialog dayDialog = dialogBuilder.create();
dayDialog.show();
}
Does everything look alright, the emulator isn't working for my and I have misplaced my phone (its on silent) so I have no way of checking whether it will work. I have linked the dialog box to a button so Im sure that when the button is clicked, the dialog box should appear.
It is recommended to use DialogFragments instead of simple dialogs. That way the system would orientation changes etc. for you.
However, you would need to convert your Activity into a FragmentActivity.
nikwen said:
It is recommended to use DialogFragments instead of simple dialogs. That way the system would orientation changes etc. for you.
However, you would need to convert your Activity into a FragmentActivity.
Click to expand...
Click to collapse
I do plan on making the app on permenantly on portrait mode so I don't think the orientation would be any problem even when the user switches to landscape.
Why would I need to convert my activity to fragmentactivity? What does this do? Also my min sdk is set to api level 8 (2.2 i believe).
Other than that, does the code seem alright?
TwilightLoz said:
I do plan on making the app on permenantly on portrait mode so I don't think the orientation would be any problem even when the user switches to landscape.
Why would I need to convert my activity to fragmentactivity? What does this do? Also my min sdk is set to api level 8 (2.2 i believe).
Other than that, does the code seem alright?
Click to expand...
Click to collapse
You just need to do this if you want to use the DialogFragment. For the normal dialog you don't need it.
You can use fragments on Android 1.6+ if you include the supportv4 library.
Try it. That's all I can say. There might be other things that make it crash, apart from that code. Maybe related to the Android lifecycle.
thanks for the info. Just runned it, everythings working smooth for now. Any idea on how to make the android system recognise/link the days of the week on my string? I did have a read through this but didn't quite understand it. I have a feeling that I should be using something got to do with the calender though...
http://developer.android.com/reference/java/util/Date.html#getDay()
TwilightLoz said:
thanks for the info. Just runned it, everythings working smooth for now. Any idea on how to make the android system recognise/link the days of the week on my string? I did have a read through this but didn't quite understand it. I have a feeling that I should be using something got to do with the calender though...
http://developer.android.com/reference/java/util/Date.html#getDay()
Click to expand...
Click to collapse
You can get the current time using
Code:
System.currentTimeMillis()
Then create a Date from it:
Code:
Date myDate = new Date(System.currentTimeMillis();
Then you can format it using the SimpleDateFormat class: http://developer.android.com/reference/java/text/SimpleDateFormat.html
For some things the Calendar class might be better: http://developer.android.com/reference/java/util/Calendar.html
However, I have not worked with it yet.