-2

Application Structure first:

//This class has a listview which will be populated from custom adapter

public class Transactions_show extends Activity implements OnItemClickListener

//This class is custom adapter which returns custom view for each row to be populated in above listview using sqlite database

public class CustomAdapter extends BaseAdapter

//Here all is working fine
//Now
//setting onitemclicklistener on each item of listview

public class Transactions_show extends ListActivity implements
    OnItemClickListener {

List<Transactions> all_transactions;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_transactions);

    DatabaseHandler db = new DatabaseHandler(getApplicationContext());

    all_transactions = db.get_all_transactions();
    size_of_transaction = all_transactions.size();

    ListView all_transactions_list = (ListView) findViewById(R.id.all_transaction_show_list);

    CustomAdapter adapter = new CustomAdapter(this, all_transactions);

    all_transactions_list.setAdapter(adapter);
    try {
        all_transactions_list.setOnItemClickListener(this);
    } catch (NullPointerException e) {
        Log.e("null pointer exception at item click", e.getMessage());
    }

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    Transactions single_transaction = new Transactions();
    single_transaction = all_transactions.get(position);

    Intent intent = new Intent(this, transacee_summary.class);
            int p_n = single_transaction.get_phone_number();
    Intent.putExtra("phone_number", p_n);
     startActivity(intent);
    /*
     * Toast toast = Toast.makeText(getApplicationContext(), "Item " +
     * (position + 1) + ": " +single_transaction.get_phone_number() ,
     * Toast.LENGTH_SHORT); toast.setGravity(Gravity.BOTTOM |
     * Gravity.CENTER_HORIZONTAL, 0, 0); toast.show();
     */
}

}

error is occuring here
Intent.putExtra("phone_number", p_n);
Which is : Cannot make a static reference to the non-static method putExtra(String, int) from the type Intent

Finding and trying on stack overflow for days and following google developers , decided to make a bundle as given in Passing a Bundle on startActivity()?

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
Transactions single_transaction = new Transactions();
single_transaction = all_transactions.get(position);

Intent intent = new Intent(this, transacee_summary.class);
int p_n = single_transaction.get_phone_number();
    Bundle extras = intent.getExtras();
    try {
        extras.putInt("phone_number", p_n);
    } catch (NullPointerException e) {
        Log.e("Null exception at putint", e.getMessage());
    }

    Intent.putExtra("phone_number", p_n);
     startActivity(intent);
}

Error removed but when application runs but crashes at extras.puInt ,although next activity starts well if don't pass this bundle.


So thought about taking complete custom view row and extract field of phone number

public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    Intent intent = new Intent(this, transacee_summary.class);
    TextView textv = (TextView) findViewById(R.id.phone_number_show);
    int p_n = Integer.parseInt( textv.getText().toString());

    Intent.putExtra("phone_number", p_n);
    startActivity(intent);
  }

now error comes again same.
What goes around comes around!

Community
  • 1
  • 1
devprashant
  • 1,285
  • 1
  • 13
  • 23

2 Answers2

2

It should probably be:

intent.putExtra("phone_number", p_n);

intent (the instance), not Intent (the class), since putExtra is indeed an instance method.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Problem is with this piece of code:

Intent intent = new Intent(this, transacee_summary.class);
        int p_n = single_transaction.get_phone_number();
Intent.putExtra("phone_number", p_n);

You need to replace Class Intent refrence with Object Intent refrence as putExtra is not a static method it is an instance method

Intent intent = new Intent(this, transacee_summary.class);
        int p_n = single_transaction.get_phone_number();
intent.putExtra("phone_number", p_n); // see starts with small i

Hope this helps.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33