Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, May 3, 2016

null pointer exception android sunshine lesson 1

null pointer exception android sunshine lesson 1


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 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 weekForecast = new ArrayList(Arrays.asList(data));          forecastAdapter = new ArrayAdapter(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:

        

My list_item_forrecast.xml file

      

tried this solution, still not working

Logcat screenshot

Answer by Ritesh for null pointer exception android sunshine lesson 1


How your adapter will know which TextView to use?

Change your adapter to this..

forecastAdapter = new ArrayAdapter(getActivity(),R.layout.list_item_forecast,R.id.list_view_textview,weekForeCast);  

Answer by SaNtoRiaN for null pointer exception android sunshine lesson 1


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 adapter = new ArrayAdapter(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

  

Answer by Herin for null pointer exception android sunshine lesson 1


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.

Answer by Swayam for null pointer exception android sunshine lesson 1


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.

Answer by Karan Chudasama for null pointer exception android sunshine lesson 1


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


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.