i am trying to use this answer but i don't get why am i getting this error:
error: class, interface, or enum expected
the code:
package com.example.moviereview3;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
}
BottomNavigationView bottomNavigationView = BottomNavigationView)findViewById(R.id.bottom_navigation);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);
sorry for asking so many dumb questions and thanks for answering
Do like this:-
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);
}
}
You are missing a "(" right before casting the view, but even more importantly you're lacking to understand that the reference should be called inside the onCreate() method of the class. Placing stuff outside of the class would compromise proper form and prevent the compiler from recognizing and/or synthesizing the code you wrote, as such, the example you provided.
As an alternative, you could create a class member variable and then access it with the this operator, but most importantly make sure the code is inside the class scope.
private ...View bnv; ... this.bnv = instantiate
This would be your modified code, it's up to you if you want member variables vs local variables.
public class HomeActivity extends AppCompatActivity {
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
BottomNavigationView bottomNavigationView =
(BottomNavigationView)findViewById(R.id.bottom_navigation);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);
} //END_ON_CREATE_METHOD
}/END_HOMEACTIVITY_CLASS
Related
I want to call a method from another activity in my activity. I tried this codes but my app is crashed! :
SecondActivity:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.widget.*;
public class SecondActivity extends Activity
{
public void toast()
{
Toast.makeText(getApplicationContext(),"hello",50).show();
}
}
MainActivity:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import com.mycompany.myapp.*;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SecondActivity s=new SecondActivity();
s.toast();
}
}
What the problem?! Please help me. Thanks.
I think you are confusing the Java class and Activity. If you want to declare the methods which don't need a layout and activity stuff, create a java class and have public methods.
1) MainActivity - Activity Class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToastClass toastClass = new ToastClass();
toastClass.toast(getApplicationContext(), "Hey dude!!");
}
}
2) ToastClass - java class
public class ToastClass {
public void toast(Context context, String msg) {
Toast.makeText(context ,"hello",Toast.LENGTH_SHORT).show();
}
}
Hope, it helps!
You do not instantiate activities, but starts them with an intent
Intent intentSecondActivity = new Intent(this,SecondActivity.class);
intentSecondActivity.putExtra("methodToStart","toast");
startActivity(intentScheduleActivity);
In the secondActivity read the extras and start the method:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.secondActivity);
String method = getIntent().getStringExtra("method");
if (method.equals("toast"){
toast();
}
}
Alternatively you might want to study fragments.
I am trying to make a PPT viewer app.I have added the pptViewer library in my project from https://github.com/itsrts/pptviewer-android.
But I am getting an error called "cannot resolve symbol 'activity'" on this line pptViewer.loadPPT(activity,"/home/waheed/lab6.pptx").Please help.
Below is my code:
package com.example.waheed.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.itsrts.pptviewer.PPTViewer;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PPTViewer pptViewer = (PPTViewer) findViewById(R.id.pptviewer);
pptViewer.setNext_img(R.drawable.next)
.setPrev_img(R.drawable.prev)
.setSettings_img(R.drawable.settings)
.setZoomin_img(R.drawable.zoomin)
.setZoomout_img(R.drawable.zoomout);
pptViewer.loadPPT(activity,"/home/waheed/lab6.pptx");
}
}
activity isn't a defined symbol, but . In this case, since the code is in an activity, use the current object:
pptViewer.loadPPT(this, "/home/waheed/lab6.pptx");
You probably copy-pasted from the readme, as the activity used as sample method input in the readme means you have to pass an activity instance. You don't declare Activity activity = ..., but since you're in an activity you can use this
pptViewer.loadPPT(this,"/home/waheed/lab6.pptx");
Try this:
pptViewer.loadPPT(MainActivity.this,"/home/waheed/lab6.pptx");
You reference activity in your code which is not defined. Pass current class instead of the undefined field.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PPTViewer pptViewer = (PPTViewer) findViewById(R.id.pptviewer);
pptViewer.setNext_img(R.drawable.next)
.setPrev_img(R.drawable.prev)
.setSettings_img(R.drawable.settings)
.setZoomin_img(R.drawable.zoomin)
.setZoomout_img(R.drawable.zoomout);
pptViewer.loadPPT(MainActivity.this,"/home/waheed/lab6.pptx");
}
I have a Java activity like so.
package com.xxx.yyy.overrideoncreate;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("DEBUG","Original oncreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I wish to split some of the onCreate instructions to a separate class, so I created this too.
package com.xxx.yyy.overrideoncreate;
import android.os.Bundle;
import android.util.Log;
/**
* Created by oox on 26/6/17.
*/
public class SubClass extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("DEBUG","Overridden");
super.onCreate(savedInstanceState);
}
}
I intend for subClass.onCreate to override MainActivity.onCreate -- both Overridden and original oncreate messages should be displayed. However, that did not seem to happen, the overridden message did not appear in the Logcat.
Any idea what I did wrong?
Thanks in advance.
In order for SubClass's onCreate to be called, an instance of SubClass should be created instead of an instance of MainActivity when the application is launched.
For that to happen, SubClass should become the actual main activity of your application (i.e. the activity class registered in the AndroidManifest.xml which gets launched when the application is launched).
I have a question about programming in android.
When I write a activity like this:
package com.mathquiz;
import android.widget.ImageButton;
public class ChooseMode extends ActionBarActivity {
private ImageButton easyButton=(ImageButton)findViewById(R.id.easy_button);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_mode);
easyButton.setVisibility(View.INVISIBLE);
}
The eclipse throws NullPointerException at line 4
However, if I assign "easyButton" in method onCreate, everything will be OK.
package com.mathquiz;
import android.widget.ImageButton;
public class ChooseMode extends ActionBarActivity {
private ImageButton easyButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_mode);
easyButton=(ImageButton)findViewById(R.id.easy_button);
easyButton.setVisibility(View.INVISIBLE);
}
Please help me to understand this problem.
It can't be "instanced" as you call it because in your first example, you haven't set a content view yet. Android's lifecycle methods have a specific order, that occur after the constructor is called.
The method works as expected, it just returns null because it has nothing to find at that current time.
Also, using an instance variable that requires using the current Activity as a Context will not work (In the below example, this refers to the ChooseMode instance).
Eg
public class ChooseMode extends ActionBarActivity {
private SharedPreferences = PreferenceManager.getDefaultSharedPreferences (this);
Will throw a NullPointerException down the line when used.
The layout is not defined on class variable definition. That's why it's a NullPointerException, because you haven't called setContentView(R.layout.activity_choose_mode); yet.
If you want to do that way, to avoid that amount of boilerplating, try RoboGuice. An example of a view could be this:
#ContentView(R.layout.main)
class RoboWay extends RoboActivity {
#InjectView(R.id.name) TextView name;
#InjectView(R.id.thumbnail) ImageView thumbnail;
#InjectResource(R.drawable.icon) Drawable icon;
#InjectResource(R.string.app_name) String myName;
#Inject LocationManager loc;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
name.setText( "Hello, " + myName );
}
}
More examples and tutorials are here.
I am trying to implement facebook like slider, and i am planning to user below opensource code
https://github.com/leonardosalles/shipp-sliding-menu
but after i integrate, i created a sample activity
as per the code guide, in the
PrincipalActivity.java
//intent = new Intent(this, MyNewActivity2.class);
if it try giving my class, i get below error
cannot find symbol constructor Intent(,java.lang.Class)
here is my activity class
import android.app.Activity;
import android.os.Bundle;
public class SampleFirstTest extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
please Help me to resolve the issue, i am new to android..
add SampleFirstTest in your AndroidManifest.xml