No PlaceholderFragment class in MainActivity.java - android

I am trying to learn android programming by google's android programming course (android studio) in udacity.
The problem is I do not have "PlaceholderFragment" class in my MainActivity.java.
I only have MainActivity class in this file.
Could anyone help me please?
Edit:
my MainActivity.java is:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

I experienced the same problem when using Udacity course "Developing Android Apps: Android Fundamentals". The Udacity videos are old > 9 months and since android studios is always updating, the newer version of android studio does not provide an inner class for the fragment anymore. Instead android studio generates a separate class containing a fragment. But, no problem.
Go to the tab of Lesson 1: "Using the Sunshine Github Repository" and open the link: Udacity Sunshine repository
Download the branch : "1.01_hello_world" and import the contents into android studios. You will have the "PlaceholderFragment" class within the MainActivity.java and then you can continue the lessons at Udacity

You need to read a good webpage. I recommend reading and studying code from Google's # Fragments.
Code snippets from the webpage:
public static class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Note: ExampleFragment could be your "PlaceholderFragment".

Just delete your code succeeding your package name in the MainActivity.java file and replace it with this one below.
import android.app.Fragment;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}

I got the same problem, right after I put the "ForecastFragment" class into it's own file.
What solved the problem for me was to go build - clean project. After doing that, it built just fine.
Perhaps there are some old build files that look for placeholderFragment?

I had the same problem, but I tried to figure out how to work with the new structure. I put the code inside MainActivityFragment.java, inside the method onCreateView, since this is the new approach. It worked for me.
Take a look of my final code:
public class MainActivityFragment extends Fragment {
private ArrayAdapter<String> mForecastAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container);
ArrayList<String> weekForecast = new ArrayList<>();
weekForecast.add("Today - Sunny - 88/63");
weekForecast.add("Tomorrow - Foggy - 70/40");
weekForecast.add("Weds - Cloudy - 72/63");
weekForecast.add("Thurs - Asteroids - 75/65");
weekForecast.add("Fri - Heavy Rain - 65/56");
weekForecast.add("Sat - HELP TRAPPED IN WEATHERSTATION - 60/51");
weekForecast.add("Sun - Sunny - 80/68");
mForecastAdapter = new ArrayAdapter<>(getActivity(),
R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekForecast);
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
//return inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}

Related

Opening an activity from a fragment

I have a bottom navigation bar that switches between 5 fragments. One of these 5 buttons should open WebView (newsfeed) and display some HTML. The code that calls the newsfeed.xml is this:
public class newsfeedFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.newsfeed,container,false);
return v;
}
}
Now I followed a youtube tutorial to pull the HTML from a website (code below). In the tutorial, the code is in it's own activity and not called within a fragment. I am not sure how to combine these 2 code snippets to create the webview inside my fragment. Help would be much appreciated!
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends ActionBarActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.website.com");
myWebView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if(myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I assume you want to load the webview into the fragment?
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.newsfeed,container,false)
WebView webview = (WebView) v.findViewById(R.id.webview);
webview.loadUrl("http://www.website.com")
return v;
}
So inflate the Layout and afterwards call findviewbyid with your new layout and then you could do anything you want with your webview and it is loaded in your Fragment. Hopefully the solution for your question.
Take WebView in your newsfeed layout, and do the same thing in fragment.
You can use this code anywhere in the fragment to open the activity:-
startActivity(new Intent(getActivity(),ActivitytoOpen.class));

Can't resolve getActionBarActivity() method

I am trying to hide the ActionBar in my Activity. I have an Activity with a Fragment, which looks like this:
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class IntroActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intro_activity);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
ActionBar actionBar = getSupportActionBar();<---- Error here
actionBar.hide();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_intro, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.intro_page_1, container, false);
return rootView;
}
}
}
The error I get is Cannot resolve method getActionBarActivity method. I have tried extending ActionBarActivity, but when I do, I get onCreate errors, and several others.
How can I hide the action bar?
It does not look like you are using the support library? If that is the case you should call getActionBar() not getSupportActionBar()
From your imports, it looks like you're not using support library. So you should use getActionBar() instead of getSupportActionBar().
use this code
ActionBar bar = getActionBar();
bar.hide();
This will solve your problem

