-1

I have a Class which when it executes, displays this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.onth/com.example.onth.Savedlocation}: java.lang.NullPointerException  

This is my code:

package com.example.onth;

import java.util.List;
import org.w3c.dom.Comment;
import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Savedlocation extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DatabaseHandler db = new DatabaseHandler(this);

           Log.d("Reading: ", "Reading all contacts..");
            List<locationprofl> loc = db.getAlllocation();  
            String log = " ";

            for (locationprofl cn : loc) {
                 log ="lat: "+cn.getlat()+" long:"+cn.getlong()+" ,Name: " + cn.getName() + " ,Profile: " + cn.getProfile();
                 Log.v("aaa", log);

            }
            final Spinner dropdown = (Spinner)findViewById(R.id.spinner1);
            String[] items = new String[]{"General", "Silent", "Outdoor"};
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
            dropdown.setAdapter(adapter);
            //addListenerOnButton();    

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.savedlocation, menu);
        return true;
    }

}
Blo
  • 11,903
  • 5
  • 45
  • 99
PranavPinarayi
  • 3,037
  • 5
  • 21
  • 32
  • post the complete log – Libin Apr 02 '14 at 01:49
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Feb 09 '15 at 20:04

3 Answers3

1

You are not setting the content to the activity. (setContentView missing).

final Spinner dropdown = (Spinner)findViewById(R.id.spinner1);  

dropdown will be null, since you have not set the view to the activity

Libin
  • 16,967
  • 7
  • 61
  • 83
0

The problem is that your spinner is null. You need to set a view for this activity, otherwise findViewById won't find your spinner.

Add setContentView(R.layout.yourLayoutFile); after super.onCreate(savedInstanceState);

dimasiu
  • 61
  • 4
0

You forgot to call setContentView();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location_saved); //You forgot this line
    DatabaseHandler db = new DatabaseHandler(this);
    ...  

}
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69