im sorry for the long question, but i could really use the help
so I've been trying to make a camera app for this school project that i have. i'm really new to coding in general, and i don't really know much about Java. i decided to use the CameraKit library by Furgle to help me with this. they say all i have to do is include
protected void onResume() {
super.onResume();
CameraView.start();
and
#Override
protected void onPause () {
super.onPause();
CameraView.stop();
}
i should be able to start and stop the camera preview im trying to create.
however, when i added this code to my main activity, i got the following:
non static method 'stop()' / 'start()' cannot be referenced from a static context
I've tried a few things like trying to create an object of the class and calling the method from that (i'm not completely sure if i said that right or not)
#Override
protected void onResume() {
super.onResume();
CameraView main = new CameraView()
main.start();
when i try to run that, i get:
cannot resolve constructor CameraView()
I also tried to create instances of the class called "CameraView" which is where the method "start();" and "stop();" are. sadly i have not been able to get anywhere with that.
the point is i tried everything that i could understand but any help would be greatly appreciated.
after looking into the code for the library, i saw that neither the start method or the stop method within the CameraView class are declared "static". so i really don't see where the problem is coming from and how to overcome it
Assuming the tutorial you're following is this one https://github.com/gogopop/CameraKit-Android#usage ...
When they say that "all you have to do" is add this code:
#Override
protected void onResume() {
super.onResume();
cameraView.start();
}
#Override
protected void onPause() {
cameraView.stop();
super.onPause();
}
They're speaking to more-experienced developers. The part they're leaving out is where does cameraView come from?
Well, the first step is to include a <CameraView> in your layout. But even after that, you need to find it and assign it to a cameraView variable. So really, you need all this:
public class MainActivity extends AppCompatActivity {
private CameraView cameraView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // `activity_main.xml` must have a `<CameraView>` tag with id `camera`
cameraView = (CameraView) findViewById(R.id.camera);
}
#Override
protected void onResume() {
super.onResume();
cameraView.start();
}
#Override
protected void onPause() {
cameraView.stop();
super.onPause();
}
}
Related
I am trying to define global objects using the application class.
I therefore define the following class.
public class MyApplication extends Application {
private MyObject myObject=new MyObject();
public MyObject getMyObject(){
return this.myObject;
}
}
Then, I use it in an activity, but I get an error (Cannot resolve method getApplication()):
public class AnActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mood);
Button buttonMusic=(Button) findViewById(R.id.button5);
buttonMusic.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MyApplication myApplication = ((MyApplication)this.getApplication());
Toast.makeText(MoodActivity.this, "playing music", Toast.LENGTH_SHORT).show();
}
});
}
}
I have no clue why I get this error, as it for example works when calling the getApplication() in another activity.
I'm pretty new to Android and Java, so please excuse the ingenuity of the question.
UPDATE
Then I do MyObject myObject=myApplication.getMyObject(); and I don't get any compilation issue but the app dies as soon as I get in that activity.
As I understand it is not advised to use the Application class for such use, what would be a good alternative?
You're getting this error because you call this.getApplication() inside the View.OnClickListener. Because of this, this now references the listener and not the activity.
To do what you need, just create a Context object outside of the listener in your activity's onCreate method and assign this to it. And, inside the listener, use context instead of this. Something like this :-
public class AnActivity extends Activity {
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mood);
context = this;
Button buttonMusic=(Button) findViewById(R.id.button5);
buttonMusic.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MyApplication myApplication = ((MyApplication)context.getApplication());//Changed "the" to "context"
Toast.makeText(MoodActivity.this, "playing music", Toast.LENGTH_SHORT).show();
}
});
}
}
Edit after question update :-
Instead of using the application class, use static objects to achieve global variables. static objects are independent of objects and can be referenced using the class that they belong to. For example, if you define a static variable in your MainActivity.class and name it testObject, then it can be accessed using the following code regardless of your current activity :-
YourObject object = MainActivity.testObject;
Unless you have a specific reason for extending the Application class in Android you probably shouldn't. For reference, look at the note in the documentation for this: https://developer.android.com/reference/android/app/Application.html .
If you are trying to create an object that you can use in your Android app, simply do that as you would in Java:
public class MyObject {
//Your stuff here
}
If there is a reason that you're specifically wanting to extend the Application class then perhaps there's more that people can do to help you if you explain what you're trying to do. I just don't necessarily see a need to go through all that complexity based on your example :)
Change this to AnActivity.this.
Inside the code below the meaning of this changes from AnActivity to View.onClickListener as it is another object and inside those braces you are in the scope of the click listener class
new View.OnClickListener() {
public void onClick(View v) {
MyApplication myApplication = ((MyApplication)this.getApplication());
Toast.makeText(MoodActivity.this, "playing music", Toast.LENGTH_SHORT).show();
}
}
So the code above should become
new View.OnClickListener() {
public void onClick(View v) {
MyApplication myApplication = ((MyApplication)AnActivity.this.getApplication());
Toast.makeText(MoodActivity.this, "playing music", Toast.LENGTH_SHORT).show();
}
}
You can read a bit more about it here
I'm trying to separate some of my java in a few different files.
I have my main class:
public class StartPage extends Activity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_page);
}
...
}
And then I have this other class that I'd like to run on the same layout:
public class part_settings_session extends Activity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_page);
Toast.makeText(getApplicationContext(), "This is from settings", Toast.LENGTH_SHORT).show();
}
...
}
But I'm not able to see that Toast happen anywhere or at any time. Is there a way to make both of these classes work in two separate files? This is to organize scripts for my own sake.
Two Activities can not be visible at same time and here in your code you have defined two Activities with same layout. Your code is fine but to see both activities working, you have to manually start next activity. Below code will help you. This code will start Next Activity 3 seconds after loading First Activity.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(StartPage.this, NextPage.class));
finish();
}
}, 3000);
in your onCreate() for the 2nd class, put a Log.d("part_settings_session", "onCreate"); and see if the onCreate ever gets called in the first place. (Since they are using the same layout, it might be difficult to see if you are 'actually' creating an instance of THIS class.
My guess is that you might not even be creating an instance of the part_settings_session class. And without logging it is pretty hard to tell that.
Here is a nice Activity base class that will log all life-cycle events for you
https://github.com/douglascraigschmidt/CS282/blob/c5cf5c4808ea082988ab87c00eeade4837eac967/assignments/assignment1/src/edu/vandy/common/LifecycleLoggingActivity.java
Have some problem with android finish() methods.
I have one parent-class activity. Lets call it ParentActivity. All other activities in my project extends ParentActivity. Each time on ParentActivity.onCreate there are some statement, and I want to stop activity from executing if it fails. But when I call finish() in parent, I cant stop executing onCreate method on its child. Something like that:
public class ParentActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!someStatement) finish();
}
public class Test extends ParentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("TAG", "I dont want this code!");
}
}
Surely, I can just verify in parent activity its status each time, but I dont think its a good idea.
public class Test extends RexActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isFinishing()) return; /// It works - but it bad :(((
Log.d("TAG", "I dont want this code!");
}
}
Can I somehow stop executing onCreate method on child activity from its parent? Many thanks for any help!
I'm not sure if I got your question right. As you have some grammatical issues.
The onCreate statements are always executed. You can either have a Boolean in ParentActivity to stop the code from executing in ChildActivity#onCreate().
You can try making your onCreate() code more modular by dividing it into functions so that it's not called.
Let me know what works for you.
Best option is to Use finish() in your splash screen just before you create your second activity,
I know there are lots of quetions regarden onSaveInstanceState but I wasn't able to find an answer to my problem.
I have an activity that extends AppCompatActivity; this activity uses 3 fragments and it has a variable 'int currentStep' to track which fragment is being displayed. In the onSavedInstanceState method I store the currentStep variable in the bundle, and then in onCreate method I restore it.
public class MainActivity extends AppCompatActivity {
private final String CURRENT_STEP_TAG = "current_step";
private int currentStep;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pedido);
if(savedInstanceState == null) {
loadFirstFragment();
} else {
currentStep = savedInstanceState.getInt(CURRENT_STEP_TAG);
if(currentStep == 2){
//Do some stuff...
}
}
}
#Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
Log.d("deubg", "--------onsaveinstancestate--------");
outState.putInt(CURRENT_STEP_TAG, currentStep);
super.onSaveInstanceState(outState, outPersistentState);
}
...
}
The thing is that onSavedInstanceState won't get called when screen orentation changes, and according to google documentation, it should. The message "--------onsaveinstancestate--------" doesn't show in the console.
However the Bundle savedInstanceState in the method onCreate is non null after screen rotation, but it doesn't have the int 'currentStep'.
I've tried many things: changed AppCompatActivity for ActionBarActivity, moved the call for super.onSavedInstanceState to different locations, tried to store other variables; but no matter what I do the method doesn't execute.
Also I DO NOT have android:configChanges in my manifest.
My application is being compiled against sdk version 22, buildToolsVersion 22.0.1
Unless your app is running on Lollipop (API21) version of Android or newer, your
public void onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState);
will NOT be called as it simply does not exist on earlier versions of the platform than 21. To support pre API 21 devices you must, instead of the above, override the following method:
public void onSaveInstanceState (Bundle outState);
This will work on API 21+ as well, so you do not need to override both methods, of course (unless you know you need to deal with PersistableBundle the new one offers).
See docs.
How to overload onResume() to work the correct way? I want to come back from activity to MainActivity where I want to have the same state as after app start. I wanted to use recreate() but it looped or some sort of that.
My code:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
recreate();
}
implement onSaveInstanceState(Bundle save) and onRestoreInstanceState(Bundle restore) to save and restore the state. See the documentation on this.
I guess, when you press back button, you want to refresh the previous activity.
It is pretty simple, you can just put your method in onResume(), and it should do the trick.
Ok Try this out, it is working for me.
public class main extends Activity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//add any buttons or anything you have here.
doMainWork(); \\lets just say you have this method, which contains the main code of the layout.
}
protected void onResume(){
super.onResume();
doMainWork();
}
public void doMainWork(){
\\Put all your working code here. And this should work it out man.
}