Android: 简单适配器空指针
Android: simple adapter null pointer
我有一个应用程序,我想使用 PHP Json 和获取方法从我的服务器检索数据,我使用简单的适配器和 Json 下载器以及 json此应用程序的解析器,
我的简单适配器在我的 listView 中显示我的数据时遇到问题,
当我 运行 我的项目发生崩溃并显示一些错误时。
这是主要的 activity:
@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);
}
这是json下载者:
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;
}
这是我的Json解析器:
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 );
}
这是我在 activity 中 运行 应用程序时的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aqamamad.myapplication/com.example.aqamamad.myapplication.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a
null object reference
您的代码包含
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()
之前调用
您需要在单独的线程上调用网络 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();
}
我有一个应用程序,我想使用 PHP Json 和获取方法从我的服务器检索数据,我使用简单的适配器和 Json 下载器以及 json此应用程序的解析器,
我的简单适配器在我的 listView 中显示我的数据时遇到问题,
当我 运行 我的项目发生崩溃并显示一些错误时。
这是主要的 activity:
@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);
}
这是json下载者:
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;
}
这是我的Json解析器:
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 );
}
这是我在 activity 中 运行 应用程序时的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aqamamad.myapplication/com.example.aqamamad.myapplication.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
您的代码包含
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()
之前调用您需要在单独的线程上调用网络 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();
}