繁体   English   中英

将数据从非活动类传递到活动(通过意图)时发生NullPointerException

[英]NullPointerException when passing data from a non-activity class to an activity (via intent)

我正在尝试从OnClickListener调用另一个活动,但是遇到了NullPointerException。 我使用https://stackoverflow.com/a/7325248/1291619提供的建议将数据传递给其他活动。

此类旨在侦听列表,并在单击某项时使用选定的uri启动VCardActivity.java。 它发送uri和意图:

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

此类旨在与uri一起接收意图,并解析数据并显示它。 我还没有整理数据格式和解析,仅使用Log作为占位符即可:

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

我的例外:

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)

如果有人可以告诉我我要去哪里错了,将不胜感激。

这样通过意图传递数据

Intent i = new Intent(getApplicationContext(), VCardActivity.class);
        i.putExtra("URI",uri.toString());
       startActivity(i);

并像这样获取数据

 Bundle extras = getIntent().getExtras();
        if (extras != null) {
            uri = extras.getString("URI");
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM