1
我想從parse.com獲得一列字符串,並使用ArrayAdapter將它們加載到ListView。 它似乎在日誌中我試圖加載的字符串數組是空的(然後寫入「storage = null」)。 我不明白是什麼問題。 我正在從MainActivity中解析字符串,然後用我之前創建的字符串數組初始化適配器。 在此先感謝。從Parse.com填充列表
MainActivity
public class MainActivity extends AppCompatActivity {
ListView list;
String[] itemname ;
String[] Strings;
Integer[] imgid={
R.drawable.one,
R.drawable.one,
R.drawable.one,
R.drawable.one,
R.drawable.one,
R.drawable.one,
R.drawable.one,
R.drawable.one,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Song");
query.orderByDescending("updatedAt");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> songs, ParseException e) {
if (e == null) {
Strings = new String[songs.size()-1];
for (int i = 0; i < songs.size()-1; i++) {
ParseObject object = songs.get(i);
String songName = object.getString("name");
Strings[i] = songName;
}
} else {
Log.d("error", "Error: " + e.getMessage());
}
}
});
CustomListAdapter adapter=new CustomListAdapter(this, Strings);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem= itemname[+position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
適配器
public class CustomListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] itemname;
public CustomListAdapter(Activity context, String[] itemname) {
super(context, R.layout.content_main, itemname);
this.context=context;
this.itemname=itemname;
}
public TextView extratxt;
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.line_single, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.Poet);
TextView dayView = (TextView) rowView.findViewById(R.id.Day);
extratxt = (TextView) rowView.findViewById(R.id.SongaName);
txtTitle.setText("hello");
dayView.setText("D");
extratxt.setText("Description " + itemname[position]);
return rowView;
};
}
日誌(錯誤)
java.lang.RuntimeException: Unable to start activity ComponentInfo{.MainActivity}: java.lang.NullPointerException: storage == null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2378)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.access$800(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5430)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
Caused by: java.lang.NullPointerException: storage == null
at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
at java.util.Arrays.asList(Arrays.java:155)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
at shirapp.israel.nir_zabari.shira.CustomListAdapter.<init>(CustomListAdapter.java:16)
at shirapp.israel.nir_zabari.shira.MainActivity.onCreate(MainActivity.java:62)
at android.app.Activity.performCreate(Activity.java:6097)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.access$800(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5430)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
嗨,聽起來很有道理。謝謝。 對於可能會看到此問題的人,您還需要更改適配器。 –
對不起,是從String []更改爲ArrayList。 –
V01D1