【问题标题】:InvocationTargetException: Caused by NullPointerExceptionInvocationTargetException:由 NullPointerException 引起
【发布时间】:2013-02-10 17:42:24
【问题描述】:

我的应用程序崩溃了。我有错误,我刚刚完成调试,正如标题所说,这就是问题所在。

在调试时我发现了导致问题的行:

listView.setAdapter(arrayAdapter2);

朝向底部。在here 之前我有一个类似的nullpointerException。这次不同了,因为我在正确的区域声明了我的 arrayadapter。在调试进入数组适配器的结果不是 NULL。

我已经搜索了论坛并找到了这些:

Why am I getting an InvocationTargetException? Android 2D game

another nullpointerexception

这些 NullPointerExceptions 在 android 中似乎很常见,但它们是特定于代码的。

在阅读其他信息时,如果我错了,请纠正我,唯一的空值可能是arrayAdapter2NULL

public class ByZipcode extends Activity{ 
Button btngetLObyzipcode;
Spinner spinner1;
ProgressBar progressBar1;
EditText textinput4byzip;
UserFunctions userFunctions  = new UserFunctions();
ArrayAdapter<String> arrayAdapter2;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.byzipcodepage);
    arrayAdapter2 = new ArrayAdapter<String>(ByZipcode.this,android.R.layout.simple_list_item_1);

    // Initializing spinner with predetermined results
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
    textinput4byzip = (EditText) findViewById(R.id.textinput4byzip);
    ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this,
            R.array.byzipspinner, android.R.layout.simple_spinner_item);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(spinnerAdapter);


    btngetLObyzipcode = (Button) findViewById(R.id.btngetLObyzipcode);
    btngetLObyzipcode.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (textinput4byzip.getText() != null & textinput4byzip.getText().toString().length() == 5 ){
                progressBar1.setVisibility(View.VISIBLE);
                new DownloadDataTask().execute();
                }
            }
    });

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main_screen, menu);
    return true;
}

private class DownloadDataTask extends AsyncTask<JSONArray, JSONArray, ArrayList<String> > {


        @Override
        protected ArrayList<String> doInBackground(JSONArray... params) {
            String spinValue = spinner1.getSelectedItem().toString();
            //if(textinput4byzip.getText() != null)
            JSONArray json = userFunctions.getCustomerbyZipCode((textinput4byzip.getText().toString()), spinValue);
            ArrayList<String> customers = new ArrayList<String>();
            for(int i=0; i < json.length() ; i++) {
                JSONObject jarray;
                try {
                    jarray = json.getJSONObject(i);
                    String zip = jarray.getString("CustomerName");
                    customers.add(zip);
                    Log.d(zip,"Output");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return customers;
        }   
        protected void onPostExecute(ArrayList<String> result){
            ListView listView = (ListView) findViewById(R.id.listView1);
            arrayAdapter2.addAll(result);
            listView.setAdapter(arrayAdapter2);
            progressBar1.setVisibility(View.GONE);
            Intent viewCustomers = new Intent(getApplicationContext(), StoreListView.class);
            viewCustomers.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(viewCustomers);
        }
 }

}

以防万一您有兴趣,失败如下:

02-10 17:39:36.976: E/AndroidRuntime(7686): FATAL EXCEPTION: main
02-10 17:39:36.976: E/AndroidRuntime(7686): java.lang.NullPointerException
02-10 17:39:36.976: E/AndroidRuntime(7686):     at     com.example.lo.ByZipcode$DownloadDataTask.onPostExecute(ByZipcode.java:93)
02-10 17:39:36.976: E/AndroidRuntime(7686):     at com.example.lo.ByZipcode$DownloadDataTask.onPostExecute(ByZipcode.java:1)
02-10 17:39:36.976: E/AndroidRuntime(7686):     at android.os.AsyncTask.finish(AsyncTask.java:631)
02-10 17:39:36.976: E/AndroidRuntime(7686):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-10 17:39:36.976: E/AndroidRuntime(7686):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-10 17:39:36.976: E/AndroidRuntime(7686):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 17:39:36.976: E/AndroidRuntime(7686):     at android.os.Looper.loop(Looper.java:137)
02-10 17:39:36.976: E/AndroidRuntime(7686):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-10 17:39:36.976: E/AndroidRuntime(7686):     at java.lang.reflect.Method.invokeNative(Native Method)
02-10 17:39:36.976: E/AndroidRuntime(7686):     at java.lang.reflect.Method.invoke(Method.java:511)
02-10 17:39:36.976: E/AndroidRuntime(7686):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-10 17:39:36.976: E/AndroidRuntime(7686):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-10 17:39:36.976: E/AndroidRuntime(7686):     at dalvik.system.NativeStart.main(Native Method)

根据上述活动的xml页面要求:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/textinput4byzip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|center_vertical"
    android:contentDescription="@string/zipcodefielddescription"
    android:ems="10"
    android:gravity="center_vertical|center_horizontal"
    android:inputType="number"
    android:maxLength="@integer/zipcodelength"
    android:textSize="@dimen/LargeFont" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    android:text="@string/entrzip"
    android:textAlignment="center"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="@dimen/MediumFont" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical|center_horizontal"
    android:orientation="vertical"
    android:paddingLeft="@dimen/MediumFont"
    android:paddingTop="@dimen/MediumFont" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="285dp"
        android:layout_height="308dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:visibility="gone" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:entries="@array/byzipspinner"
        android:textAlignment="center" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/restaurants"
        android:textSize="@dimen/LargeFont" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="CheckBox"
        android:textSize="@dimen/LargeFont" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="CheckBox"
        android:textSize="@dimen/LargeFont" />

