Android: starting activity using intent on button click - android

I'm trying to start activitiy using intent:
public class Bez_provjere_739 extends Activity {
Button Tbutun;
public void onCreate(Bundle savedInstanceState) {
Tbutun.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent otvoriIn = new Intent("com.riteh.HL.IN");
startActivity(otvoriIn);
}
});
I use it several times in my aplication in the same way but only in this case i get error:
05-17 00:44:43.694: E/AndroidRuntime(3533): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.riteh.HL/com.riteh.HL.Bez_provjere_739}: java.lang.NullPointerException

You never initialized your button nor assigned to it, so it is null.
Also, there are some basics missing from your code:
// naming conventions - variable names start with lower case letter
private Button tbutun;
#Override
public void onCreate(Bundle savedInstanceState) {
// always call super.onCreate
super.onCreate(savedInstanceState);
// if you have a layout XML, use it
setContentView(R.layout.lay_xml_name);
// if the button declared in the layout, refer to it
tbutun = (Button)findViewById(R.id.the_button_name);
// the rest
}

Initialize your Button, try this
public class Bez_provjere_739 extends Activity {
Button Tbutun;
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
Tbutun=(Button)findViewById(R.id.button1) //change the id as per yours
Tbutun.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent otvoriIn = new Intent("com.riteh.HL.IN");
startActivity(otvoriIn);
}
});

Related

Cannot access a global final variable from within an inner class

I am using an animation to slide a view to the top of the screen. The code for the animation is contained within a method called LoopAnimation() which is called from main.
public class MainActivity extends AppCompatActivity {
final View view = findViewById(R.id.view);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoopAnimation(view); \\ The animation loop method
}
This LoopAnimation() method uses a nested setOnClickListener to create an animation loop
public void LoopAnimation(View view){
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// starts the animation
view.animate().translationY(-100);
view.animate().setDuration(1500);
// reverses the animation
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do some job here
view.animate().translationY(100);
view.animate().setDuration(1500);
LoopAnimation(view); // Method calls itself
// to create loop effect
}
});
}
});
}
The problem is that I am getting a trivial error that I can't understand. Although I have declared view as global and final, I get this error in LoopAnimation()
Variable 'view' is accessed from within inner class, needs to be declared final.
You are using the variable view that is defined within your method's scope. Notice that your method's parameter is also called view so you are not actually using the global variable that you think you are using.
public void LoopAnimation(View view){
Edit: I've looked more into the way that you are trying to do this, and the approach isn't what I would do. Here is something more reasonable:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.view);
view.setOnClickListener(new View.OnClickListener() {
private boolean _forwards = true;
#Override
public void onClick(View v) {
if (_forwards) {
// starts the animation
v.animate().translationY(-100);
v.animate().setDuration(1500);
_forwards = false;
} else {
// reverses the animation
v.animate().translationY(100);
v.animate().setDuration(1500);
_forwards = true;
}
}
}
}
}

how to disable a button on restart of an activity android

I have activity flow
Activity 1-> Activity 2->Activity 3
If the user performs certain action in Activity 3, shared preferences boolean variable "performed" stores true. Now if the user presses back button to load Activity 2, it doesn't (i used finish()) and loads Activity 1. This is fine and as per the need.
What I want now is, when Activity 1 restarts this way, based on the value of the shared preferences boolean variable "performed" value true the button in Activity should be hidden.
I am using,
if (preferences.getBoolean("performed",false)){
button.setVisibility(View.Gone);
}
I have written this code in onRestart method of the Activity 1.
What am I missing?
The button does not hides.
I have done it in following way
- wrote a singalton class has variable IsVisited.
- check if it is visited in onResume() method
- if visited then set visibility of the button gone
Main Activity
Controller c;
Button bu ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bu = (Button)findViewById(R.id.button1);
c = Controller.getController();
bu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
c.setIsVisited(true);
Intent i = new Intent(MainActivity.this, mainactivity2.class);
startActivity(i);
}
});
}
protected void onResume() {
super.onResume();
if(c.getIsVisited())
{
bu.setVisibility(View.GONE);
}
Another Activity
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Controller c = Controller.getController();
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
Controller Class (Singleton Class)
public class Controller {
private static final Controller controller = new Controller();
private Boolean isVisited = false;
private Controller()
{
}
public static Controller getController()
{
return controller;
}
public Boolean getIsVisited() {
return isVisited;
}
public void setIsVisited(Boolean isVisited) {
this.isVisited = isVisited;
}}
![enter image description here][1]

android inheritance activity(Base - Main activity)

I am having a problem about implemeting android apps.
I made baseActivity which is that base of other applications and other applications.
These are my code.
First, BaseActivity.java
public class BaseActivity extends Activity {
protected void onCreate(Bundle savedInstanceState,int layoutId) {
super.onCreate(savedInstanceState);
setContentView(layoutId);
Button menuBtn = (Button)findViewById(R.id.menuBtn);
menuBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("bss","menu");
}
});
}
}
And the other one is MainActivity.java
public class MainActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState,R.layout.activity_main);
setContentView(R.layout.activity_main);
}
}
Now I have a question! When I was click the menu button, click listener did not act. It did not print log message and any action. So, I have a problem to make my application. Is it related with life-cycle or How can I solve the problem.
I do not like the way you are forcing thing, still if you want to make it works this way you have remove setContentView(R.layout.activity_main); from MainActivity, that's because one you call again setContentView(R.layout.activity_main); in the sublcass, the view hierarchy for that Activity will be recreated invalidating what you have done in the super class
The code is setting the Content View twice. Following is the sequence of your code.
setContentView()
Add button listener
setContentView()
Statement # 2, adds the button listener and it is all good till now. But as soon as you reset the content view on statement # 3, the previous settings get void. And the button gets reinitialized and the onClickListener is no more attached to the button.
Remove the contentview from BaseActivity and put
Button menuBtn = (Button)findViewById(R.id.menuBtn);
menuBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("bss","menu");
}
});
into MainActivity. The Base activity should be SuperClass and MainActivity subclass all the actions for view shold be initialize into the subclass.
Try this :
public class BaseActivity extends Activity {
protected void onCreate(Bundle savedInstanceState,int layoutId) {
super.onCreate(savedInstanceState);
}
public int getLayoutXML() {
return -1;
}
public abstract int getMenuId();
}
After this use this BaseActivity class like this :
public class service_detail extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Use button like this:
Button menuBtn = (Button)findViewById(R.id.menuBtn);
menuBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("bss","menu");
}
});
}
#Override
public int getMenuId() {
// TODO Auto-generated method stub
return 1;
}
#Override
public int getLayoutXML() {
// TODO Auto-generated method stub
return R.layout.service_detail;
}
}

