我创建了一个名为spinner_fn()
的函数。但是当调用此函数时,它会显示NullPointerException
。所有值都来自数据库。有人可以帮我解决这个问题吗?
源代码
...
String result = DatabaseUtility.executeQueryPhp("initialName","");
System.out.print(result);
try
{
JSONArray JA = new JSONArray(result);
initial_name = new String[JA.length()];
initial_id = new String[JA.length()];
String initialname="";
String initialid="";
for(int i=0;i<JA.length();i++)
{
initialname = JA.getJSONObject(i).getString("initial_name");
initialid = JA.getJSONObject(i).getString("initial_id");
initialMap.put(initialname,initialid);
}
spinner_fn();
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
e.printStackTrace();
}
}
private void spinner_fn() {
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_spinner_item,
initial_name);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
initial.setAdapter(dataAdapter);
initial.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
Log.e("Position new", initial_name[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
logcat的
11-03 07:27:36.161: E/AndroidRuntime(2896): FATAL EXCEPTION: main
11-03 07:27:36.161: E/AndroidRuntime(2896): java.lang.NullPointerException
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.Spinner.onMeasure(Spinner.java:439)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.View.measure(View.java:15518)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1034)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.View.measure(View.java:15518)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.View.measure(View.java:15518)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1217)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.ScrollView.onMeasure(ScrollView.java:321)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.View.measure(View.java:15518)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.View.measure(View.java:15518)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.View.measure(View.java:15518)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.View.measure(View.java:15518)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.View.measure(View.java:15518)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-03 07:27:36.161: E/AndroidRuntime(2896): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.View.measure(View.java:15518)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.Choreographer.doFrame(Choreographer.java:532)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.os.Handler.handleCallback(Handler.java:725)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.os.Handler.dispatchMessage(Handler.java:92)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.os.Looper.loop(Looper.java:137)
11-03 07:27:36.161: E/AndroidRuntime(2896): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-03 07:27:36.161: E/AndroidRuntime(2896): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 07:27:36.161: E/AndroidRuntime(2896): at java.lang.reflect.Method.invoke(Method.java:511)
11-03 07:27:36.161: E/AndroidRuntime(2896): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-03 07:27:36.161: E/AndroidRuntime(2896): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-03 07:27:36.161: E/AndroidRuntime(2896): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
initial_name = new String[JA.length()];
...
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>( getApplicationContext(), android.R.layout.simple_spinner_item, initial_name);
您的initial_name
数组已满null
秒,因为您尚未在其中指定任何值。所有ArrayAdapter
数组元素都必须为非null。
猜猜你想改变
for(int i=0;i<JA.length();i++) { initialname = JA.getJSONObject(i).getString("initial_name");
类似
for(int i=0;i<JA.length();i++)
{
initial_name[i] = JA.getJSONObject(i).getString("initial_name");