显示java.lang.NullPointerException

时间:2017-03-01 03:36:05

标签: java android

单击buttonSearch以启动新的Intent时出现以下错误。

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

这是我目前正在运行的代码:

package com.example.desktop.finalyyearprojectv1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

public class RecipeGeneratorActivity extends AppCompatActivity implements View.OnClickListener {


private ArrayList<String> arrayList;
private ArrayAdapter<String> adapter;
private EditText txtInput;
private Button buttonClear;
private Button buttonSearch;
public String apiCode = "http://api.yummly.com/v1/api/recipes?_app_id=6e474ff5&_app_key=dc48611378c79997b39cf477b63f0270&q=";


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipe_generator);


    ListView listView = (ListView) findViewById(R.id.listView);

    final String[] items = {};
    arrayList = new ArrayList<>(Arrays.asList(items));
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.txtItem, arrayList);

    listView.setAdapter(adapter);
    txtInput = (EditText) findViewById(R.id.txtInput);

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


        @Override
        public void onClick(View v) {

            String newItem = txtInput.getText().toString();
            arrayList.add(newItem);
            adapter.notifyDataSetChanged();
            txtInput.setText("");

            StringBuilder listString = new StringBuilder();

            for (String s : arrayList)
                listString.append(s + "+");

            System.out.println(listString);

            apiCode = apiCode + listString;

            System.out.println(apiCode + "&maxResult=50&start=50");

        }
    });

使用上面的代码将项目添加到列表视图中显示的数组中,然后附加到API地址。

单击以下搜索按钮时发生错误。

    buttonSearch = (Button) findViewById(R.id.buttonSearch);
    buttonSearch.setOnClickListener(this);

    buttonClear = (Button) findViewById(R.id.buttonClear);
    buttonClear.setOnClickListener(this);


}




public void onClick(View view) {

    if (view == buttonSearch) {
        finish();
        startActivity(new Intent(this, RecipeFilterActivity.class));

    }

    else if (view == buttonClear) {

        arrayList.clear();
        adapter.notifyDataSetChanged();

        apiCode = "http://api.yummly.com/v1/api/recipes?_app_id=6e474ff5&_app_key=dc48611378c79997b39cf477b63f0270&q=";


    }

}
}

任何帮助将不胜感激! 感谢。

编辑:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.desktop.finalyyearprojectv1, PID: 31714
              java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.desktop.finalyyearprojectv1/com.example.desktop.finalyyearprojectv1.RecipeFilterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3094)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
                  at android.app.ActivityThread.access$1100(ActivityThread.java:222)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:158)
                  at android.app.ActivityThread.main(ActivityThread.java:7229)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                  at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:116)
                  at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
                  at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
                  at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:50)
                  at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
                  at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:199)
                  at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
                  at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
                  at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
                  at com.example.desktop.finalyyearprojectv1.RecipeFilterActivity.<init>(RecipeFilterActivity.java:82)
                  at java.lang.Class.newInstance(Native Method)
                  at android.app.Instrumentation.newActivity(Instrumentation.java:1095)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3084)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
                  at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:158) 
                  at android.app.ActivityThread.main(ActivityThread.java:7229) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
I/Process: Sending signal. PID: 31714 SIG: 9
Application terminated.

1 个答案:

答案 0 :(得分:0)

重写点击lisner,如

public void onClick(View view) {

if (view == buttonSearch) {

 startActivity(new Intent(this, RecipeFilterActivity.class));
 finish();

}
}