从另一个班级调用一个意图

时间:2017-07-22 12:36:44

标签: java android class android-intent

我创建了一个单独的类来运行主活动的意图。当我试图运行它时,我得到一个java.lang.NullPointerException,我无法理解为什么。

主要活动:

SearchToolbar searchToolbar = new SearchToolbar(MainActivity.this, this, title);
searchToolbar.runToolbar();

SearchToolbar活动:     package com.example.android.redroom;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class SearchToolbar extends AppCompatActivity {

    private static final String BOOKS_REQUEST_BASE_URL = "https://www.googleapis.com/books/v1/volumes?q=";
    private Activity activity;
    private Context context;
    private String title;
    private TextView generalTitleView;
    private EditText searchView;
    private String searchWord;
    private String finalURL;

    public SearchToolbar(Activity activity,  Context context, String title) {
        this.activity = activity;
        this.context = context;
        this.title = title;
    }

    public void runToolbar() {

        //Set current fragment title
        generalTitleView = (TextView) activity.findViewById(R.id.general_title);
        generalTitleView.setText(title);

        //Search editor on enter listener
        searchView = (EditText) activity.findViewById(R.id.search_view);
        searchView.setVisibility(View.GONE);
        searchView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == EditorInfo.IME_ACTION_DONE) {

                    StringBuilder requestUrl = new StringBuilder(BOOKS_REQUEST_BASE_URL);
                    searchWord = searchView.getText().toString();
                    requestUrl.append(searchWord);
                    requestUrl.append("&maxResults=10");
                    finalURL = requestUrl.toString();

                    Intent goToSearchActivity = new Intent(context, ActivitySearch.class);
                    goToSearchActivity.putExtra("requestURL", finalURL);
                    goToSearchActivity.putExtra("searchWord", searchWord);
                    startActivity(goToSearchActivity);
                }
                return false;
            }
        });

        //Toolbar on click listener
        ImageView searchBtn = (ImageView) activity.findViewById(R.id.search_btn);
        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchView.setVisibility(View.VISIBLE);
                generalTitleView.setVisibility(View.GONE);
            }
        });
    }
}

错误:

FATAL EXCEPTION: main
                                                                             Process: com.example.android.redroom, PID: 11174
                                                                             java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
                                                                                 at android.app.Activity.startActivityForResult(Activity.java:3967)
                                                                                 at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
                                                                                 at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
                                                                                 at android.app.Activity.startActivityForResult(Activity.java:3928)
                                                                                 at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
                                                                                 at android.app.Activity.startActivity(Activity.java:4251)
                                                                                 at android.app.Activity.startActivity(Activity.java:4219)
                                                                                 at com.example.android.redroom.SearchToolbar$1.onEditorAction(SearchToolbar.java:64)
                                                                                 at android.widget.TextView.onEditorAction(TextView.java:4773)
                                                                                 at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:139)
                                                                                 at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:304)
                                                                                 at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:152)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5497)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:1)

就这样做

 Intent goToSearchActivity = new Intent(context, ActivitySearch.class);
                goToSearchActivity.putExtra("requestURL", finalURL);
                goToSearchActivity.putExtra("searchWord", searchWord);
                context.startActivity(goToSearchActivity);