简体   繁体   English

onItemClick,Intent,startActivity错误

[英]onItemClick, Intent, startActivity errors

My code: 我的代码:

package elf.app;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import elf.app.entity.ELFList;
import elf.app.entity.Entry;
import elf.app.test.FakeComm;

// TODO Kunna skicka att något är färdigt (ett rum är städat).

public class RoomListActivity extends ListActivity {
private ELFList eList;
//  private FakeComm fakecomm;
private Bundle extras;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.extras = getIntent().getExtras();
    eList = new ELFList();

//      fakecomm = new FakeComm();
//      eList.add(fakecomm.getData());

    String[] strArr = {"asd","sdf","dfg"};
    eList.add(strArr);

    String[] str = eList.returnNames();


    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, str));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Entry e = eList.getEntry(position);
            String roominfo = e.toString();


            Intent intent = new Intent(this, RoomInfoActivity.class);
            intent.putExtra("entry",roominfo);
            this.startActivity(intent);

                // old stuff
            // String message;
            // message = eList.getEntryInfo(position);
            // Toast.makeText(getApplicationContext(),
            // message, Toast.LENGTH_SHORT).show();
        }
    });
}

}

I'm getting errors at the following lines: 我在以下几行收到错误:

Intent intent = new Intent(this, RoomInfoActivity.class);

and

this.startActivity(intent);

I don't have much of a clue why I get these errors, the exact output in the editor for these errors are: 我没有太多线索为什么我得到这些错误,编辑器中针对这些错误的确切输出是:

  • "The constructor Intent(new AdapterView.OnItemClickListener(){}, Class ) is undefined" “构造函数Intent(new AdapterView.OnItemClickListener(){},Class)未定义”
  • "The method startActivity(Intent) is undefined for the type new AdapterView.OnItemClickListener(){}" “对于类型new AdapterView.OnItemClickListener(){},未定义方法startActivity(Intent)”

I'm an Android newbie so please take that into consideration, however I have studied Java for about a year. 我是一个Android新手,所以请考虑到这一点,但我已经研究了Java大约一年。

Fix 固定

Intent intent = new Intent(this, RoomInfoActivity.class);

to

Intent intent = new Intent(RoomListActivity.this, RoomInfoActivity.class);

The error is because by this you refer to OnClickListener. 该错误是因为您通过this引用OnClickListener。 The problem is fixed, if you refer to the Activity's this . 问题是固定的,如果你指的是活动的this The second error is the same - wrong reference. 第二个错误是相同的 - 错误的引用。 Just remove this , and startActivity() method will be searched within the enclosing class too. 只需删除this ,也将在封闭类中搜索startActivity()方法。

try this 尝试这个

Intent intent = new Intent(RoomListActivity.this, RoomInfoActivity.class);
intent.putExtra("entry",roominfo);
RoomListActivity.this.startActivity(intent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM