java.lang.RuntimeException:无法在searchview中启动活动ComponentInfo



我知道这个问题已经被问了很多次,但似乎没有一个解决方案对我有效。请帮忙。

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="abhimanyu.example.com.seed" >
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.MyCompatTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchResultsActivity" />
        </activity>
        <activity
            android:name=".SearchResultsActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.MyCompatTheme">
            <!-- to identify this activity as "searchable" -->
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
    </application>
</manifest>

主要活动.java

package abhimanyu.example.com.seed;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import abhimanyu.example.com.seed.adater.CustomListAdapter;
import abhimanyu.example.com.seed.app.AppController;
import abhimanyu.example.com.seed.model.Movie;
public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getSimpleName();
// Movies json url
private static final String     url="http://api.androidhive.info/json/movies.json";  
private ProgressDialog pDialog;
private List<abhimanyu.example.com.seed.model.Movie> movieList = new          ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.list);
    adapter = new CustomListAdapter(this,movieList);
    listView.setAdapter(adapter);
    pDialog = new ProgressDialog(this);
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();
    // changing action bar color
    getActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#00897B")));
    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setTitle("Abhimanyu");
    getActionBar().setIcon(R.drawable.pp);

    // Creating volley request obj
    JsonArrayRequest movieReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                  //  hidePDialog();
                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject obj = response.getJSONObject(i);
                            Movie movie = new Movie();
                            movie.setTitle(obj.getString("title"));
                            movie.setThumbnailUrl(obj.getString("image"));
                            movie.setRating(((Number) obj.get("rating"))
                                    .doubleValue());
                            movie.setYear(obj.getInt("releaseYear"));
                            // Genre is json array
                            JSONArray genreArry = obj.getJSONArray("genre");
                            ArrayList<String> genre = new ArrayList<String>();
                            for (int j = 0; j < genreArry.length(); j++) {
                                genre.add((String) genreArry.get(j));
                            }
                            movie.setGenre(genre);
                            // adding movie to movies array
                            movieList.add(movie);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
      //      hidePDialog();
        }
    });
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(movieReq);
}
@Override
public void onDestroy() {
    super.onDestroy();
    //hidePDialog();
}
/*
    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }

/* public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        // Inflate menu to add items to action bar if it is present.
        inflater.inflate(R.menu.menu_main, menu);
        // Associate searchable configuration with the SearchView
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search_action).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        return true;
    }*/
}

搜索结果活动.java

package abhimanyu.example.com.seed;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
public class SearchResultsActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    handleIntent(getIntent());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.search_action).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));
    return true;
}
@Override
protected void onNewIntent(Intent intent) {
    handleIntent(intent);
}
private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        //use the query to search
    }
}
}

我是android开发的新手,请帮我解决这个例外。这是Logcat

12-18 15:57:19.319 6757-757/info.androidhive.customlistview截击E/AndroidRuntime:致命异常:main进程:info.androidhive.customlistviewvolleyPID:6757java.lang.NullPointerException在info.androidhive.customlistviewvolley.MainActivity.onCreateOptionsMenu(MainActivity.java:142)在android.app.Activity.onCreatePanelMenu(Activity.java:2538)在com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)在com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)在com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)在android.view.Cchoreographer$CallbackRecord.run(Choreographer.java:761)在android.view.Cchoreographer.doCallbacks(Choreographer.java:574)位于android.view.Cchoreographer.doFrame(Choreographer.java:543)在android.view.Cchoreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)在android.os.Handler.handleCallback(Handler.java:733)在android.os.Handler.dispatchMessage(Handler.java:95)在android.os.Looper.loop(Looper.java:136)在android.app.ActivityThread.main(ActivityThreads.java:5001)位于java.lang.reflect.Method.invokeNative(Native Method)位于java.lang.reflect.Method.ioke(Method.java:515)在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:601)在dalvik.system.NativeStart.main(本地方法)

目前尚不清楚活动中包含第142行的内容,但我猜您的searchView为null。searchView的空指针异常对我来说听起来很熟悉,因为我以前在片段的onCreateOptionsMenu 中遇到过它

  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main_menu, menu);
SearchManager searchManager =
        (SearchManager)getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo =
        searchManager.getSearchableInfo(getActivity().getComponentName());
searchItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo( searchableInfo);
.....

}

将我的menu_search中actionViewClass的命名空间从android更改为app
为我工作。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  tools:context="...">
  <item
android:id="@+id/menu_search"
android:title="@string/menu_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom">

相关内容

  • 没有找到相关文章

最新更新