No errors in eclipse.. But whenever I start the application it FC's :(
I am trying to make a app which install scripts to init.d whenever I press the button. I think the problem is the brackets cuz they troubled me ALOT! You can see that they are arranged in an unorganised manner.
MainActivity.java
package com.example.kernel.version;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.stericson.RootTools.Command;
import com.stericson.RootTools.RootTools;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button= (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
RootTools.remount("/system/", "rw");
Command command = new Command(0, "su", "cp /sdcard/scripts/* /system/etc/init.d/")
{
#Override
public void output(int id, String line)
{
}
};
try {
RootTools.getShell(true).add(command).waitForFinish();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
});}}
Activity_main.xml
<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" >
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="141dp"
android:text="Install Scripts" />
</RelativeLayout>
I think NullPointerException on Line, button.setOnClickListener(new View.OnClickListener() {
Because You missed setContentView(R.layout.Activity_main); before accessing Button from layout Xml file.. So your Button button is NULL.
Something Like,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// You missed this code line,
setContentView(R.layout.Activity_main);
Button button= (Button) findViewById(R.id.button);
First create Button instance then add listener
Button button;
public void onCreate(Bundle savedInstanceState) {
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
}
}
Related
I have this code that changes the background when you click a button however, I want to change the background every 10 seconds, and I want to switch between files img1.png, img2.png and img3.png and when the cycle is completed start all over again. Thanks in advance.
Here is the code:
In 'MainActivity.java'
package lucas.app_2001;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button button;
LinearLayout mainLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout=(LinearLayout)findViewById(R.id.myLayout);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
mainLayout.setBackgroundResource(R.drawable.yellowgradient);
}
});
}
}
'MainActivity.xml':
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="64dp"
android:layout_marginTop="71dp"
android:text="Shout!" />
</LinearLayout>
You can do this way:
private Handler mHandler;
private Runnable mRunnable;
private int i = 0;
MainActivity.java:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button button;
LinearLayout mainLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout=(LinearLayout)findViewById(R.id.myLayout);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
mainLayout.setBackgroundResource(R.drawable.yellowgradient);
}
});
mHandler = new Handler();
mRunnable = new Runnable(){
#Override
public void run() {
i++;
if(i==1){
mainLayout.setBackgroundResource(R.drawable.image_one);
}else if(i==2){
mainLayout.setBackgroundResource(R.drawable.image_two);
}else if(i==3){
mainLayout.setBackgroundResource(R.drawable.image_three);
i ==0;
}
mHandler.postDelayed(mRunnable , 10000);
}
};
mHandler .post(mRunnable);
}
}
on onStop():
if(mHandler!=null){
mHandler.removeCallbacks(mRunnable);
}
Done
Use a while loop to repeat.
To delay it for 10 seconds try something like this:
//wait 10 seconds
Button.postDelayed(new Runnable() {
#Override
public void run() {
Button.setClickable(true);
}
}, 10000);
Android app, i wrote to test image button doesn't work. I have created a image button and implement an event listener for that button. What's wrong with this source code?
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
public class ImageButtonTestApp extends Activity {
ImageButton imageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void eventListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(ImageButtonTestApp.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
}
}
u have written setOnClickListener in different method but u didnot call that method any where call that method in oncreate.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
eventListenerOnButton();
}
try this one,
Try it
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(ImageButtonTestApp.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
Java code for map activity
package com.schweigert.drinkingbuddy;
import android.app.Activity;
import android.os.Bundle;
public class BarMaps extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.barmaps);
}
}
XML Code
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment" >
</fragment>
Java code that starts intent
maps.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Intent openMap = new Intent("com.schweigert.drinkingbuddy.BarMaps");
startActivity(openMap);
}
});
When i am running this code on my emulator i have set up on my computer it brings up the "This app wont run unless you update Google Play services. But when i run it on a physical device it just times out and says that the app has stopped working.Why am i getting such a difference in errors between my emulator and physical device?
Edit:
package com.schweigert.drinkingbuddy;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
public class StartingPoint extends Activity {
double numDrinks;
int counter;
EditText standardDrinks;
Button bacIntent, maps, drinks;
TextView display;
Bundle testBundle;
#Override
protected void onCreate(Bundle testBundle) {
super.onCreate(testBundle);
setContentView(R.layout.activity_starting_point);
counter = 0;
bacIntent = (Button) findViewById(R.id.bacIntent);
maps = (Button) findViewById(R.id.Maps);
drinks = (Button) findViewById(R.id.drinks);
bacIntent.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v){
try{
Intent openBac = new Intent("com.schweigert.drinkingbuddy.BloodAlcoholContent");
startActivity(openBac);
}
catch(Exception e)
{
display.setText("my d" + e);
}
}
});
maps.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
Intent openMap = new Intent("com.schweigert.drinkingbuddy.MainActivity");
startActivity(openMap);
}
catch(Exception e){
display.setText("my d"+ e);
}
}
});
drinks.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
Intent openDrinkMenu = new Intent("com.schweigert.drinkingbuddy.DrinkType");
startActivity(openDrinkMenu);
}
catch(Exception e){
display.setText("my d:" + e);
}
}
});
}
}
So i had a try catch that was interacting with a textview that didn't exist anymore and then i had my manifest set up incorrectly. Thanks for the help Shivan.
After reading about Android's Toast class, I had to try it out.
I added a button to my layout, followed the instructions on this page, and added an OnClickListener to my button that would call the toast.
The problem now is that when I debug the app, the button doesn't appear on the view.
Do I have something where it's not supposed to be? (Additional information available upon request)
Code:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relLayout"
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=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
MainActivity.java
package com.joseph.toasttest;
import com.joseph.toasttest.R;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity {
Context context = getApplicationContext();
CharSequence text = "Test";
int duration = Toast.LENGTH_SHORT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnToast = (Button) findViewById(R.id.button1);
btnToast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toastMe(v);
}
});
}
private void toastMe(View v) {
Toast.makeText(context, text, duration).show();
}
}
try this :
btnToast.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast msg = Toast.makeText(MainActivity.this, text, duration );
msg.show();
Just guessing here, but I'd start removing those paddings in your RelativeLayout. You may be running the app in a device in which values of
#dimen/activity_vertical_margin
and/or
#dimen/activity_horizontal_margin
are 'moving' your button out of the screen. You can also try aligning the button in the center of the view.-
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button" />
You must create your context inside onCreate() method:
package com.joseph.toasttest;
import com.joseph.toasttest.R;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity {
Context context = getApplicationContext();
CharSequence text = "Test";
int duration = Toast.LENGTH_SHORT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnToast = (Button) findViewById(R.id.button1);
btnToast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toastMe(v);
}
});
}
private void toastMe(View v) {
Toast.makeText(context, text, duration).show();
}
}
You can't use the application context for UI stuff, like creating dialogs, toasts or launching activities. Just use the activity as a context instead.
Different contexts in Android, and what they can and can't be used for.
I got it to work by removing the context and duration variables altogether and setting them manually inside my Toast method.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnToast = (Button) findViewById(R.id.button1);
btnToast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toastMe(v);
}
});
}
private void toastMe(View v) {
Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_SHORT).show();
}
}
This should work:
package com.joseph.toasttest;
import com.joseph.toasttest.R;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity {
CharSequence text = "Test";
int duration = Toast.LENGTH_SHORT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnToast = (Button) findViewById(R.id.button1);
btnToast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toastMe(this);
}
});
}
private void toastMe(Context context) {
Toast.makeText(context, text, duration).show();
}
}
This is my starting activity. I'm trying to get this button to work but it's been giving me this error.
Line 15 button can not be resolved.
package com.synamegames.giveaway;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class GiveawayActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button register = (Button) findViewById(R.id.register);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
setContentView(R.layout.main);
}
}
Please try this..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
}
The problem is You have defined the Button instances as register in the line final Button register = (Button) findViewById(R.id.register); But you are setting onclick listener to the button instance which is not defined. You should have
register.setOnClickListener(new OnClickListener() {
instead of
button.setOnClickListener(new OnClickListener() {
U can use android:onClick from xml and pass a the view into the .java file
eg:
android:onClick="bactToList"
in java:
public void bactToList(View view){
}