更改Launcher活动时的意图错误

时间:2016-07-14 07:51:21

标签: android android-intent android-activity

我更改了我的启动器Activity,现在我收到了错误。首先,我导入了一个项目,而不是在开始时添加登录活动。 我在这里如何在登录活动中调用旧的启动器活动:

Intent intent = new Intent(Login.this,Main.class);
startActivity(intent);

在主Activity中我从这段代码中得到错误Ç

Intent intent = getIntent();

if(intent.getAction().equals(Intent.ACTION_GET_CONTENT)) {
    mReturnIntent = true;
    }

以下是错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at com.filemanager.pros.free.Main.onCreate(Main.java:182)


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.filemanager.pros.free/com.filemanager.pros.free.Main}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

我该怎么办?

编辑: OnCreate中:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //StartAppSDK.init(this, "201025739", true);

    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    /*read settings*/
    mSettings = getSharedPreferences(PREFS_NAME, 0);
    boolean hide = mSettings.getBoolean(PREFS_HIDDEN, true);
    boolean thumb = mSettings.getBoolean(PREFS_THUMBNAIL, true);
    int color = mSettings.getInt(PREFS_COLOR, -1);
    int sort = mSettings.getInt(PREFS_SORT, 2);

    mFileMag = new FileManager();
    mFileMag.setShowHiddenFiles(hide);
    mFileMag.setSortType(sort);

    if (savedInstanceState != null)
        mHandler = new EventHandler(Main.this, mFileMag, savedInstanceState.getString("location"));
    else
        mHandler = new EventHandler(Main.this, mFileMag);

    mHandler.setTextColor(color);
    mHandler.setShowThumbnails(thumb);
    mTable = mHandler.new TableRow();
    mGrid = mHandler.new GridRow();

    /*sets the ListAdapter for our ListActivity and
     *gives our EventHandler class the same adapter
     */
    mHandler.setListAdapter(mTable);
    setListAdapter(mTable);

    final GridView imagegrid = (GridView) findViewById(R.id.grid);
    imagegrid.setAdapter(mGrid);

    mHandler.setGridListAdapter(mGrid);

    registerForContextMenu(imagegrid);

    imagegrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            onListItemClick(null, v, position, id);
        }
    });

    imagegrid.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            mSelectedItem = position;
            imagegrid.showContextMenu();
            return true;
        }
    });

    /* register context menu for our list view */
    registerForContextMenu(getListView());

    mPathLabel = (TextView)findViewById(R.id.path_label);
    mPathLabel.setText(mHandler.getHomeDir());

    mHandler.setUpdateLabels(mPathLabel);

    /* setup buttons */
    int[] img_button_id = {R.id.back_button};

    int[] button_id = {R.id.help_button, R.id.memory_button};

    ImageButton[] bimg = new ImageButton[img_button_id.length];
    Button[] bt = new Button[button_id.length];

    for(int i = 0; i < img_button_id.length; i++) {
        bimg[i] = (ImageButton) findViewById(img_button_id[i]);
        bimg[i].setOnClickListener(mHandler);
    }

    for(int j = 0; j < button_id.length; j++) {
        bt[j] = (Button)findViewById(button_id[j]);
        bt[j].setOnClickListener(mHandler);
    }

    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.titlebar);

    View view = new View(this);
    view.setBackgroundColor(0xFF000000);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(1, relativeLayout.getLayoutParams().height - 35);
    layoutParams.addRule(RelativeLayout.LEFT_OF, R.id.memory_button);
    layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
    relativeLayout.addView(view, layoutParams);

    View view1 = new View(this);
    view1.setBackgroundColor(0xFF000000);
    RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(1, relativeLayout.getLayoutParams().height - 35);
    layoutParams1.addRule(RelativeLayout.LEFT_OF, R.id.help_button);
    layoutParams1.addRule(RelativeLayout.CENTER_VERTICAL);
    relativeLayout.addView(view1, layoutParams1);

    Intent intent = getIntent();

    if(intent.getAction().equals(Intent.ACTION_GET_CONTENT)) {
        mReturnIntent = true;

    } else if (intent.getAction().equals(ACTION_WIDGET)) {
        Log.e("MAIN", "Widget action, string = " + intent.getExtras().getString("folder"));
        mHandler.updateDirectory(mFileMag.getNextDir(intent.getExtras().getString("folder"), true));

    }

    //banner = (com.startapp.android.publish.banner.Banner) findViewById(R.id.startAppBanner);

    //LinearLayout bannerLayout = (LinearLayout) findViewById(R.id.bannerLayout);
    //bannerLayout.setOnClickListener(new View.OnClickListener() {
        //@Override
        //public void onClick(View v) {
        //  banner.hideBanner();
        //}
    //});

    _inst = this;
}

1 个答案:

答案 0 :(得分:1)

您的intent.getAction()在此处返回null。您需要添加空检查。你问,为什么它会返回null?好吧,请参考:https://stackoverflow.com/a/15049016/4747587