Android:简单的适配器空指针
Posted
技术标签:
【中文标题】Android:简单的适配器空指针【英文标题】:Android: simple adapter null pointer 【发布时间】:2019-02-25 14:28:34 【问题描述】:我有一个应用程序,我想使用 php Json 和 get 方法从我的服务器检索数据,我为此应用程序使用简单的适配器和 Json 下载器和 json 解析器,
我的简单适配器无法在我的 listView 中显示我的数据,
当我运行我的项目时,它会发生崩溃并显示一些错误。
这是主要活动:
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
set_bedehkariha_list();
public void set_bedehkariha_list()
JSONDownloader jsdl = new JSONDownloader();
String temp = jsdl.downloadURL( "http://famila1.ir/khabgah/get_khabgah_cash.php");
List<HashMap<String,Object>> cash = new ArrayList<HashMap<String, Object>>();
cashParser cashparse = new cashParser();
cash= cashparse.parse(temp);
String[] from = "hazine","tozihat","hazine_shareTo","who_paid","date";
int[] to = R.id.mablaq_hazine,R.id.babate_hazine,R.id.moshtarak_ba,R.id.pardakhtaz,R.id.tarikh_hazine;
SimpleAdapter myadapter = new SimpleAdapter(MainActivity.this,cash, R.layout.cash_list_row,from,to);
ListView lv = (ListView) findViewById(R.id.list_bedehkariha);
lv.setAdapter(myadapter);
这是 jsonDownloader:
public class JSONDownloader
public String downloadURL( String strUrl )
String data = "";
try
URL url = new URL( strUrl );
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
InputStream myStream = connection.getInputStream();
BufferedReader br = new BufferedReader( new InputStreamReader( myStream ) );
StringBuilder sb = new StringBuilder();
String line;
while ( ( line = br.readLine() ) != null )
sb.append( line );
data = sb.toString();
br.close();
connection.disconnect();
myStream.close();
catch ( Exception e )
/*
*Log.i( "MatiMessage" , "error in JSONDownloader in downloadURL() -> " + e.toString() );
*/
return data;
这是我的 JsonParser:
public List<HashMap<String , Object>> parse( String json )
List<HashMap<String , Object>> all_cash = new ArrayList<>();
try
JSONObject jObj = new JSONObject( json );
JSONArray jArr = jObj.getJSONArray( "cash" );
for( int i = 0; i < jArr.length(); i ++ )
JSONObject temp = jArr.getJSONObject( i );
HashMap<String , Object> cash = new HashMap<String , Object>();
cash.put( "id" , temp.getString( "id" ) );
cash.put( "member_id" , temp.getString( "member_id" ) );
cash.put( "hazine" , temp.getString( "hazine" ) );
cash.put( "date" , temp.getString( "date" ) );
cash.put( "hazine_shareTo" , "hazine_shareTo" );
cash.put( "tozihat" , temp.getString( "tozihat" ) );
cash.put( "who_paid" , temp.getString( "who_paid" ) );
all_cash.add( cash );
catch ( Exception e )
/*
* Log.i( "MatiMessage" , "error in cashParser in parse() -> " + e.toString() );
*/
return ( all_cash );
这是我在运行应用程序时在主要活动中的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.aqamamad.myapplication/com.example.aqamamad.myapplication.MainActivity:
java.lang.NullPointerException: 尝试调用虚方法 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' 空对象引用
【问题讨论】:
确保你的activity_main有这个id list_bedehkariha @KrishnaSharma 现在错误已解决,但列表为空且没有显示 你能确定List您需要在单独的线程上调用网络 Api。试试下面
class DownloadTask extends AsyncTask<Void, Void, String>
JSONDownloader downloader;
public DownloadTask()
downloader = new JSONDownloader();
@Override
protected String doInBackground(Void... voids)
return downloader.downloadURL("http://famila1.ir/khabgah/get_khabgah_cash.php");
@Override
protected void onPostExecute(String data)
super.onPostExecute(data);
cashParser cashparse = new cashParser();
List<HashMap<String,Object>> cash = cashparse.parse(data);
String[] from = "hazine","tozihat","hazine_shareTo","who_paid","date";
int[] to = R.id.mablaq_hazine,R.id.babate_hazine,R.id.moshtarak_ba,R.id.pardakhtaz,R.id.tarikh_hazine;
SimpleAdapter myadapter = new SimpleAdapter(MainActivity.this,cash, R.layout.cash_list_row,from,to);
ListView lv = (ListView) findViewById(R.id.list_bedehkariha);
lv.setAdapter(myadapter);
最后把下面的方法改成调用asyncTask。
public void set_bedehkariha_list()
new DownloadTask().execute();
【讨论】:
请分享堆栈跟踪。你是在无限期地调用任何方法吗?? 我没有 github 帐户,但我在我的个人网站上分享我的项目文件,这里是下载的直接链接:famila1.ir/MyApplication.zip 请查看我的应用程序 tnx @MohammadSeyedmahmudi 这是修复MyApplication @MohammadSeyedmahmudi 欢迎您。你能按预期投票并标记答案吗!【参考方案2】:您的代码包含
onCreate()
setContentView(R.layout.activity_main);
set_bedehkariha_list()
...
set_bedehkariha_list()
ListView lv = (ListView) findViewById(R.id.list_bedehkariha);
// nullpointerexception here
lv.setAdapter(myadapter);
这意味着 R.layout.activity_main 不包含 R.id.list_bedehkariha 或 set_bedehkariha_list() 在 onCreate() 之前调用
【讨论】:
我的错误已解决,但列表视图尚未显示任何内容以上是关于Android:简单的适配器空指针的主要内容,如果未能解决你的问题,请参考以下文章