android cannot resolve method setcontentview - android

I came across an error today in android studio. I am trying to create a about us screen in the app. The layout xml file has been created. Any help is appreciated. Thanks.
error: cannot resolve method setcontentview(int)
package example.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class AboutFragment extends android.app.Fragment {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about_screen);
}
}

Your class extends Fragment and you have setContentView(R.layout.about_screen);. setContentView is a method of Activity class.
Instead inflate about_screen.xml in onCreateView of Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.about_screeen, container, false);
return rootView;
}

Related

How do I add a Map Activity to a fragment and obtain the location of the nearest hospital?

I have an empty fragment where I want to add a Google View map which can find the location of the nearest hospital. I have no clue how to do this.
Here is the code for the empty fragment:
package com.iotaconcepts.aurum;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TwoFragment extends Fragment
{
public TwoFragment()
{
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_two, container, false);
}
}
This tutorial will be useful for you to get started with your MAP Fragment
http://www.joellipman.com/articles/google/android/application-development/android-os-add-googlemap-as-fragment.html
And for finding the hospitals near you you can use the following tutorial
http://javapapers.com/android/find-places-nearby-in-google-maps-using-google-places-apiandroid-app/

add() function giving error in Fragment

I have the following code in the Main Activity and I have a FragmentLayout class Which is responsible for calling the fragment.
Code
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends FragmentActivity
{
Button btn;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
FragmentLayout f1=new FragmentLayout();
getSupportFragmentManager().beginTransaction().add(R.id.main_id, f1).commit();
}
});
}
}
I get this error-
The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, FragmentLayout) MainActivity.java.
What is the problem here?
You can use transaction manager only for Fragments.
I provide you simple example:
Create some Fragment and layour for it:
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
And in your public void onClick(View v)
just call:
FragmentOne f1=new FragmentOne();
getSupportFragmentManager().beginTransaction().add(R.id.main_id, f1).commit();
Here
getSupportFragmentManager().beginTransaction().add(R.id.main_id, f1).commit();
f1 must extend Fragment in order to be added as part of a FragmentTransaction.

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

Application fails to start once onClickListener is set

Being new to android development I'm expecting few hiccups there and now, but this one got me scratching my head.
I started out by creating few buttons in fragment_main.xml file (I'm working in Android Studio and using Genymotion as my Emulator)
The reason why I mentioned this is because I use activity_main.xml as a view in my MainActivity java file (I'm still a little bit confused with these two files, but from what I've heard activity_main.xml uses layout in fragment_main.xml file?)
Anyways, Application runs as expected, but gives "Unfortunately [Application Name] has stopped" error once I add onClick Listener to getAboutButton
My MainActivity Looks like this at the moment (including imports as well just in case):
package com.example.braintraininggame.app;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
/*
Declare variables.
*/
Button getAboutButton;
#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();
}
/*
Assign views to variables.
*/
getAboutButton = (Button) findViewById(R.id.buttonAbout);
/*
Create Listeners.
*/
getAboutButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}
/*
Methods
*/
#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;
}
/**
* 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 assume this is stacktrace:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.braintraininggame.app/com.example.braintraininggame.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Android studio says that it is caused by line 38, in this case line that follows right after "Create Listener" comment.
Your about button doesn't exist yet since your Fragment has not inflated its view. Calling commit() on a fragment transaction does not mean your fragment is ready to go. It just means you are scheduling the commit transaction the next time the UI thread is available. Your calls to findViewById will return null. You should put code that accesses or modifies a fragments UI within the fragment itself. By putting it in the activity you are eliminating the point of fragments.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button getAboutButton = (Button) rootView.findViewById(R.id.buttonAbout);
getAboutButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
return rootView;
}
Add #override.
Change this code:
getAboutButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
To this:
getAboutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});

webview is not loading url inside the fragment in android

I am new to fragment.I am loading url in webview inside fragment but its not loading .In emulator webpage may temporarily down message is coming .in samsung galaxy tab device coming blank can anybody tell what is problem How to solve
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
public class WebViewFragment1 extends Fragment {
WebView loadWeb;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Tell the framework to try to keep this fragment around
// during a configuration change.
setRetainInstance(true);
// Start up the worker thread.
}
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.gcmweb, container, false);
loadWeb=(WebView)layout.findViewById(R.id.loadWeb);
/*String url = "http://unstructure.org/unstruc/unstruc.php?i="
+ "sample test".replaceAll(" ", "-");*/
String url="http://www.google.com/";
loadWeb.loadUrl(url);
return layout;
}
}
Here I am adding dynamically in activity
FragmentTransaction ft = fm.beginTransaction();
Fragment fragReplace=new WebViewFragment1();
//fm.beginTransaction();
ft.add(R.id.replace_frag_container,fragReplace);
ft.addToBackStack(null);
ft.commit();
Thanks
For using fragments, you should extend activity and have a layout of fragment or vice versa. Check this link This is a great tutorial for beginners
public class DetailsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return(inflater.inflate(R.layout.details_fragment, container, false));
}
public void loadUrl(String url) {
((WebView)(getView().findViewById(R.id.browser))).loadUrl(url);
}
}
This is working for me can u try this..

Categories

Resources