0

我只是想拥有一个按钮,单击该按钮时,会将活动更改为具有更多按钮的另一个屏幕。

<ImageButton
            android:id="@+id/options"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/basketball"
            android:src="@drawable/options_t"
            android:onClick="hitoptions"
            android:textSize="15sp" />

public void hitoptions(View button){
        startActivity( new Intent("android.intent.action.OPTIONSMENU"));
    }

<activity
    android:name="com.example.mediaplayerdownloaderservice.OptionsMenu"
    android:label="@string/title_activity_options_menu"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:parentActivityName="com.example.MediaPlayerDownloaderService.MainActivity" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.MediaPlayerDownloaderService.MainActivity" />
    <intent-filter> 
        <action android:name="android.intent.action.OPTIONSMENU" />  
        <category android:name="android.intent.category.DEFAULT" />  
    </intent-filter> 
</activity>

package com.example.mediaplayerdownloaderservice;

import java.util.List;

import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.support.v4.app.NavUtils;

public class OptionsMenu extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_options_menu);
        setupActionBar();
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    private void setupActionBar() {

        getActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.options_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

NullPointerException单击选项按钮时我得到一个。此处询问相同问题的每个帖子似乎都忘记在清单文件中发布意图过滤器,但我已经做到了。

日志猫:

    09-22 13:29:13.460: E/AndroidRuntime(768): FATAL EXCEPTION: main
    09-22 13:29:13.460: E/AndroidRuntime(768): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mediaplayerdownloaderservice/com.example.mediaplayerdownloaderservice.OptionsMenu}: java.lang.NullPointerException
    09-22 13:29:13.460: E/AndroidRuntime(768):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at android.os.Handler.dispatchMessage(Handler.java:99)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at android.os.Looper.loop(Looper.java:137)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at android.app.ActivityThread.main(ActivityThread.java:5103)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at java.lang.reflect.Method.invokeNative(Native Method)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at java.lang.reflect.Method.invoke(Method.java:525)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at dalvik.system.NativeStart.main(Native Method)
    09-22 13:29:13.460: E/AndroidRuntime(768): Caused by: java.lang.NullPointerException
    09-22 13:29:13.460: E/AndroidRuntime(768):  at com.example.mediaplayerdownloaderservice.OptionsMenu.setupActionBar(OptionsMenu.java:33)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at com.example.mediaplayerdownloaderservice.OptionsMenu.onCreate(OptionsMenu.java:27)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at android.app.Activity.performCreate(Activity.java:5133)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    09-22 13:29:13.460: E/AndroidRuntime(768):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
    09-22 13:29:13.460: E/AndroidRuntime(768):  ... 11 more
4

3 回答 3

0

NPE 来自这一行:

getActionBar().setDisplayHomeAsUpEnabled(true);

所以我想你的活动中没有 ActionBar。

于 2013-09-22T18:02:20.073 回答
0

你需要上下文,

Intent intent = new Intent(this,"Thing")
startActivity(intent)

“事物”可以是一个东西。类,等等。

于 2013-09-22T17:58:47.293 回答
0

错误在线

getActionBar().setDisplayHomeAsUpEnabled(true);

您扩展了 Activity 类,但如果要使用 getActionBar() 则需要扩展 ActionBarActivity

于 2013-09-22T18:08:16.707 回答