Question

In my listview there are to feilds,ward number and the date.i want to get these data to new activity when i'am clicking a listview.but in this there is a runtime error and i have no idea to how to fix it.

this is my dashboardActivity.java file,

public class DashboardActivity extends ListActivity implements FetchDataListener{

    UserFunctions userFunctions;
    Button btnLogout;
    TextView resultView;
    private ProgressDialog dialog;
    //private String email;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    /**
     * Dashboard Screen for the application
     * */        
    // Check login status in database
    userFunctions = new UserFunctions();
    if(userFunctions.isUserLoggedIn(getApplicationContext())){
        setContentView(R.layout.dashboard);

        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
              TextView txtviewWard= (TextView) view.findViewById(R.id.ward_number);
              String ward = txtviewWard.getText().toString();

              TextView txtviewDate= (TextView) view.findViewById(R.id.date_time);
              String dateTime = txtviewDate.getText().toString(); 

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

            }
          });

        btnLogout = (Button) findViewById(R.id.btnLogout);

        btnLogout.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                userFunctions.logoutUser(getApplicationContext());
                Intent login = new Intent(getApplicationContext(), LoginActivity.class);
                login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(login);
                // Closing dashboard screen
                finish();
            }
        });

    }else{
        // user is not logged in show login screen
        Intent login = new Intent(getApplicationContext(), LoginActivity.class);
        login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(login);
        // Closing dashboard screen
        finish();
        }
    }

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

    Intent intent = getIntent();
    String email = intent.getStringExtra(LoginActivity.EMAIL);


    String url = "http://pubbapp.comze.com/pubapp1.php?email=" + email;
    FetchDataTask task = new FetchDataTask(this);
    task.execute(url);


}


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


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 SingleListItem.java file,

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

    TextView txtWard = (TextView) findViewById(R.id.ward_number);
    TextView txtDate = (TextView) findViewById(R.id.date_time);

    Intent i = getIntent();
    // getting attached intent data
    String wardNumber = i.getStringExtra("ward");
    String dateTime = i.getStringExtra("dateTime");
    // displaying selected product name
    txtWard.setText(wardNumber);
    txtDate.setText(dateTime);

}

this is my xml file,

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

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

<TextView
    android:id="@+id/date_time"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Date"
    android:padding="10dip"
    android:textColor="#ffffff"
    android:textSize="20dip"
    android:textStyle="bold" />

</LinearLayout>

and this is the error in the LogCat,

12-02 19:10:01.100: E/AndroidRuntime(22705): FATAL EXCEPTION: main
12-02 19:10:01.100: E/AndroidRuntime(22705): java.lang.NullPointerException
12-02 19:10:01.100: E/AndroidRuntime(22705):    at com.example.androidhive.DashboardActivity$1.onItemClick(DashboardActivity.java:59)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at android.widget.ListView.performItemClick(ListView.java:3745)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1980)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at android.os.Handler.handleCallback(Handler.java:587)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at android.os.Looper.loop(Looper.java:130)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at android.app.ActivityThread.main(ActivityThread.java:3691)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at java.lang.reflect.Method.invokeNative(Native Method)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at java.lang.reflect.Method.invoke(Method.java:507)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at dalvik.system.NativeStart.main(Native Method)

please help me to fix this error. thank you.

Was it helpful?

Solution

In first activity, pass the data as a bundle using putExtras:

// sending data to new activity
Bundle b = new Bundle();
b.putString("ward", ward);
b.putString("dateTime", dateTime);
i.putExtras(b);   

In second activity, retrieve the bundle and parse the data as follows:

Bundle b = getIntent().getExtras();
if (b != null) 
{
    String wardNumber = b.getString("ward");
    String dateTime = b.getString("dateTime");
}

OTHER TIPS

try this....

   ListView lv = getListView();

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

       //your code

    }
  });

get data like this in ur second activity:

Bundle b = savedInstanceState.getExtras();
    if(b!=null)
    {
        String ward =b.getString("ward");
        String dateTime =b.getString("datetime");

    }

bundle is responsible to getexras(),so use this.it worked for me and my other friends.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top