0

我正在使用类似 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)

菜单活动类:

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>
4

1 回答 1

1

假设这条线是第 28 行MenuFragment

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

将其更改为

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

Activity有它自己的Context所以通过调用getActivity()你获得它Context

于 2013-07-25T20:14:02.067 回答