Android Fragement Activity

I dont know what happen with my sdk and eclipse. I install google services,it is install successfully but now i add activity to my old project but now .java file is extends FragementActivity. i dont know why this is happen. Please give reply please.
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class WebViewActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.web_view, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}

Android Eclipse debugging source not found

I looked already a lot for a solution for my problem and found similar items - but unfortunately I could not get round with that all to solve my problem. Here is what messes me up:
My small project is with a button and an onClickListener on it.
package com.example.wbbtn;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends Activity {
Button wb1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
wb1 = (Button) findViewById(R.id.b1);
wb1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
I put a breakpoint at wb1 = (Button) findViewById(R.id.b1);
When I start the debugging with my tablet already set to the debugging mode and connected to the PC and hit the F6 key I get the message that a source is not found.
I've made a screen shot to make things a bit more clear but I was told to have at least 10 reputations to post a pic. Okay - sorry !
Hello - sorry for this late response. Okay here are the links to the screen shots - hope it works:
a) source not found
b) Break point

R.id returns false

I have just started playing with Android using eclipse. I have been stalled on this one issue for quite a while now, and I have no idea why it doesn't work.
I have started a new app with as little coding done as possible just to see if the issue still occurs. Basically when I am typing 'R.id.' it will come up with the correct list of possible ids for me to access but when I actually try to run them it returns false.
The current one I'm testing has a very simple xml, where one of the ids is 'text'. So when I type R.id it prompts me with options, one of them is text. So i write R.id.text expecting it to return the id. Also when findViewById(R.id.text) it crashes the app with a null pointer exception on that line.
fragment_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.reecemerrett.historical.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
MainActivity.java:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
new AlertDialog.Builder(this)
.setTitle("ID")
.setMessage(R.id.text)
.show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
This code outputs false. I have tried it with many different ids and even with R.id.container.
I also tried removing the whole fragment style and calling fragment_main.xml directly with setContentView().
Any help is much appreciated.
The TextView belongs to the Fragment Layout ie fragment_main.xml.
So change to
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView tv = (TextView) rootView.findViewByid(R.id.text);
new AlertDialog.Builder(getActivity())
.setTitle("ID")
.setMessage(tv.getText().toString())
.show();
return rootView;
}
Or
As you already tried
I also tried removing the whole fragment style and calling fragment_main.xmldirectly with setContentView().
This will work. Make sure you get rid of Fragment related code like
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
TextView tv = (TextView)findViewByid(R.id.text);
new AlertDialog.Builder(this)
.setTitle("ID")
.setMessage(tv.getText().toString())
.show();
}
}
Try this..
Remove AlertDialog.Builder from MainActivity put that code in PlaceholderFragment like below
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
new AlertDialog.Builder(getActivity())
.setTitle("ID")
.setMessage(((TextView) rootView.findViewById(R.id.text)).getText().toString().trim())
.show();
return rootView;
}
}
I faced similar problem before. Since your view is inside your fragment findViewByID will not find it for you. Why? I don't know!! But it shouldn't be a problem because you can find this view inside your fragment's onCreateView callback. Assuming your view is a textView the code will look like this
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content_view, container, false);
TextView viewText = (TextView) view.findViewById(R.id.text);
return view;
}
Normally this would solve the problem because Activity doesn't need to know about views inside fragments. Fragments should handle their affair themselves and talk to activity only when it's necessary. However your activity still can access the this data using Fragment To Activity - Activity To Fragment Communication Technics. I will not explain them since there is a very good documentation on developer.android.com

Categories

Resources