0

I am learning android through udacity android course. I am using android studio. I am stuck at a point where i'm supposed to see a listview with sample data instead i am having NUllPointerException and the app crashes every time.

MainActivity.java

package com.example.android.sunshine.app;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
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.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//import static com.example.android.sunshine.app.R.id.listView_forecast;


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 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.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);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    private ArrayAdapter<String> forecastAdapter;
    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        String[] data = {
                "Today-Sunny-88/63",
                "Tomorrow-Sunny-88/63",
                "Weds-Sunny-88/63",
                "Thurs-Sunny-88/63",
                "Fri-Sunny-88/63",
                "Sat-Sunny-88/63",
                "Sun-Sunny-88/63",
        };
        List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));
        forecastAdapter = new ArrayAdapter<String>(getActivity(),
                R.layout.list_item_forecast,
                weekForecast);

        ListView listView = (ListView) rootView.findViewById(R.id.listView_forecast);
        listView.setAdapter(forecastAdapter);
        return rootView;
    }
}
}

my Fragment_main.xml file:

<FrameLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.android.sunshine.app.MainActivity$PlaceholderFragment">


<ListView
    android:id="@+id/listView_forecast"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

My list_item_forrecast.xml file

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id="@+id/list_view_textview">
</TextView>

tried this solution, still not working

Logcat screenshot

Community
  • 1
  • 1

5 Answers5

0

How your adapter will know which TextView to use?

Change your adapter to this..

forecastAdapter = new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_view_textview,weekForeCast);
Ritt
  • 3,181
  • 3
  • 22
  • 51
0

Try this simple code for creating ListView. change your onCreate() method to the following

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    String[] data = new String[]{"Item1, Item2, Item3, Item4"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item_forecast, data);

    ListView lv = (ListView) findViewById(R.id.list);
    lv.setAdapter(adapter);
}

then delete that whole class PlaceholderFragment.
Inside your activity_main layout file add the following code

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
SaNtoRiaN
  • 2,212
  • 2
  • 15
  • 25
0

As per the code you have pasted above for the String data array. String[] data = { "Today-Sunny-88/63", "Tomorrow-Sunny-88/63", "Weds-Sunny-88/63", "Thurs-Sunny-88/63", "Fri-Sunny-88/63", "Sat-Sunny-88/63", "Sun-Sunny-88/63", };

you have forgot the remove the comma present on the last element.

it should be something like this

String[] data = { "Mon 6/23 - Sunny - 31/17", "Tue 6/24 - Foggy - 21/8", "Wed 6/25 - Cloudy - 22/17", "Thurs 6/26 - Rainy - 18/11", "Fri 6/27 - Foggy - 21/10", "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18", "Sun 6/29 - Sunny - 20/7" }; I couldn't found any other issue in the code,just try to correct the array and rerun it. Hope it helps.. cheers.

Herin
  • 141
  • 5
0

In the Fragment lifecycle, onCreateView() is called at a much earlier stage before the Fragment gets access to the Activity instance. It is only after onActivityCreated() is called, that the Fragment can use the context of the Activity.

Your problem is that you are using getActivity() inside onCreateView() to initialise the adapter. Move that to onActivityCreated() and things should be fine.

One general suggestion : Don't write too much logic inside onCreateView(). Simply inflate and return the view. Write most of the logic inside onActivityCreated(), because by then you know for sure that you have the Context of the Activity.

Swayam
  • 16,294
  • 14
  • 64
  • 102
  • This might be helpful as well : http://stackoverflow.com/questions/8041206/android-fragment-oncreateview-vs-onactivitycreated – Swayam Oct 23 '15 at 09:04
  • By onActivityCreated() do you mean onCreate() in MainActivity class? Actually i was following the tutorial and they had same code in their repository. – Prateek Khandelwal Oct 23 '15 at 14:58
  • onCreate is for Activities. onActivityCreated is for Fragments. Since, you were using a fragment, so I explained it in that context. Otherwise, you really don't need a fragment in this scenario. Just use an activity directly, as given in another answer. – Swayam Oct 23 '15 at 15:09
  • sorry for asking silly questions but i just started learning. I can't find onActivityCreated() in my code. – Prateek Khandelwal Oct 23 '15 at 15:22
  • You have to override it in your Fragment class. It's at the same level as the onCreateView method. You need to override it. And it's okay to ask questions. :) – Swayam Oct 23 '15 at 15:27
  • http://pastebin.com/BYGQQJDV Is this correct? Can you suggest good source for learning android – Prateek Khandelwal Oct 23 '15 at 16:39
  • nope!! i don't understand what i am doing wrong. java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.widget.ListView – Prateek Khandelwal Oct 23 '15 at 16:49
  • Apparently, findViewById is not able to reference the correct view. It is getting the parent of the list, which is a FrameLayout and trying to cast it into a List view. You can try to rebuild your project again, so that R.java file is created again. – Swayam Oct 23 '15 at 16:52
  • Also, I would recommend tutorials by Vogella or thenewboston for learning the basics of Android. – Swayam Oct 23 '15 at 16:53
  • Thank you so much for looking into my problem and helping me :) – Prateek Khandelwal Oct 23 '15 at 17:03
  • No problem. Glad it helped. :) – Swayam Oct 23 '15 at 17:22
0

I have tried this as shown in figure remove that line if it present.this you can find in DetailActivity.java (in android studio 1.5.1)

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Karan Chudasama
  • 196
  • 1
  • 2
  • 14