【问题标题】:Starting new intent开始新的意图
【发布时间】:2012-07-30 08:37:16
【问题描述】:

我正在尝试开始单击列表视图中的项目的新意图,但不知道如何使其正常工作。

代码如下:

final ListView lv = getListView();
lv.setTextFilterEnabled(true);  
lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")                  
            Intent intent = new Intent(this, Profileviewer.class);  
            startActivity(intent); 
        }
});

我在new Intent(this, Profileviewer.class); 上收到编译器错误

The constructor Intent(new AdapterView.OnItemClickListener(){}, Class<Profileviewer>) is undefined

【问题讨论】:

    标签: android listview android-intent


    【解决方案1】:

    您应该将活动的上下文传递给意图(通过输入YourActivity.this),通过仅传递this,您将传递AdapterView.OnItemClickListener()..

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")                  
            Intent intent = new Intent(YourActivity.this, Profileviewer.class);  
            startActivity(intent); 
        }
    });
    

    【讨论】:

    • 很好,现在它就像一个魅力,只需要再等 5 分钟才能接受这个遮阳篷!
    • 除了使用 YouActivity.this 你还可以使用 getBaseContext();这确实是使用该方法 afaik 的原因。
    • 为什么我们需要这个? : @SuppressWarnings("未选中")
    【解决方案2】:

    我想你忘了在 AndroidManifest.xml 中添加一个新活动。

    【讨论】:

      【解决方案3】:

      正如您所说,您正在尝试从 ListView 启动 Intent。在您的代码中,这意味着列表视图。这就是错误消息所说的。您需要使用以下任何一种方法来使用您的包名。

      1. Intent intent = new Intent({package_name}, Profileviewer.class);
      2. Intent intent = new Intent(Profileviewer.this, Profileviewer.class);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-19
        • 2017-08-09
        相关资源
        最近更新 更多