【问题标题】:Attempt to invoke virtual method '...' on null object Reference尝试在空对象引用上调用虚拟方法“...”
【发布时间】:2015-04-22 05:14:58
【问题描述】:

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

// 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);
    }
}

【问题讨论】:

  • 请发布异常堆栈跟踪并指出它指的是哪一行。
  • 与论坛网站不同,我们不使用“谢谢”、“任何帮助表示赞赏”或Stack Overflow 上的签名。请参阅"Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?"。顺便说一句,这是“提前致谢”,而不是“在此先致谢”。此外,您实际上并没有告诉我们什么您的问题是什么,也没有解释出了什么问题。

标签: android nullpointerexception


【解决方案1】:

由于您没有发布发生错误的代码行,所以我唯一可以告诉您的是,您需要检查导致问题的对象是否为空。 试试这样的验证:

if(object != null){
    //do something
}

【讨论】:

    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 2019-01-10
    • 2019-05-10
    • 2015-06-26
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多