Why make this activity does not show my layout? - android

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.

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

Keeping inputs in activity after moving to another activity

I am using Android Studio. I have two activities, MainActivity and Main2Activity. There is an edit text and a button in each one. How do I keep the input in the edit text in any activity when I go to the second activity? I tried many answers but nothing worked.
Here is the code of the activity.
public class MainActivity extends AppCompatActivity {
EditText editText2;
Button button;
String var1 ;
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("var1", var1);
editText2 = (EditText)findViewById(R.id.editText2);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
var1 = savedInstanceState.getString("var1");
var1 = editText2.getText().toString();
editText2.setText(var1);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Click1(View view)
{
Intent i = new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
}
To carry over values from one activity to another, you need to add a line of code before startActivity(i);. This line of code that you need to add is:
i.putExtra("inputValue", String.valueOf(editText2.getText()));
Then in the other activity, in the onCreate() method, add this:
Intent intent = getIntent();
String inputValue = intent.getStringExtra("inputValue");
editText.setText(inputValue);
Obviously I don't know the name of your EditText in the MainActivity2 so replace the editText with its name.
Hope this helps!
You need to check for the existence of savedInstanceState in your onCreate function.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check for a savedInstanceState
if(savedInstanceState != null){
var1 = savedInstanceState.getString("var1");
editText2.setText(var1); // Making sure that you have assigned editText2 already, of course.
}
}

Loader is not retained after starting new activity, changing orientation in new activity and pressing back button

I been running into an issue with loaders lately. I created a small project that reproduces the issue https://github.com/solcott/loaders-orientation-change
I have a simple Activity that adds a fragment
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
if(mainFragment == null){
Log.w(getClass().getSimpleName(), "creating new Fragment");
mainFragment = new MainFragment();
getFragmentManager().beginTransaction().add(R.id.main_fragment, mainFragment).commit();
}
}
}
In my fragment I start a Loader that just returns an integer that is displayed on the screen. There is also a button that starts a new Activity:
public class MainFragment extends Fragment implements LoaderCallbacks<Integer> {
private static int NEXT_VAL = 0;
TextView text1;
Button button1;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(1, null, this);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
text1 = (TextView) view.findViewById(android.R.id.text1);
button1 = (Button) view.findViewById(android.R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), NextActivity.class));
}
});
}
#Override
public Loader<Integer> onCreateLoader(int id, Bundle args) {
AsyncTaskLoader<Integer> loader = new AsyncTaskLoader<Integer>(
getActivity()) {
#Override
public Integer loadInBackground() {
return NEXT_VAL++;
}
};
loader.forceLoad();
return loader;
}
#Override
public void onLoadFinished(Loader<Integer> loader, Integer data) {
text1.setText(data.toString());
}
#Override
public void onLoaderReset(Loader<Integer> loader) {
// TODO Auto-generated method stub
}
}
Up to this point everything works fine I can change the orientation and the integer that is displayed doesn't change. I can even start the new activity and then hit the back button and the integer displayed doesn't change.
However, when I navigate to the new Activity, change orientation and press the back button the integer is incremented. I would expect it to behave the same way that it did without the orientation change.
Calling setRetainInstance(true) in Fragment.onCreate() make it even worse. onLoadComplete is never called and the integer is not displayed at all.
Has anyone else run into this issue and found a workaround?
Same question as asked here: Loader unable to retain itself during certain configuration change
This is a damn annoying problem with AsyncTask loaders. Although a solution is proposed in that thread, I think the general consensus is to avoid AsyncTaskLoaders.

Best practice: subclass or send intent?

I have two activities almost doing the same thing. The only thing that differs them is a URL to be parsed.
What is considered best practice regarding Android development, subclass just to set the URL or send the URL via an intent?
public SuperActivity extends Activity{
protected String pageUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
[...lots of stuff...]
super.onCreate(savedInstanceState);
}
}
public SubActivityOne extends SuperActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
pageUrl = "http://urlOne.com"
super.onCreate(savedInstanceState);
}
}
public SubActivityTwo extends SuperActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
pageUrl = "http://urlTwo.com"
super.onCreate(savedInstanceState);
}
}
or
public SuperActivity extends Activity{
private String pageUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
Bundle extras = getIntent().getExtras();
pageUrl = extras.getString("intent_key_url");
[...lots of stuff...]
super.onCreate(savedInstanceState);
}
}
if you have multiple activities, which share similar functionality (and/or variables/methods) -> go for sub-classing.
if all you need is to pass a value from the invoking activity, use intents (or maybe Static)
In your case, I would subclass, but do it slightly differently than you. It's dangerous to put some code in onCreate when it's not definitely needed. (You may get lost in your hierarchy and not call exactly what you want to call in the correct order) I would use an overriden method rather than a variable. Do that:
public abstract SuperActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
[...lots of stuff...]
// instead of using pageUrl, use a method when you need it: pageUrl()
super.onCreate(savedInstanceState);
}
protected abstract String pageUrl();
}
And your subactivities would look like: (only one shown here)
public SubActivityOne extends SuperActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected String pageUrl() {
return "http://urlOne.com";
}
}
The intent way is good too, but it might become complicated if later on you want to add more differences in your subactivities. With subclasses, it's very flexible.
To avoid duplicate code (which is mostly the good thing), make base activity class (make it abstract) and put your common code there. Then make your "real" activities extend base class. So option "A" is the way.
A third option would be to make a callback interface and have any class you want implement it. Something like "PageUrlProvider"
public interface PageUrlProvider
{
String getPageURL();
}
Then the concrete implementation would be
public MyActivity extends Activity implements PageUrlProvider
{
private String pageUrl ="http://example.com/";
#Override
protected void onCreate(Bundle savedInstanceState)
{
String myURL = getPageURL();
super.onCreate(savedInstanceState);
}
#Override
public String getPageURL()
{
return pageUrl;
}
}
Which option you choose is largely dependent on what you are trying to accomplish. But this option offers a lot of flexibility.

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