I'm getting the 'The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, WeatherFragment)' error.
This is my current code, I'm already using the android.support.v4.app.Fragment import, so what could be the problem?
Here's my currently code and the imports I'm doing:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.app.AlertDialog;
import android.content.DialogInterface;
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.EditText;
import android.os.Build;
public class WheaterActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wheater);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new WeatherFragment())
.commit();
}
}
First guess off the top of my head is that WeatherFragment does not extend from android.support.v4.app.Fragment but instead extends android.app.Fragment. Check you import statements in WeatherFragment.java.
Related
while learning from Udacity´s Sunshine app lesson, immediately after creating the sugested ForecastFragment.java - extracted from MainActivity.java - I got an error on both java fles, regardind the reference to ForecastFragment as shown int the code below.
In MainActivity at "new ForecastFragment()";
In ForecastFragment at "Public class ForecastFragment extends Fragment {"
Help will be very much appreciated.
// MainActivity.java
package com.example.android.sunshine.app;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
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 ForecastFragment())
.commit();
}
}
// ForecastFragment.java
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.example.android.sunshine.app.R;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* A placeholder fragment containing a simple view.
*/
public class ForecastFragment extends Fragment {
Sorry about this question. I've just figured out what is the problem: I have to include the reference to the app package at the beginning of the ForescastFragment.java, i.e,
Package com.example.android.sunshine.app;
I have a problem when using DialogFragment
DialogFragment dialog = new DateDialog(this);
dialog.setTargetFragment(this,1);
dialog.show(getFragmentManager(), "tag");
Where this caused the error. my class extends FragmentActivity.
I'm following this answer, but so far, cannot apply on my code. what do i do wrong ?
This is my class :
import android.app.AlertDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.support.v4.app.Fragment;
public class RegisterActivity extends FragmentActivity implements OnDateSetListener
{
}
Be sure you are using the correct DialogFragment:
http://developer.android.com/reference/android/app/DialogFragment.html
↳ android.app.Fragment
↳ android.app.DialogFragment
versus
↳ android.support.v4.app.Fragment
↳ android.support.v4.app.DialogFragment
It's easy to import the wrong class. Just look at which version of FragmentActivity you are using and pick the corresponding DialogFragment.
I use this tutorial to make Navigation Drawer: http://www.tutecentral.com/android-custom-navigation-drawer/
so now i want to add my navigation to other activities but unfortunately i cant.
i also follow this : How to Display Navigation Drawer in all activities?
and do this on my second activity but it have some error:
package com.example.uniapp;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ShowAll extends Description {
MyDatabase MyDataBase;
SQLiteDatabase mydb;
TextView title,content;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.showall, null, false);
mDrawerLayout.addView(contentView, 0); //null pointer error
}
}
Do you have any suggestion?
This means your drawerLayout is null. This is probably because it can;t find it. If you want your navigationdrawer in multiple activities, you also need to add the drawerLayout the all the activities its xml files.
Im trying to Use Fragements. But in MainActivity.java file the transaction.add(R.id.my_layout, testfrag, "");not Initiated correctly.please find below the code i used.
package com.example.testtabs;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.TabActivity;
import android.view.Menu;
import android.widget.TabHost;
import android.support.v4.app.Fragment;
import com.example.testtabs.R;
public class MainActivity extends TabActivity {
TabHost tab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragementTest testfrag=new FragementTest();
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.my_layout, testfrag, "");//Error is here
}
}
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity"
android:id="#+id/my_layout"
>
Error: The method add(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, FragementTest, String)
Fragement.test
package com.example.testtabs;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link android.support.v4.app.Fragment} subclass.
*
*/
public class FragementTest extends Fragment {
public FragementTest() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragement, container, false);
}
}
change:
import android.app.FragmentManager;
import android.app.FragmentTransaction;
to:
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
or---
change:
import android.support.v4.app.Fragment;
to
import android.app.Fragment;
All the Fragment should be android.app.Fragment. If you want to use android.support.v4.app.Fragment, you should use getSupportFragmentManager() to get the Manager.
Its working for me:
Instead of using getFragmentManager use getSupportFragmentManager();
I have some code which I think should compile, but doesn't:
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.problemio.ViewSolutionsActivity.DownloadWebPageTask;
import com.problemio.data.Discussion;
import com.problemio.data.DiscussionMessage;
import com.problemio.data.SuggestedSolution;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class TopicActivity extends Activity
{
ArrayAdapter<Discussion> adapter;
ArrayList<Discussion> discussion = new ArrayList <Discussion>( );
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.discussion);
// Have to display the topic, and the existing discussion, and the form field.
Discussion d = new Discussion ();
d.setDiscussionTopicName( "Please wait while the discussion comments load" );
discussion.add(d);
adapter = new ArrayAdapter<Discussion>( this,R.layout.discussion_comments, discussion);
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
There is more code below but the line with setListAdapter(adapter); gives this error:
The method setListAdapter(ArrayAdapter<Discussion>) is undefined for the type TopicActivity
Any idea why? I actually copied this code from another class and it worked well there.
Thanks!
You can only user setListAdapter() in a ListActivity
I think this should work extends your class to ListActivity.
You need to extend your activity with the ListActivity. Right now you have
public class TopicActivity extends Activity
Change it to
public class TopicActivity extends ListActivity
setListAdapter(adapter) works with
ListActivity or
if the layout has atleast one list view with id android.R.id.list