Could anyone please help me with the below java code as I want to create a image slideshow without a click of a button. I want the view flipper to automatically switch through the different iamges without a click of a button. I want it to keep on showing all the images again and again, without a click of a button. I have deleted the button in my XML file as I dont require it.
Java File Code
public class MainActivity extends Activity {
int mFlipping = 0 ; // Initially flipping is off
Button mButton ; // Reference to button available in the layout to start and stop the flipper
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Click event handler for button */
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.flipper1);
if(mFlipping==0){
/** Start Flipping */
flipper.startFlipping();
mFlipping=1;
mButton.setText(R.string.str_btn_stop);
}
else{
/** Stop Flipping */
flipper.stopFlipping();
mFlipping=0;
mButton.setText(R.string.str_btn_start);
}
}
};
/** Getting a reference to the button available in the resource */
mButton = (Button) findViewById(R.id.btn);
/** Setting click event listner for the button */
mButton.setOnClickListener(listener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
XML File
<ViewFlipper
android:id="#+id/flipper1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:flipInterval="3000"
android:inAnimation="#android:anim/slide_in_left"
android:outAnimation="#android:anim/slide_out_right"
android:layout_centerInParent="true"
>
<ImageView
android:src="#drawable/img1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/str_img1"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:src="#drawable/img2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/str_img2"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:src="#drawable/img3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/str_img3"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:src="#drawable/img4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/str_img4"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:src="#drawable/img5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/str_img5"
android:layout_gravity="center_horizontal"
/>
</ViewFlipper>
</RelativeLayout>
For(int mflipping:0;mflipping less than total images-1 length; mflipping++)
Curly brases starts
Flipper.startflipping();
//Delay for sometime
Curly Brases End
Flipper.stopflipping();
Sodu code,write your code now
Just remove button listener and try.
public class MainActivity extends Activity {
int mFlipping = 0 ; // Initially flipping is off
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.flipper1);
if(mFlipping==0){
/** Start Flipping */
flipper.startFlipping();
mFlipping=1;
}
else{
/** Stop Flipping */
flipper.stopFlipping();
mFlipping=0;
}
}
};
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Related
I am using a progress bar sample from Github however I noticed that the progress bar fill is set to a fixed value. For example, if the step count goal is 10 (.java class) the man should be "10" in the .XML
My goal: when the user inputs their goal step count, the "10" should be variable and change depending on user input.
java snippet:
ProgressBar progressBar = (ProgressBar) this.findViewById(R.id.progressBar);
ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", lastStep, stepCounter); //animate only from last known step to current step count
animation.setDuration(5000); // in milliseconds
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
lastStep = stepCounter;
XML snippet
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="376dp"
android:layout_height="392dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:max="10"
android:progress="0"
android:progressDrawable="#drawable/circular" />
EDIT********************EDIT
Sorry maybe my question wasn't clear. To provide an example...if the users step goal is 500. I need the progress bar to fill respectively. Therefore, IF step_count = 250 then progress bar should be half full, IF step_count = 750 then should be 3/4 full. I need the progression to be respective to a variable value.
Set android:indeterminate="false". See this fully implemented code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ProgressBar
android:id="#+id/pBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="200dp"
android:minHeight="50dp"
android:minWidth="200dp"
android:max="100"
android:indeterminate="false"
android:progress="0" />
<TextView
android:id="#+id/tView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/pBar"
android:layout_below="#+id/pBar" />
<Button
android:id="#+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp"
android:layout_marginTop="20dp"
android:text="Start Progress"
android:layout_below="#+id/tView"/>
public class MainActivity extends AppCompatActivity {
private ProgressBar pgsBar;
private int i = 0;
private TextView txtView;
private Handler hdlr = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pgsBar = (ProgressBar) findViewById(R.id.pBar);
txtView = (TextView) findViewById(R.id.tView);
Button btn = (Button)findViewById(R.id.btnShow);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i = pgsBar.getProgress();
new Thread(new Runnable() {
public void run() {
while (i < 100) {
i += 1;
// Update the progress bar and display the current value in text view
hdlr.post(new Runnable() {
public void run() {
pgsBar.setProgress(i);
txtView.setText(i+"/"+pgsBar.getMax());
}
});
try {
// Sleep for 100 milliseconds to show the progress slowly.
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});
}
}
after adding 2 new features (swipelayout + animation) my ui freezes.
I have a recyclerview which includes movie objects. If i add/remove/modify a movie i call my moviemanager because there's a notify concept (all views which want to use the data have to be registered on this manager).
This all works fine before i added the new features.
I use this swipelayout:
https://github.com/daimajia/AndroidSwipeLayout
Currently the items looks like this:
After swiping:
The freeze is always reproducible if i have 4 items. I swipe on the second item and delete it. Then it will dissapear like i want. After im swiping the "new" second item there's a freeze for like 10-15 seconds.
The layout xml of the item:
<com.daimajia.swipe.SwipeLayout
android:id="#+id/item_movie_swipe_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/item_movie_swipe_bottom_wrapper"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#ff0000">
<TextView
android:id="#+id/item_movie_bottom_delete_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_delete_white_24dp"
android:text="#string/delete"
android:textColor="#fff"
android:layout_gravity="center"
android:padding="10dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<ImageView
android:id="#+id/item_movie_list_poster"
style="#style/image_border_style"
android:layout_width="68dp"
android:layout_height="100dp"
android:layout_marginRight="3dp"
android:background="#android:color/black"/>
<TextView
android:id="#+id/item_movie_list_name_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="3dp"
android:layout_toEndOf="#+id/item_movie_list_poster"
android:layout_toRightOf="#+id/item_movie_list_poster"
android:gravity="center|left"
android:padding="3dp" android:text="name"/>
<TextView
android:id="#+id/item_movie_list_genre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/item_movie_list_name_year"
android:layout_marginBottom="3dp"
android:layout_toEndOf="#+id/item_movie_list_poster"
android:layout_toRightOf="#+id/item_movie_list_poster"
android:padding="3dp"
android:text="Drama - Komödie - Action" android:textColor="#color/item_textyear"
android:textSize="12dp"/>
<TextView
android:id="#+id/item_movie_list_viewed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/item_movie_list_poster"
android:layout_toRightOf="#+id/item_movie_list_poster"
android:paddingLeft="3dp" android:text="Gesehen: "
android:textColor="#color/accept"
android:textSize="12dp"/>
</RelativeLayout>
</com.daimajia.swipe.SwipeLayout>
Relevant code of the adapter:
#Override
public void onBindViewHolder(final CDMMoviesAdapter.IC_ViewHolder p_Holder, final int p_iPosition) {
// more unimportant code...
p_Holder.m_SwipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
// Drag From Right
p_Holder.m_SwipeLayout.addDrag(SwipeLayout.DragEdge.Right, p_Holder.m_SwipeLayout.findViewById(R.id.item_movie_swipe_bottom_wrapper));
// Handling different events when swiping
p_Holder.m_SwipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
#Override
public void onClose(SwipeLayout layout) {
//when the SurfaceView totally cover the BottomView.
}
#Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
//you are swiping.
}
#Override
public void onStartOpen(SwipeLayout layout) {
}
#Override
public void onOpen(SwipeLayout layout) {
//when the BottomView totally show.
}
#Override
public void onStartClose(SwipeLayout layout) {
}
#Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
//when user's hand released.
}
});
p_Holder.m_SwipeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View p_View) {
if((((SwipeLayout) p_View).getOpenStatus() == SwipeLayout.Status.Close)) {
Intent l_Intent = new Intent();
Bundle l_Bundle = new Bundle();
l_Bundle.putLong(CDMMovieDetailsActivity.ARG_MOVIE, l_MovieID);
l_Intent.putExtras(l_Bundle);
l_Intent.setClass(l_Context, CDMMovieDetailsActivity.class);
l_Context.startActivity(l_Intent);
}
}
});
p_Holder.m_tvDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View p_View) {
Animation l_DeleteAnimation = AnimationUtils.loadAnimation(l_Context, R.anim.slide_out);
l_DeleteAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
mItemManger.removeShownLayouts(p_Holder.m_SwipeLayout);
CDMMovieManager.getInstance().removeMovieID(m_List, m_MovieIDs.get(p_iPosition));
mItemManger.closeAllItems();
}
});
p_Holder.m_View.startAnimation(l_DeleteAnimation);
}
});
// mItemManger is member in RecyclerSwipeAdapter Class
mItemManger.bindView(p_Holder.itemView, p_iPosition);
// more unimportant code...
}
/**
* This class is the viewholder. For internal use only.
*/
public static class IC_ViewHolder extends RecyclerView.ViewHolder {
/**
* Constructor
*
* #param p_View the view
*/
public IC_ViewHolder(View p_View) {
super(p_View);
m_View = p_View;
m_SwipeLayout = (SwipeLayout) p_View.findViewById(R.id.item_movie_swipe_layout);
m_tvNameYear = (TextView) p_View.findViewById(R.id.item_movie_list_name_year);
m_ivPoster = (ImageView) p_View.findViewById(R.id.item_movie_list_poster);
m_tvGenre = (TextView) p_View.findViewById(R.id.item_movie_list_genre);
m_tvViewed = (TextView) p_View.findViewById(R.id.item_movie_list_viewed);
m_tvDelete = (TextView) p_View.findViewById(R.id.item_movie_bottom_delete_desc);
}
public View m_View;
public SwipeLayout m_SwipeLayout;
public TextView m_tvNameYear;
public ImageView m_ivPoster;
public TextView m_tvGenre;
public TextView m_tvViewed;
public TextView m_tvDelete;
}
In CDMMovieManager.getInstance().removeMovieID(m_List, m_MovieIDs.get(p_iPosition)); the movie will be removed and all registered views will be notified. In this case the fragment of this adapter will call m_Adapter.notifyDataSetChanged();
The animation file:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="250"
android:fromXDelta="0%"
android:toXDelta="-100%"/>
</set>
I tried things like View.clearAnimation() or Animation.setAnimationListener(null) but i always get this problem.
Plase ask if you need more informations.
Update:
Problem only occures if i try to swipe the next item. (For example delete 2nd item. After that try to swipe the 3rd item which is the new second item -> freeze on the item i tried to swipe). The freeze can only be removed on swiping on any other item.
I solved my problem. This answer helped me: How to animate RecyclerView items when they appear
I added this code to my adapter:
#Override
public void onViewDetachedFromWindow(IC_ViewHolder p_Holder) {
p_Holder.m_View.clearAnimation();
}
Hi I am new to Android development and StackOverflow.
I have an Android WebView application in which a 'starter activity' (just intro text and a button to start main intent) starts my WebView activity, which loads a single URL (this is in the onCreate method).
In this main activity, I have three buttons, one of which opens the same URL in the current activity (effectively refreshing the page). The other two buttons I would like to open two different URLs in the same WebView activity, and I was wondering if/how this is possible.
Here is the main ReadWebpageAsyncTask.java activity code:
public class ReadWebpageAsyncTask extends Activity {
private WebView browser;
protected String urlStrOne = "url one";
protected String urlNewCourses = "url two";
protected String urlFutureCourses = "url three";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_webpage_async_task);
browser = (WebView)findViewById(R.id.WebView01);
browser.setWebViewClient(new MyBrowser());
browser.loadUrl(urlStrOne);
}
#SuppressLint("SetJavaScriptEnabled")
public void onClick(View view){
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.loadUrl(urlStrOne);
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView browser, String urlStrOne) {
browser.loadUrl(urlStrOne);
return true;
}
}
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(browser.canGoBack()) {
browser.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
And the corresponding XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|right"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<Button
android:id="#+id/readWebpage"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/soon"
android:background="#+drawable/custom_button_one"
android:onClick="onClick"
android:text="#string/load_page" />
<Button
android:id="#+id/exitapp01"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/soon"
android:background="#+drawable/custom_button_one"
android:onClick="exitButton"
android:text="#string/exit_button" />
<Button
android:id="#+id/soon"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_toLeftOf="#+id/exitapp01"
android:layout_toRightOf="#+id/readWebpage"
android:layout_alignParentTop="true"
android:onClick="comingSoon"
android:text="#string/future_courses" />
</RelativeLayout>
<WebView
android:id="#+id/WebView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/WebView01" >
</WebView>
</LinearLayout>
P.S. Please excuse the terrible naming of everything, I wrote this a while back and haven't gotten around to changing it all.
To clarify, the main URL which is loaded first is the same as the button "readWebpage", and the other two URLs which I would like to be loaded on click are buttons "exitapp01" (what can I say, I am a noob) and "soon".
You can add OnClickListeners to your buttons and switch the url depending on the case:
public class ReadWebpageAsyncTask extends Activity implements OnClickListener{
private WebView browser;
protected String urlStrOne = "url one";
protected String urlNewCourses = "url two";
protected String urlFutureCourses = "url three";
Button reload, exit, soon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_webpage_async_task);
browser = (WebView)findViewById(R.id.WebView01);
browser.setWebViewClient(new MyBrowser());
browser.loadUrl(urlStrOne);
reload = (Button)findViewById(R.id.readWebpage);
exit = (Button)findViewById(R.id.exitapp01);
soon = (Button)findViewById(R.id.soon);
reload.setOnClickListener(this);
exit.setOnClickListener(this);
soon.setOnClickListener(this);
}
#SuppressLint("SetJavaScriptEnabled")
public void onClick(View view){
}
#Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.readWebpage:
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.loadUrl(urlStrOne);
break;
case R.id.exitapp01:
//Do whatever you want with this button
break;
case R.id.soon:
//Do whatever you want with this button
break;
}
}
}
And remove all the android:onClick="" from your xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|right"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<Button
android:id="#+id/readWebpage"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"/>
<Button
android:id="#+id/exitapp01"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/soon"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<WebView
android:id="#+id/WebView01"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
If The buttons are on Activity You have to set the OnClickListener on all the buttons and within the onClick() do like this:
#Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.[Your first button Id]:
{
//Functionality
}
case R.id.[Your Second button Id]:
{
//Functionality
}
case R.id.[Your first button Id]:
{
//Functionality
}
//End Switch
}
//You also need To Write implements OnClickListener at the Class Definition Of Your activity
I have just started programming in Android and I couldn't get the result on clicking button that I expected. Here s the code.
.java file
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class AndroidLove extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_love);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.android_love, menu);
return true;
}
public void onButtonClicked(View view) {
TextView textView=(TextView)findViewById(R.id.textView1);
textView.setVisibility(View.VISIBLE);
}
}
.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".AndroidLove" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="104dp"
android:text="#string/hello_world"
android:visibility="invisible" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:text="#string/Button"
android:onClick="onButtonClicked" />
strings.xml file
<resources>
<string name="app_name">AndroidLove</string>
<string name="action_settings">Settings</string>
<string name="hello_world">First Line\nSecond Line\nThird Line</string>
<string name="Button">Get Lyrics</string>
On clicking the button, the following text should be displayed but its not displayed:-
First Line
Second Line
Third Line
Please help
Edit: Maybe and probably your binding is wrong about onclick, try below code instead of top. Put that code into your onCreate method.
Button button= (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView textView=(TextView)findViewById(R.id.textView1);
textView.setVisibility(View.VISIBLE);
}
});
You need to get the string value from the strings.xml file which can be acheived by
String st =getResources.getString(R.string.hello_world);
Then set that to textview by
textView.setText(st);
Try this code:
public class AndroidLove extends Activity {
private Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_love);
textView=(TextView)findViewById(R.id.textView1);
button1 = (Button)findViewByid(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
// do your stuff here
String _data =getResources.getString(R.string.hello_world);
textView.setText(_data);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
I recently started developing apps, and I've gotten my first gig. It's a simple app with a logo and a button linking to a website. Everytime I run it, it crashes, and I'm stumped as to why, as it's such a simple program. I've spent a lot of time looking for similar problems on SO, but to no avail. I've also gone through eclipse and eliminated any compiler warnings. Does anyone have an idea as to what could have gone wrong?
Error messages:
I've kept cutting things out over and over to the app's most simple function. It still won't work. Here is my .java and main activity. (It's a one activity and one class app)
//necessary imports are omitted. I get no errors for imports
public class FullscreenActivity extends Activity {
Button button = (Button) findViewById(R.id.button1);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
goToUrl("http://switchemup.com");
}
});
}
private void goToUrl(String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fullscreen, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".FullscreenActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:src="#drawable/switchuplogo" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="94dp"
android:text="View Website" />
</RelativeLayout>
This line Button button = (Button) findViewById(R.id.button1); should create an error and this should cause the crash you app. You set a variable which contains a method that should be in onCreate method.
Try this instead:
// init your variable only
Button button;
// Then in onCreate, as you already did:
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
// set findViewById method only here
button = (Button) findViewById(R.id.button1);
// ...
}
Let me know if this helps.