E
E
Ermalay2016-04-29 19:38:39
Android
Ermalay, 2016-04-29 19:38:39

Android. Intent not working?

The Activity has a list and a button at the bottom. By pressing the button, another Activity should open, but the application crashes. Where is the mistake? All code works as it should, except for this button. I'm a complete noob :(

import android.app.Activity;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;



public class TodosOverview extends ListActivity implements View.OnClickListener {
    private TodoDbAdapter dbHelper;
    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;
    private static final int DELETE_ID = Menu.FIRST + 1;
    private Cursor cursor;

    Button button;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.todo_list);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);

        this.getListView().setDividerHeight(2);
        dbHelper = new TodoDbAdapter(this);
        dbHelper.open();
        fillData();
        registerForContextMenu(getListView());
    }
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, MyActivity.class);
        startActivity(intent);
    }

    // Создаем меню, основанное на XML-файле
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.listmenu, menu);
        return true;
    }

    // Реакция на выбор меню
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.insert:
                createTodo();
                return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.insert:
                createTodo();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case DELETE_ID:
                AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
                        .getMenuInfo();
                dbHelper.deleteTodo(info.id);
                fillData();
                return true;
        }
        return super.onContextItemSelected(item);
    }

    private void createTodo() {
        Intent i = new Intent(this, TodoDetails.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, TodoDetails.class);
        i.putExtra(TodoDbAdapter.KEY_ROWID, id);
        // активити вернет результат если будет вызвано с помощью этого метода
        startActivityForResult(i, ACTIVITY_EDIT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();

    }

    private void fillData() {
        cursor = dbHelper.fetchAllTodos();
        startManagingCursor(cursor);

        String[] from = new String[] { TodoDbAdapter.KEY_SUMMARY };
        int[] to = new int[] { R.id.label };

        // Теперь создадим адаптер массива и установим его для отображения наших данных
        SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
                R.layout.todo_row, cursor, from, to);
        setListAdapter(notes);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dbHelper != null) {
            dbHelper.close();
        }
    }

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
VZVZ, 2016-04-29
@VZVZ

I'm a complete noob :(

So learn. Learn to look for an error not only by digging in the code, you also need to master debugging skills.
We need to learn how to use LogCat or at least try-catch+Exception.getMessage()+Toast.
Sit down right now and figure it out.
Without this, one can only guess.
Is it correct in the manifest? And what about MainActivity.onCreate and MainActivity in general? Maybe it's the ListActivity? Or maybe when you click some other code is called and leads to an error?
Etc. etc. from the simplest to the most incredible.

O
one pavel, 2016-04-29
@onepavel

MyActivity registered in the manifest?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question