开发者

customize toast in android

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

LayoutInflater infator = getLayoutInflater()开发者_StackOverflow社区;
            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();


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