how to retrieve Data from Intent ?? [duplicate]

This question already has answers here:
How do I get extra data from intent on Android?
(16 answers)
Closed 8 years ago.
I just want to show an simple Employee record on next layoutPage/Activity !!
this is my EmpLogin Java File
public class EmpLogin extends Activity {
private Button show;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
// TODO Auto-generated method stub
show=(Button)findViewById(R.id.show);
show.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText no=(EditText)findViewById(R.id.getno);
EditText name=(EditText)findViewById(R.id.getname);
EditText sal=(EditText)findViewById(R.id.getsalary);
Intent emp = new Intent(getApplicationContext(),EmpShow.class);
emp.putExtra("EmpNO",(no.getText().toString()));
emp.putExtra("EmpName",(name.getText().toString()));
emp.putExtra("Sal",(sal.getText().toString()));
startActivity(emp);
}
});
}
}
How to use Retrieve data from the Intent ??? By using getExtra() method ??? or there is simple way ?? this my EmpShow.class file !!
public class EmpShow extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empshow);
// TODO Auto-generated method stub
Intent show = getIntent();
}
}
As you passed data insideintent, So try to fetch the intent which has started your activity using the getIntent() method:
Intent intent = getIntent();
If your extra data is represented as strings, then you can use
intent.getStringExtra(String name) method.
As per your OP's class and intent. Here is the snipped which can fetch the values from your intent against respective variable,
public class EmpShow extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.empshow);
Intent intent = getIntent();
String empNo = intent.getStringExtra("EmpNO");
String empNname = intent.getStringExtra("EmpName");
String empSal = intent.getStringExtra("sal");
}
}
First Activity:
Intent i=new Intent(FirstActivity.this,SecondActivity.class);
i.putExtra("VAL",value1);
startActivity(i);
Second Activity:
Intent getI=getIntent();
String a =getI.getStringExtra("VAL");

How can I activity close in Android?

I have three classActivity created. One is super class and other sub class and third is HomeActivity.
Code for super class is :
public class MyActivity extends Activity {
Button btnHome = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onHomeClick(View view) {
String LOG_TAG = "Akshar";
System.out.println("Hello11111");
btnHome = (Button) view;
Log.v(LOG_TAG, "index=" + btnHome);
}
}
and my subclass code is :
public class ChooseIsland extends MyActivity {
Button btn_home = null;
MyActivity ob1 = new MyActivity();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chooseiland);
addListenerOnButton();
}
private void addListenerOnButton() {
// TODO Auto-generated method stub
ob1.onHomeClick(btn_home);
}
}
Now I want to go on Home page when click so when I write ?
Intent intent = new Intent(this, HomeActivity.class);
There is no close operation as such in android. You should make sure you do not save anything in stack so whenever you are traversing from one activity to other, make sure you use intent flags to clear history or top of stack and then call finish.
finish() will close the current activity and the previous activity will come to foreground.
Generally to we write finish() method to close activity.

Categories

Resources