why am i getting error message on MainActivity Java? - android

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();
}

Related

Very strange error in code?

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);
}

Why must be objects initialized in onCreate?

I have the following code:
public class MainActivity extends Activity {
TextView number=(TextView)findViewById(R.id.number2);
TextView number2=(TextView)findViewById(R.id.number2);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number.setText("Text");
number.setText("Text");
}
followed by more code, but when I run it it crashes.
After doing that i tried to initialize TextViews in onCreate()
public class MainActivity extends Activity {
TextView number;
TextView number2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number=(TextView)findViewById(R.id.number);
number2=(TextView)findViewById(R.id.number2);
number.setText("Text");
number.setText("Text");
}
and it worked. Why must objects be initialized in onCreate?
Your activity won't have a Window until onCreate(). Attempting to find any views before the window is initialized will lead to NPE.
Additionally, attempting to find views before setContentView() will return nulls and so the return values are not good for anything.

Why make this activity does not show my layout?

I have the following code
#SuppressLint("NewApi")
public class Happy_HourActivity extends SherlockActivity{
private ListView listaHP;
private Bundle bundle;
private List<HappyHourModel> listaHpModelResponse;
private HappyHourAdapter adapterHappyHour;
public void OnCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_happy__hour);
setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
setSupportProgressBarIndeterminateVisibility(true);
getSupportActionBar().setTitle("Happy Hour");
listaHP = (ListView)findViewById(R.id.list_happy);
bundle = getIntent().getExtras();
and is called from an activity called "descriptionActivity" follows.
case R.id.happy:
intent = new Intent(DescriptionActivity.this, Happy_HourActivity.class);
intent.putExtra("_id", _bundle.getString("_id"));
intent.putExtra("_name", _bundle.getString("_name"));
startActivity(intent);
break;
layout:
but this is what show
This method is wrong for what you want and probably never called
public void OnCreate(Bundle savedInstanceState){
it should be
#Override
public void onCreate(Bundle savedInstanceState){
Small "o". If you would have had the #Override annotation there then your IDE probably would have yelled at you making it easier to diagnose before running.

Application didn't launch when I create a Button in java

I've a strange problem. I explain after this :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// final Button buttonAlpha = (Button) findViewById(R.id.Alphabet);
}
Look you see, I put in comments the buttonAlpha and the launching of application works perfectly but when I remove the comments, my app didn't launching and I've no idea why. If somebody have/had the same problem, can I help me please ?
You need to put
final Button buttonAlpha = (Button) findViewById(R.id.Alphabet);
inside a method or else it will try to run it before running onCreate(), hence before setContentView()
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button buttonAlpha = (Button) findViewById(R.id.Alphabet);
}

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