2013-05-20 24 views
6

Tôi đang tự tìm hiểu một số phát triển ứng dụng Android và tôi đang làm theo hướng dẫn bằng video về cách tạo ứng dụng danh sách ToDo đơn giản. Tuy nhiên, tôi đã gặp phải một lỗi biên dịch và từ những kiến ​​thức hạn chế mà tôi có, mọi thứ dường như theo thứ tự. Dưới đây là tất cả những lớp học chính và cách bố trí:findViewById() đang trở về không thể tìm thấy biểu tượng, nhưng ID được xác định trong bố cục?

Main.java: http://pastebin.com/vfuANx0p

package com.example.exampletodo; 

import android.R; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnKeyListener; 
import android.widget.EditText; 
import android.widget.Button; 
import android.widget.ListView; 
import java.util.ArrayList; 
import android.widget.ArrayAdapter; 

public class Main extends Activity implements OnClickListener, OnKeyListener { 

    EditText txtItem; 
    Button btnAdd; 
    ListView listItems; 

    ArrayList<String> toDoItems; 
    ArrayAdapter<String> aa; 

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

     txtItem = (EditText)findViewById(R.id.txtItem); 
     btnAdd = (Button)findViewById(R.id.btnAdd); 
     listItems = (ListView)findViewById(R.id.listItems); 

     btnAdd.setOnClickListener(this); 
     txtItem.setOnKeyListener(this); 

     toDoItems = new ArrayList<String>(); 
     aa = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, toDoItems); 
     listItems.setAdapter(aa); 
    } 

    private void addItem(String item){ 
     if(item.length() > 0){ 
      this.toDoItems.add(item); 
      this.aa.notifyDataSetChanged(); 
      this.txtItem.setText(""); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public void onClick(View v){ 
     if(v == this.btnAdd){ 
      this.addItem(this.txtItem.getText().toString()); 
     } 
    } 

    public boolean onKey(View v, int keyCode, KeyEvent event){ 
     if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DPAD_CENTER){ 
      this.addItem(this.txtItem.getText().toString()); 
     } 
     return false; 
    } 

} 

main.xml: http://pastebin.com/WcWg692v

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".Main"> 

    <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/txtItem" android:layout_alignParentTop="true" 
      android:layout_alignParentRight="true" android:layout_alignParentLeft="true" android:focusable="true"/> 
    <Button 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/add" 
      android:id="@+id/btnAdd" android:layout_below="@+id/txtItem" android:layout_alignRight="@+id/txtItem" 
      android:layout_alignLeft="@+id/txtItem"/> 
    <ListView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/listItems" 
      android:layout_alignParentRight="true" android:layout_alignParentLeft="true" 
      android:layout_alignParentBottom="true" android:layout_below="@+id/btnAdd"/> 
</RelativeLayout> 

Lỗi này là:

/home/rohan/ExampleToDo/ExampleToDo/src/main/java/com/example/exampletodo/Main.java 
Gradle: cannot find symbol variable main Gradle: cannot find symbol 
variable txtItem Gradle: cannot find symbol variable btnAdd Gradle: 
cannot find symbol variable listItems Gradle: cannot find symbol 
variable main 

Cảm ơn bạn rất nhiều cho bất cứ ai là w để giúp đỡ!

+1

cố gắng làm sạch dự án trong menu Project> Clean ... và chọn một dự án Bạn gặp sự cố với – Gustek

Trả lời

12

Bạn import file R sai, bạn nhập android.R và bạn nên nhập package.R bạn

com.example.exampletodo.R 

EDIT: Và đừng quên sạch và xây dựng là đôi khi bắt buộc. Vì vậy, tốt hơn làm điều đó tự của bạn hơn tính vào tạo R tự động. Và bạn luôn có thể kiểm tra tệp R của mình cho dù tệp có chứa ID của bạn hay không.

phần 2:

thay đổi này

ArrayAdapter<String>(this, R.layout.simple_list_item_1, toDoItems); 

vào

ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, toDoItems); 

vì simple_list_item_1 là một phần của android.R không R của bạn

Hope this helps và tận hưởng công việc của bạn .

+0

Điều đó sẽ loại bỏ các lỗi được đề cập; cảm ơn! Tuy nhiên, nó tạo ra một cái mới với dòng 39 với simple_list_item_1: Gradle: không thể tìm thấy biến biểu tượng simple_list_item_1 – rohan32

+0

xem câu trả lời đã chỉnh sửa :) –

+0

Hoàn hảo, hoạt động tốt. Cảm ơn bạn rất nhiều! – rohan32

Các vấn đề liên quan