【问题标题】:Android - The Method i(String, String) is undefined for the type LogAndroid - Log 类型的方法 i(String, String) 未定义
【发布时间】:2012-11-08 20:51:05
【问题描述】:

我的 Main.Activity.java 中不断出现此错误。

    The Method i(String, String) is undefined for the type Log

错误发生在这一行:

Log.i("HelloListView", "You clicked Item: " + id + " at position:" + position);

我不知道出了什么问题,所以这是我的整个 MainActivity.java。如果有人可以提供帮助,那就太好了。

    package com.example.uc.pf;

import org.apache.commons.logging.Log;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends Activity implements OnItemClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    //* *EDIT* * 
    ListView listview = (ListView) findViewById(R.id.listView1);
    listview.setOnItemClickListener(this);
}

public void onItemClick(AdapterView<?> l, View v, int position, long id) {
    Log.i("HelloListView", "You clicked Item: " + id + " at position:" + position);
        // Then you start a new Activity via Intent
        Intent intent = new Intent();
        intent.setClass(this, ListItemDetail.class);
        intent.putExtra("position", position);
        // Or / And
        intent.putExtra("id", id);
        startActivity(intent);
    }
}

【问题讨论】:

  • 以后,请复制并粘贴您的错误消息,而不是输入它们。我编辑了一个微妙的错字,暗示了您的问题的不同原因。
  • 我确实复制和粘贴了..?啊,我想没关系。谢谢,我想。
  • 这很奇怪......错误消息有log,而你说的导致错误的代码有Log。如果给出的答案不能完全解决您的问题,请仔细检查大小写。
  • @代码大师。实际上,您的编辑几乎掩盖了问题!
  • @Simon 这里可能存在不止一个问题,因为其他人指出了我最初错过的一个完全独立的问题。我进行了编辑和最初的评论,因为关于修复导入语句的答案被接受了。

标签: java android listview logging onitemclicklistener


【解决方案1】:

您正在导入org.apache.commons.logging.Log;。 换成android.util.Log

【讨论】:

  • "position" 和 "id" 都是整数,您必须使用 String.valueOf(id) + String.valueOf(position) 将其转换为字符串
  • 实际上,你没有。在这种情况下,Java 会自动执行此操作。
【解决方案2】:

您使用的日志记录功能似乎与原生 android 不同:android.util.Log。如果您决定使用 apache,那么使用 Log.info(java.lang.Object message, java.lang.Throwable t) 应该可以。

【讨论】:

    猜你喜欢
    • 2015-07-30
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多