logeyg
logeyg

Reputation: 559

Android Application Crashing When Switching Activities

Upon clicking any item of a listview I want a new activity to start up. When I run this through the emulator my application crashes every time. I am positive my AndroidManifest is correct as well. Any help appreciated.

public static class SectionFragment extends ListFragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";
    private String dataArrayOne[];
    private String dataArrayTwo[];

    public SectionFragment() {
        dataArrayOne = new String[] {
                "Steven's Portfolio",
                "Sean's Portfolio",
                "Logan's Portfolio",
        };

        dataArrayTwo = new String[] {
                "GOOG",
                "YHOO",
                "AAPL",
                "MSFT"
        };
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        switch(getArguments().getInt(ARG_SECTION_NUMBER))
        {
        case 0:
            ListAdapter listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, dataArrayOne);
            setListAdapter(listAdapter);
            break;
        case 1:
            ListAdapter listAdapter1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, dataArrayTwo);
            setListAdapter(listAdapter1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main_dummy, container, false);
    }

    @Override
    public void onListItemClick(ListView list, View v, int position, long id)
    {
        Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(getActivity(), Portfolio_Select.class);
        intent.putExtra("USERNAME", getListView().getItemAtPosition(position).toString());
        startActivity(intent);

    }

AndroidManifest.xml

<activity
        android:name=".Portfolio_Select"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</activity>

Portfolio_Select Code

public class Portfolio_Select extends Activity {

    // variables go here

    TextView display;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.example.stockticker.R.layout.activity_main);



        display.setText("Stocks go here!");


    }


}

Upvotes: 0

Views: 526

Answers (1)

A--C
A--C

Reputation: 36449

TextView display;

is declared, but it always references null since findViewById() hasn't been used to give it anything else to hold.

This means when

display.setText("Stocks go here!");

is called, you'll get an NPE.

So use make sure that you have a TextView in R.layout.activity_main with an id, then use findViewById() to give display that TextView reference

setContentView(com.example.stockticker.R.layout.activity_main);

display = (TextView) findViewById (R.id.your_text_view_id);

display.setText("Stocks go here!");

If there are any other issues, add the stack trace to your question, so there's no other guessing involved.

Upvotes: 2

Related Questions