android开头怪异的错误

时间:2013-12-01 14:11:08

标签: java android android-listview

package com.example.medapp;

public class MainActivity extends Activity {
public final static String apts = "com.example.medapp.editapts";
static final int PICK_DATE_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void open(View view) {
    // Do something in response to button
    DatePicker dp = (DatePicker)findViewById(R.id.datePicker1);     
    int date = dp.getYear() + dp.getMonth() + dp.getDayOfMonth();

    //load DATABASE

    //start showing the list
    final ListView listview = (ListView) findViewById(R.id.listView1);
    String[] values = new String[] { "apt1", "apt2", "apt3",
        "apt4", "apt5", "apt6", "apt7", "apt8" };
    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < values.length; ++i) {
      list.add(values[i]);
    }
    final StableArrayAdapter adapter = new StableArrayAdapter(this,
            android.R.layout.simple_list_item_1, list);
    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, final View view,
          int position, long id) {
        final String item = (String) parent.getItemAtPosition(position);
        view.animate().setDuration(2000).alpha(0)
            .withEndAction(new Runnable() {
              @Override
              public void run() {
                Intent intent = new Intent(view.getContext(),editapts.class);
                intent.putExtra(apts,item);
                adapter.notifyDataSetChanged();
                view.setAlpha(1);
                startActivity(intent);
              }
            });
      }

    });
}

@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;
}
你好 我的代码有一些奇怪的问题 它说

Multiple markers at this line
    - button1 cannot be resolved
    - overrides 
     android.app.Activity.onCreate

代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

之前完全正常(button1是执行动作的触发器) 创建文件时提供了错误部分 请问任何提示? android noob here:s 谢谢 :) 我已经干净并重建了所有东西但仍然没有工作:(

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <DatePicker
        android:id="@+id/datePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="19dp" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/datePicker1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="20dp" >

    </ListView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/datePicker1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="34dp"
        android:text="Appointments" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/datePicker1"
        android:layout_alignBottom="@+id/datePicker1"
        android:layout_alignRight="@+id/listView1"
        android:layout_marginRight="36dp"
        android:onClick="open"
        android:text="Open" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

删除

android:onClick="open"
从您的Xml

。并尝试使用OnCreate方法

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {           

    @Override
    public void onClick(View v) 
    {
      // do whatever you are doing in open method 
    }    
 }
}

基本上你的代码和这个编辑是一样的,但肯定会出错。希望它有所帮助。