Reputation: 1132
package com.example.**.beatle.app;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.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 {
private ArrayAdapter<String> mForecastAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastArray = {"Today-Sunny-88/63", "Tommorrow-windy-88/94", "wed-aloo-alooo"};
List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter = new ArrayAdapter<String>(getActivity(), R.layout.fragment_main, R.id.list_item_forecast_textview, weekForecast);
ListView listView = (ListView) rootView.findViewById(R.id.list_view_forecast);
listView.setAdapter(mForecastAdapter);
return rootView;
}
}}
Messages Gradle Build Error:(27, 59) error: no suitable method found for add(int,PlaceholderFragment) method FragmentTransaction.add(Fragment,String) is not applicable (argument mismatch; int cannot be converted to Fragment) method FragmentTransaction.add(int,Fragment) is not applicable (argument mismatch; PlaceholderFragment cannot be converted to Fragment) Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output Error:Execution failed for task ':app:compileDebugJava'. Compilation failed; see the compiler error output for details.
Upvotes: 16
Views: 25781
Reputation: 3156
If you use AndroidX of Andorid Jetpack, then use,
androidx.fragment.app.Fragment
instead of,
import android.app.Fragment;
// or
import android.support.v4.app.Fragment;
Upvotes: 3
Reputation: 399
Use
import android.support.v4.app.Fragment;
instead of
import android.app.Fragment;
All over in the project.
Also, use support library for FragmentManager and FragmentTransaction.
Upvotes: 3
Reputation: 1007330
You are using FragmentActivity
and getSupportFragmentManager()
. Therefore, PlaceholderFragment
needs to inherit from android.support.v4.app.Fragment
, not android.app.Fragment
.
Upvotes: 42