I am trying to call another activity from an OnClickListener, but have encountered a NullPointerException. I used the suggestion provided at https://stackoverflow.com/a/7325248/1291619 to pass data to the other activity.
This class is intended to listen on a list and start VCardActivity.java with the selected uri when an item is clicked. It sends the uri along with the intent:
public class StaffListListener implements OnItemClickListener {
ArrayList<String> items;
Activity activity;
public StaffListListener(ArrayList<String> items, Activity activity) {
this.items = items;
this.activity = activity;
}
/**
* Send user to view with contact details
*/
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
//items.get(pos) returns the UPI needed. Append to http://www.cs.auckland.ac.nz/our_staff/vcard.php?upi=
Uri.Builder b = Uri.parse("http://www.cs.auckland.ac.nz/our_staff/vcard.php").buildUpon();
b.appendQueryParameter("upi", items.get(pos));
Uri uri = b.build();
Log.d("URL of staff", uri.toString());
Intent i = new Intent(activity.getApplicationContext(), VCardActivity.class);
i.putExtra("URI",uri);
activity.startActivity(i);
}
}
This class is intended to receive the intent, along with the uri, and parse the data and display it. I have not sorted out the data formatting and parsing yet, simply using Log as a placeholder:
public class VCardActivity extends ListActivity {
private VCardActivity local;
private String uri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vcard);
local = this;
Bundle extras = getIntent().getExtras();
if (extras != null) {
uri = extras.getString("URI");
}
try {
URL url = new URL(uri.toString());
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream stream = urlConnection.getInputStream();
StringBuffer fileContent = new StringBuffer("");
int ch;
while( (ch = stream.read()) != -1){
fileContent.append((char)ch);
}
String data = new String(fileContent);
Log.i("TAG", "data: " + data);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
My exception:
06-01 10:06:13.293: E/AndroidRuntime(903): FATAL EXCEPTION: main
06-01 10:06:13.293: E/AndroidRuntime(903): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lim.assignment/com.lim.json.VCardActivity}: java.lang.NullPointerException
06-01 10:06:13.293: E/AndroidRuntime(903): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
If anyone can tell me where I'm going wrong, would be much appreciated.
pass data via intent like that
Intent i = new Intent(getApplicationContext(), VCardActivity.class);
i.putExtra("URI",uri.toString());
startActivity(i);
and get data like that
Bundle extras = getIntent().getExtras();
if (extras != null) {
uri = extras.getString("URI");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.