Very strange error in code? - android

Very strange error in code. The part right after the semicolon in setContentView(R.layout.activity_main); is underlined in red. everytime I try to put up code block there. I am pretty sure my brackets add up correctly and there is no syntax error in the xml as well. Then Why is the red underlined ?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void function_name(View view){
/* code for the actual program including some declarations. */
}
I don't get it can someone help ?

You have not added } in onCreate method. You should define methods outside of onCreate method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void function_name(View view){
/* code for the actual program including some declarations. */
}

You forgot } in onCreate() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

Related

why am i getting error message on MainActivity Java?

I always get an error warning anytime i want to set find view by id
public class MainActivity extends AppCompatActivity {
View pdfView = findViewById(R.id.pdfView);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdfView.**fromAsset**("").load();
}
The error comes from "fromAsset." It always highlighted in red.
Does anyone have a solution to this?
Please initialize View inside onCreate() Method.
View pdfView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdfView = findViewById(R.id.pdfView);
pdfView.**fromAsset**("").load();
}

Android activity transition does not appear

I have been trying for some months to get an activity transition to work. I have tried a number of approaches, using XML and styles, and code, to no avail. Below is my most recent attempt. Does anyone know why I can't get any transitions to appear?
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addSlideTransitions();
setContentView(R.layout.my_activity_layout);
....
}
protected void addSlideTransitions()
{
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
Slide slide = new Slide();
slide.setDuration(1000);
getWindow().setEnterTransition(slide);
getWindow().setExitTransition(slide);
}
You must start the activity with an ActivityOptions bundle created with makeSceneTransitionAnimation.
That type of transition also only works when in the same task (as of N).
Try to do like this,I am sure this will work for you.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addSlideTransitions();
setContentView(R.layout.activity_main);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected void addSlideTransitions()
{
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
Slide slide = new Slide();
slide.setDuration(1000);
getWindow().setEnterTransition(slide);
getWindow().setExitTransition(slide);
}
}
Try adding the call to addSlideTransitions before super.onCreate.
#Override
protected void onCreate(Bundle savedInstanceState)
{
addSlideTransitions();
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity_layout);
....
}

How to call a method in Android from onCreate

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chustilla();}
public void chustilla (View v){ //Do anything }
When I compile it gives me a problem in the parameter of chustilla(). What can I do to call this method from onCreate?
PD: If I put "this" or "null" inside the brackets it doesn't work aswell
chustilla(View) requires a View reference to be passed as a parameter so it won't work if you don't pass a View reference to it. According to what is done inchustilla(View) (best known to you) you can pass it a View from the layout (Also, best known to you).
Your method public void chustilla (View v) expects to be passed a View object. But in onCreate() You simply call chustilla(). You need pass it a view object.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chustilla(a);
}
public void chustilla (Int a)
{
//your code
}
Try the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chustilla();
}
public void chustilla ()
{
//Do anything
}
or use this for example:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s;
chustilla(s);
}
public void chustilla (String v)
{ //write code here }
example parameter "new View(this)" :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chustilla(new View(this));}
public void chustilla (View v){ //Do anything }

What could possibly be causing this NPE in onCreate?

I am getting an NPE in onCreate of the following file (MySubActivity):
public class MySubActivity extends MySuperActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myTextView.setText(getResources().getString(R.string.myString));
}
}
MySuperActivity:
public class MySuperActivity extends Activity {
protected TextView myTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
myTextView = (TextView)findViewById(R.id.myTextViewid);
}
}
The strange thing is that I have never seen this crash while testing the app. The page works fine when I test it. However I got a crash report from Google notifying me of the crash. I cannot reproduce it, and I have no idea under what scenario this crash could happen. Seeing as how it works for me, the resource ids and string names etc. must be correct.
The only thing that came across my mind was that maybe the user had their phone set to a different language, so it couldn't properly pull the resources. However, there are default resources for all of them, and I tested changing the language of my emulator and it didn't crash. Any ideas?
Set your view in another method, like this:
public class MySubActivity extends MySuperActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void setView(){
myTextView.setText(getResources().getString(R.string.myString));
}
}
Edit: Don't call setView() in MySuperActivity
public class MySuperActivity extends Activity {
protected TextView myTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
myTextView = (TextView)findViewById(R.id.myTextViewid);
// Or, you can do this in a method called getView() if you like
}
}

Want to Run a function without click any button when application start

I have a function named 'func()'. I want to run this function when application start without clicking any button. just when application load I want to show a massage.that massage in that function. I just want to run that function when app start what will be the code.
public class TextViewActivity extends Activity {
public static EditText etxt;
public final void func(){
etxt.setText("Massage");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
}
}
Just put a call to the function on the onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
func(); //A call to the function.
}
Hope that helps.
I don't recommend subclassing the Application in order to do this. When the application starts it will go to the main activity. So I would say just keep a SharedPreference boolean value if it has been set. If not show the message.
So keep state of the application here: http://developer.android.com/reference/android/content/SharedPreferences.html, just set a boolean. Remember when you app gets called, the activity onCreate always gets called of the main activity, so its just a matter of not calling it again.
try using below code.. you need to call ur function after you initialize edittext etxt. so it can not cause you NPE
public final void func(){
etxt.setText("Message");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
func();// here your function call.
}
Try this:
public class TextViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.etxt2).setText("SMTH");
}
}
In case you really need a function to be called, you can use this:
public class TextViewActivity extends Activity {
public final void func() {
findViewById(R.id.etxt2).setText("SMTH");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
func();
}
}

Categories

Resources