2

I developed a simple listview application and it displays the listview but when I'm selecting a single listview I get the following error.

This is my single list item XML file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<TextView android:id="@+id/product_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25dip"
        android:textStyle="bold"
        android:padding="10dip"
        android:textColor="#ffffff"/>   
</LinearLayout>

this is my MainActivity.java file

import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.Toast;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity implements FetchDataListener{
private ProgressDialog dialog;

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

    ListView lv = getListView();

    // listening to single list item on click
    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

          // selected item
          String product = ((TextView) view).getText().toString();

          // Launching new Activity on selecting single List Item
          Intent i = new Intent(getApplicationContext(), SingleListItem.class);
          // sending data to new activity
          i.putExtra("product", product);
          startActivity(i);

      }
    });

}

private void initView() {
    // show progress dialog
    dialog = ProgressDialog.show(this, "", "Loading...");

    String url = "http://pubbapp.comze.com/pubapp.php";
    FetchDataTask task = new FetchDataTask(this);
    task.execute(url);
}

@Override
public void onFetchComplete(List<Application> data) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // create new adapter
    ApplicationAdapter adapter = new ApplicationAdapter(this, data);
    // set the adapter to list
    setListAdapter(adapter);        
}

@Override
public void onFetchFailure(String msg) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // show failure message
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();        
}
}

This is my second screen java file,

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SingleListItem extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.single_list_item_view);

    TextView txtProduct = (TextView) findViewById(R.id.product_label);

    Intent i = getIntent();
    // getting attached intent data
    String product = i.getStringExtra("product");
    // displaying selected product name
    txtProduct.setText(product);

}
}

I have no idea how to solve this error. Can someone please help me fix this?

2
  • what you have with ApplicationAdapter Commented Nov 26, 2013 at 7:15
  • check my answer -------- No need to pass getApplicationContext() in your intent .instead of this you may pass MainActivity.this in intent. Intent i = new Intent(MainActivity.this, SingleListItem.class); Commented Jul 16, 2014 at 10:43

4 Answers 4

10

as you are getting:

java.lang.ClassCastException: android.widget.RelativeLayout

because here:

      String product = ((TextView) view).getText().toString();

you are trying to cast ListView selected row view(RelativeLayout) to TextView. if you want to access TextView from selected row layout then do it as:

    TextView txtview = ((TextView) view.findViewById(R.id.your_textview_id));
    String product = txtview.getText().toString();
Sign up to request clarification or add additional context in comments.

3 Comments

i changed above code as u mention.but then i got another error.
this is my error. 11-26 15:48:14.860: E/AndroidRuntime(5570): FATAL EXCEPTION: main 11-26 15:48:14.860: E/AndroidRuntime(5570): java.lang.NullPointerException 11-26 15:48:14.860: E/AndroidRuntime(5570): at com.sj.jsondemo.MainActivity$1.onItemClick(MainActivity.java:35) 11-26 15:48:14.860: E/AndroidRuntime(5570): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 11-26 15:48:14.860: E/AndroidRuntime(5570): at android.widget.ListView.performItemClick(ListView.java:3745)
@user2881604: please update your code in question and also attach logs with question to get more help
5

Change

// selected item
          String product = ((TextView) view).getText().toString();

To

TextView txtview= (TextView) view.findViewById(R.id.product_label);
String product = txtview.getText().toString();

Comments

0

Right click on the project and select properties in list. Go to java build path move the Gen folder to up and src folder to down. Clean the project and run .That's work for me if it didn't tell me ?

Comments

0

if you are using custom xml file for ListItem then you can use your textview id which you have written in your xml file .replace these line with commented line

 //   String product = ((TextView) view).getText().toString();
 TextView txtview= (TextView) view.findViewById(R.id.your_textview_id);
 String product = txtview.getText().toString();

And if you are using android.R.layout.simple_list_item_1 in your adapter then you will have to set android.R.id.text1 resource id in your code. same for this case

 //   String product = ((TextView) view).getText().toString();
 TextView txtview= (TextView) view.findViewById(android.R.id.text1);
 String product = txtview.getText().toString();

for Eg.

 // listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {

      // selected item------------->replace below line with this
       // String product = ((TextView) view).getText().toString();
        TextView txtview= (TextView) view.findViewById(R.id.your_textview_id);
        String product = txtview.getText().toString();



      // Launching new Activity on selecting single List Item
      Intent i = new Intent(MainActivity.this, SingleListItem.class);
      // sending data to new activity
      i.putExtra("product", product);
      startActivity(i);

  }
});

1 Comment

No need to pass getApplicationContext() in your intent .instead of this you may pass MainActivity.this in intent. Intent i = new Intent(MainActivity.this, SingleListItem.class);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.