在android編程中,我使用setOnItemClickListener現在拋出一個異常。我從來沒有遇到過任何麻煩,我沒有看到它在文檔中列出的任何例外。有人可以告訴我問題是什麼嗎?setOnItemClickListener拋出異常
package com.name.magicwindow;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MagicWindowActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView mainMenu;
try{
mainMenu = (ListView)findViewById(R.id.main_menu_list);
try{
mainMenu.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,View view,int position,long id)
{
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),Toast.LENGTH_SHORT).show();
}
});
}catch(Exception e)
{
Toast.makeText(getApplicationContext(), "could not set listener",Toast.LENGTH_LONG).show();
}
}catch(Exception e)
{
Toast.makeText(getApplicationContext(), "could not find listview",Toast.LENGTH_LONG).show();
}
setContentView(R.layout.main);
}
}
你能從logcat發佈stacktrace嗎? –
而不是隻是吐司,插入實際的日誌輸出,以便您可以看到發生了什麼問題。例如:Log.e(「MyActivity/outerTry。」,e.getMessage(),e); –