我想在我的應用程序中使用xzx更改背景和文本顏色xzxx 在android中自定義吐司
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();
此代碼存在於我的listview的OnItemClickListener中。
但應用程序因空指針異常而被強制關閉!!
這就是onclick監聽器,它可以提供更好的畫面。
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();
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
哪一行是65行? – ZeissS
你在'findViewById'之前調用過'setContentView'嗎? – MByD