简体   繁体   中英

android weird error at the beginning

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;
}

Hi there I have some weird problem about my code it says

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

on code

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

which was totally fine before (button1 is the trigger which do the acts) the error part was provided when the file created any tip please? android noob here :s Thank you :) I already did clean and rebuild everything and still not working :(

<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>

Remove

android:onClick="open"

from your Xml . And try this into your OnCreate method

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 
    }    
 }
}

Basically your code and this edit is same , but certainly something wrong going on. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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