    <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="CheckBox"
        android:textSize="@dimen/LargeFont" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical"
        android:paddingRight="@dimen/MediumFont" >

        <Button
            android:id="@+id/btngetLObyzipcode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/getlo"
            android:textSize="@dimen/LargeFont" />

    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • "这些 NullPointerExceptions 在 android 中似乎很常见,但它们是非常特定于代码的。" NullPointerExceptions 仅由代码中的错误引起。与安卓无关。 “在阅读其他信息时,如果我错了,请纠正我,null 的唯一位置是 arrayAdapter2 是否为 NULL。”错误的。 listView 也可以为空。 NPE 只是当您尝试访问尚未初始化的对象时引起的异常。在该行上放一个断点,看看哪一个是空的。您还应该在 onCreate() 中初始化您的 listView,而不是 onPostExecute()
  • 你能编辑你的帖子以显示byzipcodepage.xml吗?
  • 我添加了xml页面。我现在检查断点 Simon
  • listView 为 NULL,然后当我进入它时,它会直接进入异常。我打算将ListView listView = (ListView) findViewById(R.id.listView1); 转移到oncreate 和测试。

标签: java android nullpointerexception


【解决方案1】:
ListView listView = (ListView) findViewById(R.id.listView1);

您正在 byzipcodepage.xml 中查找 ID 为 listView1ListView。不存在这样的观点;事实上,那个布局中根本没有ListView

如果你想要那个视图中的列表(供适配器使用),你需要添加它;如果您的 ListView 在不同的活动中,您可能需要稍微不同地处理整个情况,以便将数据从该活动传递到该活动(可能通过 Bundle)。

【讨论】:

  • 你是 100% 正确的,这绝对是不同的观点。点击按钮后在下一页。我想我也别无选择,因为无论是这些结果还是按下按钮之前的选择答案,我都必须传递信息。如果您知道任何有关传递数据的教程,一定会很感激。感谢您的回答!
  • @Mark 您可以use a Bundle passed into your Intent,然后使用.putStringArray()Strings 数组(来自您当前在适配器中使用的List)传递给下一个Activity , 其中can then retrieve it.
  • 哇,听起来很简单。我喜欢人们用外行的话来表达它。我会试一试。今天是星期天我的编程日。
  • @Mark 祝你好运!如果您遇到困难,请随时开始另一个问题。 :)
猜你喜欢
  • 1970-01-01
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多