3

Iam trying to changebackground and text color of toast in my application using

LayoutInflater infator = getLayoutInflater();
            View layout =infator.inflate(R.layout.toast, (ViewGroup) findViewById(R.id.toast_layout));
            TextView toastText = (TextView) findViewById(R.id.toasttext);
            toastText.setBackgroundColor(Color.YELLOW);
            toastText.setText("uei:"+o.getUei());
            Toast toast = new Toast(getApplicationContext());
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();

This code is present in OnItemClickListener of my listview.

But the application gets force closed with null pointer exception!!

this is what abve the onclick listener, which could give a better picture.

public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alarms);
        m_alarmAdapter = new AlarmAdapter(this, R.layout.severity_item, m_alarms);
        setListAdapter(m_alarmAdapter);
        ListView lv = getListView();
                lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
                Log.d(TAG, "clicked: " + view);
                final Alarm o = m_alarmAdapter.getItem(position);
                LayoutInflater infator = getLayoutInflater();
TechnocraT
  • 313
  • 2
  • 6
  • 22
  • Brief error report in LogCat 08-25 13:25:14.672: ERROR/AndroidRuntime(2879): java.lang.NullPointerException 08-25 13:25:14.672: ERROR/AndroidRuntime(2879): at com.opennms.android.AlarmActivity$2.onItemClick(AlarmActivity.java:65) – TechnocraT Aug 25 '11 at 08:00
  • Have you called `setContentView` before `findViewById`? – MByD Aug 25 '11 at 08:02
  • @Zeisss line 65 is toastText.setBackgroundcolor... @MByD I set the content view of other alarm.xml file immediately in `onCreate()` method – TechnocraT Aug 25 '11 at 08:11
  • @technocrat: Then toastText is null, thus findViewById() returns null, so the `toasttext` seems to be not part of your content view. – ZeissS Aug 25 '11 at 09:04
  • @Zeisss yah it looks so but, i have specified textview with id toasttext in toast.xml.Also iam using inflator to get the toast.xml. What could be the solution to it then?? – TechnocraT Aug 25 '11 at 10:44

1 Answers1

1

You are asking your current view to look for the toasttext id, not the View you just loaded. Try layout.findViewById()

ZeissS
  • 11,867
  • 4
  • 35
  • 50