Erro on Display Navigation Drawer in other activities? - android

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.

Related

Udacity's Sunshine-Version-2 app creating ForecastFragment error

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;

ActionBar problems to use libs

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

getSupportActionBar() returns null and no actionbar showing in activity

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.

Fragment add not working

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.

Android - adapter code does not compile

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

Categories

Resources