只需开始一项新活动

时间:2013-09-22 17:41:25

标签: android

我只是想要一个按钮,当点击它时,会将活动更改为另一个带有更多按钮的屏幕。

<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。在这里提出同样问题的每个帖子似乎都忘记在清单文件中发布intent-filter,但我已经这样做了。

logcat的:

    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

3 个答案:

答案 0 :(得分:0)

你需要上下文,

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

“事情”可能是一种东西,等等。

答案 1 :(得分:0)

NPE来自这条线:

getActionBar().setDisplayHomeAsUpEnabled(true);

所以我想你的Activity中没有ActionBar。

答案 2 :(得分:0)

错误在线

getActionBar().setDisplayHomeAsUpEnabled(true);

扩展Activity类但是如果要使用getActionBar()

则需要扩展ActionBarActivity