我正在尝试从parse.com获取一列Strings并使用ArrayAdapter将它们加载到ListView。 在日志中我想要加载的String数组似乎是空的(然后编写" storage = null")。 我不明白是什么问题。 我在MainActivity中从parse加载字符串,然后使用我之前创建的String数组初始化适配器。 提前谢谢。
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)
答案 0 :(得分:2)
您收到该错误是因为Strings数组在传递给适配器时为null。
我将字符串从String []更改为ArrayList并在onCreate中将其即时播放,然后将新歌曲添加到列表调用adapter.notifyDataSetChanged()以让适配器知道新项目已添加到列表中并刷新它
这是更新的课程:
public class MainActivity extends AppCompatActivity {
ListView list;
CustomListAdapter adapter;
String[] itemname ;
ArrayList<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);
Strings = new ArrayList();
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) {
for (int i = 0; i < songs.size() - 1; i++) {
ParseObject object = songs.get(i);
String songName = object.getString("name");
Strings.add(songName);
adapter.notifyDataSetChanged();
}
} else {
Log.d("error", "Error: " + e.getMessage());
}
}
});
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);
}
}