我的索引参数不起作用

时间:2016-04-23 10:53:39

标签: java android sqlite

我一直在寻找答案,但没有运气。恢复

我的按钮的ID是正确的 活动名称是正确的,但由于某种原因,Bundle返回null并崩溃我的应用程序,这是我的主要活动,请帮助。

检查OnClick侦听器我认为我使用的是正确的表单

主要活动

package com.example.carlos.application1;


        import android.widget.Button;
        import android.content.Context;
        import android.content.Intent;
        import android.support.v7.app.ActionBarActivity;
        import android.os.Bundle;

        import android.view.KeyEvent;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;

        import android.widget.AdapterView;
        import android.widget.ArrayAdapter;
        import android.widget.AdapterView.OnItemClickListener;
        import android.widget.ListView;

        import java.util.ArrayList;
        import java.util.List;

public class MainActivity extends ActionBarActivity {
    public final static String EXTRA_MESSAGE = "MESSAGE";
    private ListView obj;
    DBHelper mydb;
    int status = 0;
    Button hp_1, HP2, fo, previous, home, next,MZ,Control,show;

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

        mydb = new DBHelper(this);
        ArrayList array_list = mydb.getAllCotacts();
        ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);

        obj = (ListView)findViewById(R.id.listView1);
        obj.setAdapter(arrayAdapter);
        obj.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                int id_To_Search = arg2 + 1;

                Bundle dataBundle = new Bundle();
                dataBundle.putInt("id", id_To_Search);

                Intent intent = new Intent(getApplicationContext(), DisplayValues.class);

                intent.putExtras(dataBundle);
                startActivity(intent);
            }
        });
        hp_1.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                status = 1;
                Bundle bundle = new Bundle();
                bundle.putInt("status", status);

                Intent intent = new Intent(MainActivity.this, DisplayValues.class);
                intent.putExtras(bundle);

                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.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        super.onOptionsItemSelected(item);

        switch(item.getItemId())
        {
            case R.id.item1:Bundle dataBundle = new Bundle();
                dataBundle.putInt("id", 0);

                Intent intent = new Intent(getApplicationContext(),DisplayValues.class);
                intent.putExtras(dataBundle);

                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public boolean onKeyDown(int keycode, KeyEvent event) {
        if (keycode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
        }
        return super.onKeyDown(keycode, event);
    }
}

1 个答案:

答案 0 :(得分:1)

您尚未向hp_1分配任何内容,因此其值为NULL,这就是问题所在。您需要执行以下操作:

hp_1 = (Button) findViewById(...);
hp_1.setOnClickListener(new View.OnClickListener() {
...