incompatible types
Required: import android.app.ActionBar;
Found: import android.support.v7.app.ActionBarActivity;
And why am I having to use #SuppressWarnings({"deprecation", "UnusedAssignment"}) for use extends ActionBarActivity.
package com.example.matheus.bars;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
#SuppressWarnings({"deprecation", "UnusedAssignment"})
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.app.ActionBar actionBar;
actionBar = getActionBar();
actionBar.setNavigationMode(actionBar.NAVIGATION_MODE_TABS);
android.app.ActionBar.Tab tab = actionBar.newTab();
tab.setText("Hello");
}
}
So there are a couple things going on here. If you are using the support Action bar you need this:
// use a different import
android.support.v7.app.ActionBar actionBar;
actionBar = getSupportActionBar();
Then as far as the navigation and tab goes, these methods are deprecated and not used any more.
http://developer.android.com/reference/android/app/ActionBar.html
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;
This question already has answers here:
Difference between android.app.Fragment and android.support.v4.app.Fragment
(6 answers)
Closed 7 years ago.
I going to add a fragment to an Activity . But its shows following problems . May be it's not compatible .Has there any solution ?
media/arifhasnat/1ED0E5663F78E3C1/
AjkerDeal/CustomNavigation/MyApplication/
app/src/main/java/navigationdrawer/arifhasnat
/com/androidcustomnavigationdrawer/
MainActivity.java:22: error: incompatible types:
FragmentOne cannot be converted to Fragment
fragmentTransaction.replace(R.id.frame_one, new FragmentOne()).commit();
Here my code: Its the Main Activity where i called Fragment class
package navigationdrawer.arifhasnat.com.androidcustomnavigationdrawer;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nav);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_one, new FragmentOne()).commit();
}
}
Fragment :
package navigationdrawer.arifhasnat.com.androidcustomnavigationdrawer;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by arifhasnat on 1/5/16.
*/
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment1,container,false);
return view;
}
}
Your FragmentOne extends from android.app.Fragment, while your MainActivity use the support library android.support.v4.app.Fragment.
Take a look at Difference between android.app.Fragment and android.support.v4.app.Fragment.
FragmentOne inherits from android.app.Fragment, apparent from the import declaration at the top of your class file:
import android.app.Fragment;
Conversely, your FragmentTransaction is from the support library:
import android.support.v4.app.FragmentTransaction;
The two types are incompatible. To resolve the issue, change the import declaration for FragmentOne so that it reads import android.support.v4.app.Fragment;
So Here the answer.
i have replace
import android.app.Fragment;
with
import android.support.v4.app.Fragment;
In the Class: where i have imported an wrong package library
package navigationdrawer.arifhasnat.com.androidcustomnavigationdrawer;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by arifhasnat on 1/5/16.
*/
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment1,container,false);
return view;
}
}
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.
I have this weird problem with the support actionbar in android.. Here's my activity:
public class TicketDetails extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.AppTheme_BlueGrey);
setContentView(R.layout.activity_ticket_details);
ActionBar actionBar = getSupportActionBar();//null in here
//some other code
}
//some other methods
}
And my theme is defined like this:
<style name="AppTheme.BlueGrey" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/blue_grey_500</item>
<item name="colorPrimaryDark">#color/blue_grey_700</item>
<item name="colorAccent">#color/blue_grey_300</item>
</style>
minSdk is set to 13 and I keep getting null in there. What could be the problem? My other activities work just fine but this one has no actionbar and getSupportActionBar returns null always. Tried calling it in the onStart or onResume but still the same.
Thank you!
[EDIT] This is the list with my imports:
package ga.adlabs.betticket.activities;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.widget.LinearLayout;
import com.mikepenz.google_material_typeface_library.GoogleMaterial;
import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.iconics.typeface.FontAwesome;
import java.util.ArrayList;
import ga.adlabs.betticket.R;
import ga.adlabs.betticket.fragments.FragmentDataProviderTicketDetails;
import ga.adlabs.betticket.fragments.FragmentTicketDetails;
import ga.adlabs.betticket.workers.AbstractDataProvider;
import ga.adlabs.betticket.workers.DataProviderTicketList;
You might created the ActionBar with Material Design (also called with Toolbar). Call this code after setContentView() method:
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar); // setting toolbar is important before calling getSupportActionBar()
ActionBar actionBar = getSupportActionBar();
Please learn more about designing the app with Material Design.
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.