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

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;

Related

Wrong 1st argument type. required "android.app.fragment"

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.

Erro on Display Navigation Drawer in other activities?

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.

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.

Which import is used for SurfaceHolder in SurfaceView.java

I copy the following code from Android's SurfaceView.java
I can see that SurfaceHolder is used in the SurfaceView.java:
public SurfaceHolder getHolder() {
return mSurfaceHolder;
}
However, I can't see which one of the following imports is used for SurfaceHolder. Can anyone explain to me . Thanks!
package android.view;
import com.android.internal.view.BaseIWindow;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.CompatibilityInfo.Translator;
import android.graphics.Canvas; import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Region;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.ParcelFileDescriptor;
import android.util.AttributeSet;
import android.util.Log;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;
your surface view is under package android.view;
and SurafaceHolder is also under same package so no import required :)
SurafceHolder derived form View
so you shoud use
import android.view.SurfaceHolder;

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