当前位置: 首页 > 知识库问答 >
问题:

尝试对空对象引用调用虚拟方法“…”

幸乐湛
2023-03-14

我的应用程序有问题,我想解决它,但我无法访问解决方案,请帮助我,,,

// Main_Activity Class

public class MainActivity extends Activity {

    Button Open_play_list;
    AccessPlayList accessPL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Open_play_list = (Button)findViewById(R.id.btnShA);
        accessPL = new AccessPlayList();

        Open_play_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Calling method tht contain code for open playlist
                try {
                    accessPL.openPlaylist();
                }`enter code here`
                catch (Exception ex) {
                    Toast.makeText(getBaseContext(), ex.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }

// AccessPlayList Class

public class AccessPlayList extends Activity {

    Intent intentPL;
    int REQUEST_CODE = 1;
    int PLAYLIST_ID = 2;

    public void openPlaylist()
    {
        intentPL = new Intent(Intent.ACTION_PICK);
        intentPL.setComponent(new ComponentName("com.android.music","com.android.music.PlaylistBrowserActivity"));
        intentPL.setType("MediaStore.Audio.Playlist.CONTENT_TYPE");
        intentPL.setFlags(0x10000000);
        intentPL.putExtra("oneShot",false);
        intentPL.putExtra("PlayList",PLAYLIST_ID);
        startActivityForResult(intentPL,REQUEST_CODE);
    }
}

共有1个答案

孙宏壮
2023-03-14

由于您没有公布代码的哪一行是发生错误的地方,所以我唯一能告诉您的是,您需要检查导致问题的对象是否为NULL。尝试进行以下验证:

if(object != null){
    //do something
}
 类似资料: