Android:在onListItemClick运行时错误中启动一个意图

时间:2013-07-25 20:08:12

标签: android android-intent menu manifest

我正在制作一个带有facebook菜单的广播项目。当单击菜单上的第一项时,应用程序应该运行myMain类,在该类中我获得无线电流。 但我最近在LogCat上收到了这个错误:

07-25 22:52:12.843: E/AndroidRuntime(9553): FATAL EXCEPTION: main
07-25 22:52:12.843: E/AndroidRuntime(9553): java.lang.NullPointerException
07-25 22:52:12.843: E/AndroidRuntime(9553):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1374)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at android.app.Activity.startActivityForResult(Activity.java:2827)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:833)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at android.support.v4.app.Fragment.startActivity(Fragment.java:856)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at com.korovyansk.android.sample.slideout.MenuFragment.onListItemClick(MenuFragment.java:28)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at android.widget.ListView.performItemClick(ListView.java:3763)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1936)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at android.os.Handler.handleCallback(Handler.java:587)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at android.os.Looper.loop(Looper.java:123)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at android.app.ActivityThread.main(ActivityThread.java:3687)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at java.lang.reflect.Method.invokeNative(Native Method)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at java.lang.reflect.Method.invoke(Method.java:507)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-25 22:52:12.843: E/AndroidRuntime(9553):     at dalvik.system.NativeStart.main(Native Method)

MenuActivity类:

package com.korovyansk.android.sample.slideout;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;

import com.korovyansk.android.slideout.SlideoutHelper;

public class MenuActivity extends FragmentActivity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSlideoutHelper = new SlideoutHelper(this);
        mSlideoutHelper.activate();
        getSupportFragmentManager().beginTransaction().add(com.korovyansk.android.slideout.R.id.slideout_placeholder, new MenuFragment(), "menu").commit();
        mSlideoutHelper.open();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            mSlideoutHelper.close();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    public SlideoutHelper getSlideoutHelper(){
        return mSlideoutHelper;
    }

    private SlideoutHelper mSlideoutHelper;

}

MenuFragment类:

package com.korovyansk.android.sample.slideout;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MenuFragment extends ListFragment {

    Intent intent; 

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, new String[] { "Canlı Yayın", "Haberler", "Etkinlikler", " Fourth", " Fifth", " Sixth"}));
        getListView().setCacheColorHint(0);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        if(position == 1)
            intent = new Intent(getActivity().getApplicationContext(), myMain.class);
            startActivity(intent);
        ((MenuActivity)getActivity()).getSlideoutHelper().close();
    }


}

SampleActionBarActivity类:

package com.korovyansk.android.sample.slideout;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.MenuItem;
import android.widget.Toast;

import com.korovyansk.android.slideout.SlideoutActivity;

public class SampleActionbarActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
            finish();
        }
        setContentView(R.layout.sample_actionbar);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId() == android.R.id.home){

            int width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
            SlideoutActivity.prepare(SampleActionbarActivity.this, R.id.inner_content, width);
            startActivity(new Intent(SampleActionbarActivity.this, MenuActivity.class));
            overridePendingTransition(0, 0);
        }
        return true;
    }

}

SampleActivity类:

package com.korovyansk.android.sample.slideout;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;

import com.korovyansk.android.slideout.SlideoutActivity;

public class SampleActivity extends Activity {

    @TargetApi(11)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
            getActionBar().hide();
        }
        findViewById(R.id.sample_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
                        SlideoutActivity.prepare(SampleActivity.this, R.id.inner_content, width);
                        startActivity(new Intent(SampleActivity.this,
                                MenuActivity.class));
                        overridePendingTransition(0, 0);
                    }
                });
    }


}

因为错误与意图有关,所以这里是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.korovyansk.android.sample.slideout"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7"
        android:targetSdkVersion="11" />

    <application android:theme="@style/AppTheme"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="Simple Sample"
            android:name=".SampleActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="@string/app_name"
            android:name=".MenuActivity"
            android:theme="@style/MenuTheme" >
        </activity>
        <activity android:name=".SampleActionbarActivity"
            android:label="Sample With Actionbar">
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".myMain"
            android:label="@string/app_name">
        </activity>
    </application>
</manifest>

1 个答案:

答案 0 :(得分:1)

假设此行是MenuFragment

的第28行
intent = new Intent(getActivity().getApplicationContext(), myMain.class);

将其更改为

intent = new Intent(getActivity(), myMain.class);

Activity有自己的Context,因此致电getActivity()即可获得Context