arr.getJSONObject(0).getString("borrower") 这句话分开写,打印以下具体值,不应该是set的问题,很可能是get有null 值。
JSONObject obj = arr.getJSONObject(0);
Log.d(...);
String str = obj.getString("borrower");
Log.d(...)

解决方案 »

  1.   


    我把这句放到回调的response里怎么就可以赋上值了,这是什么原因
    txtBorrower =  = (TextView) findViewById(R.id.txtBorrower);
      

  2.   

    这是什么赋值的写法?==不是判断是否等于么
    txtBorrower =  = (TextView) findViewById(R.id.txtBorrower);
      

  3.   


    就是把这句放到回调函数里赋值就没事了
    txtBorrower =  = (TextView) findViewById(R.id.txtBorrower);
    如果放到onCreate里报错内容为:
      

  4.   

    你确定能在onResponse方法里面能用findViewById()?
    把这一句拿出来分开写,看看有没有哪个是null
    arr.getJSONObject(0).getString("borrower")
      

  5.   


    我都调试通了 已经赋上值了,就是不知道为啥要放里面,如果初始化的时候findById总是null,我就不明白这块
      

  6.   

    在onCreate()里面findViewById肯定不会错的,应该是其他问题,你把错误时的代码、错误日志和现在正确的代码贴出来看看,行号也截进图去
      

  7.   


    package com.main.museumarchives;import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;import android.app.Activity;
    import android.app.ProgressDialog;
    import android.graphics.BitmapFactory;
    import android.graphics.Matrix;
    import android.os.Bundle;
    import android.os.Parcelable;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.util.DisplayMetrics;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.TranslateAnimation;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;import com.android.volley.Request;
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.VolleyError;
    import com.android.volley.toolbox.JsonObjectRequest;
    import com.android.volley.toolbox.Volley;
    import com.tools.ReadConfig;public class MainDetailActivity extends Activity {
    // ViewPager是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换。
    // android-support-v4.jar
    private ViewPager mPager;//页卡内容
    private List<View> listViews; // Tab页面列表
    private ImageView cursor;// 动画图片
    private TextView tabBorrowDeatil, tabBorrower, tabApproval;// 页卡头标
    private int offset = 0;// 动画图片偏移量
    private int currIndex = 0;// 当前页卡编号
    private int bmpW;// 动画图片宽度 private HashMap<String, String>session;
    private RequestQueue requestQueue; // 请求队列对象
    private ProgressDialog pd; private TextView txtObjID; // 借阅审批表主键
    private TextView txtBorrower; // 借阅人
    private TextView txtBorrowDate; // 借阅日期
    private TextView txtBorrowType; // 借阅方式
    private TextView txtBorrowRe; // 借阅说明

    private TextView txtOperator; // 经办人
    private TextView txtApprovalDate; // 审批时间
    private TextView txtApprovalRe; // 审批说明

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_detail);
    InitImageView();
    InitTabTextView();
    InitViewPager();
    requestQueue = Volley.newRequestQueue(this);
    session =  (HashMap<String, String>) this.getIntent()
    .getBundleExtra("session").getSerializable("sessionid");
    String username = session.get("username");
    String objid = session.get("objid");
    String descriptionfilesobjid = session.get("descriptionfilesobjid");
    String descriptionfilesdetailobjid = session.get("descriptionfilesdetailobjid");

    InitDate(username, objid, descriptionfilesobjid, descriptionfilesdetailobjid);
    } /**
     * 初始化头标
     */
    private void InitTabTextView() {
    tabBorrowDeatil = (TextView) findViewById(R.id.tabBorrowDeatil);
    tabBorrower = (TextView) findViewById(R.id.tabBorrower);
    tabApproval = (TextView) findViewById(R.id.tabApproval); tabBorrowDeatil.setOnClickListener(new MyOnClickListener(0));
    tabBorrower.setOnClickListener(new MyOnClickListener(1));
    tabApproval.setOnClickListener(new MyOnClickListener(2));
    }

    /**
     * 初始化各表头中的控件
     * */
    private void InitTextView(){
    // 借阅信息--控件
    txtObjID = (TextView) findViewById(R.id.txtObjID);
    txtBorrower = (TextView) findViewById(R.id.txtBorrower);
    txtBorrowDate = (TextView) findViewById(R.id.txtBorrowDate);
    txtBorrowType = (TextView) findViewById(R.id.txtBorrowType);
    txtBorrowRe = (TextView) findViewById(R.id.txtBorrowRe);
    // 审批信息--控件
    txtOperator = (TextView) findViewById(R.id.txtOperator);
    txtApprovalDate = (TextView) findViewById(R.id.txtApprovalDate);
    txtApprovalRe = (TextView) findViewById(R.id.txtApprovalRe);
    } /**
     * 初始化ViewPager
     */
    private void InitViewPager() {
    mPager = (ViewPager) findViewById(R.id.vPager);
    listViews = new ArrayList<View>();
    LayoutInflater mInflater = getLayoutInflater();
    listViews.add(mInflater.inflate(R.layout.main_detail_borrowdetail, null));
    listViews.add(mInflater.inflate(R.layout.main_detail_borrower, null));
    listViews.add(mInflater.inflate(R.layout.main_detail_approval, null));
    mPager.setAdapter(new MyPagerAdapter(listViews));
    mPager.setCurrentItem(0);
    mPager.setOnPageChangeListener(new MyOnPageChangeListener());
    } /**
     * 初始化动画
     */
    private void InitImageView() {
    cursor = (ImageView) findViewById(R.id.cursor);
    bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.decline)
    .getWidth();// 获取图片宽度
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int screenW = dm.widthPixels;// 获取分辨率宽度
    offset = (screenW / 3 - bmpW) / 2;// 计算偏移量
    Matrix matrix = new Matrix();
    matrix.postTranslate(offset, 0);
    cursor.setImageMatrix(matrix);// 设置动画初始位置
    } /**
     * 初始化数据
     * */
    private void InitDate(String username, String objid,String descriptionfilesobjid,String descriptionfilesdetailobjid){
    InitTextView();
    pd = ProgressDialog.show(MainDetailActivity.this, "", "数据加载中,请稍后……");

    // 审批页面赋值
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    txtOperator.setText(username.toString());
    txtApprovalDate.setText(sdf.format(new Date()));
    // 借阅信息页面赋值
    // 服务端验证
    String strUrl = ReadConfig.getPropertiesURL() + "/DASys/UI/WH_DescriptionApproval/AndroidDescriptionApprovalPrimaryKey.jsp?objid="+objid;
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
    Request.Method.GET, strUrl, 
    null, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
    // TODO Auto-generated method stub
    try {
    JSONObject result = new JSONObject(response.toString()); //转换为JSONObject
    JSONArray arr = result.getJSONArray("rows"); //获取JSONArray
    if(arr.length() > 0){
    txtObjID.setText(arr.getJSONObject(0).getString("objid"));
    txtBorrower.setText(arr.getJSONObject(0).getString("borrower"));
    txtBorrowDate.setText(arr.getJSONObject(0).getString("borrowstartdate") + " 至 " + arr.getJSONObject(0).getString("borrowenddate"));
    txtBorrowType.setText(arr.getJSONObject(0).getString("borrowtypename"));
    txtBorrowRe.setText(arr.getJSONObject(0).getString("borrowre"));

    pd.dismiss();// 关闭ProgressDialog
    }else{
    pd.dismiss();// 关闭ProgressDialog
    Toast.makeText(MainDetailActivity.this, "数据加载失败!", Toast.LENGTH_SHORT).show();
    }
    } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    pd.dismiss();// 关闭ProgressDialog
    Toast.makeText(MainDetailActivity.this, "数据加载失败!", Toast.LENGTH_SHORT).show();
    }
    }
    },
    new Response.ErrorListener(){
    @Override
    public void onErrorResponse(VolleyError error) {
    // TODO Auto-generated method stub
    pd.dismiss();// 关闭ProgressDialog
    Toast.makeText(MainDetailActivity.this, "数据加载失败!", Toast.LENGTH_SHORT).show();
    }
    });
    requestQueue.add(jsonObjectRequest);
    } /**
     * ViewPager适配器
     */
    public class MyPagerAdapter extends PagerAdapter {
    public List<View> mListViews; public MyPagerAdapter(List<View> mListViews) {
    this.mListViews = mListViews;
    } @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView(mListViews.get(arg1));
    } @Override
    public void finishUpdate(View arg0) {
    } @Override
    public int getCount() {
    return mListViews.size();
    } @Override
    public Object instantiateItem(View arg0, int arg1) {
    ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
    return mListViews.get(arg1);
    } @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == (arg1);
    } @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
    } @Override
    public Parcelable saveState() {
    return null;
    } @Override
    public void startUpdate(View arg0) {
    }
    } /**
     * 头标点击监听
     */
    public class MyOnClickListener implements View.OnClickListener {
    private int index = 0; public MyOnClickListener(int i) {
    index = i;
    } @Override
    public void onClick(View v) {
    mPager.setCurrentItem(index);
    }
    }; /**
     * 页卡切换监听
     */
    public class MyOnPageChangeListener implements OnPageChangeListener { int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
    int two = one * 2;// 页卡1 -> 页卡3 偏移量 @Override
    public void onPageSelected(int arg0) {
    Animation animation = null;
    /*switch (arg0) {
    case 0:
    if (currIndex == 1) {
    animation = new TranslateAnimation(one, 0, 0, 0);
    } else if (currIndex == 2) {
    animation = new TranslateAnimation(two, 0, 0, 0);
    }
    break;
    case 1:
    if (currIndex == 0) {
    animation = new TranslateAnimation(offset, one, 0, 0);
    } else if (currIndex == 2) {
    animation = new TranslateAnimation(two, one, 0, 0);
    }
    break;
    case 2:
    if (currIndex == 0) {
    animation = new TranslateAnimation(offset, two, 0, 0);
    } else if (currIndex == 1) {
    animation = new TranslateAnimation(one, two, 0, 0);
    }
    break;
    }*/
    animation = new TranslateAnimation(one*currIndex, one*arg0, 0, 0);
    currIndex = arg0;
    animation.setFillAfter(true);// True:图片停在动画结束位置
    animation.setDuration(300);
    cursor.startAnimation(animation);
    } @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    } @Override
    public void onPageScrollStateChanged(int arg0) {
    }
    }
    }
      

  8.   

    少写了一句
    InitTextView() 把这个方法放到onCreate()的InitDate();前面
      

  9.   


    我把这句放到回调的response里怎么就可以赋上值了,这是什么原因
    txtBorrower =  = (TextView) findViewById(R.id.txtBorrower);这个可能需要看你更多的代码,理论上说,你是在一个activity里调用的话,都应该是调用的当前activity的findViewById方法,find的是加载到当前window的view,只要是在setcontentview后调用,效果应该是一样的。
      

  10.   


    我把这句放到回调的response里怎么就可以赋上值了,这是什么原因
    txtBorrower =  = (TextView) findViewById(R.id.txtBorrower);这个可能需要看你更多的代码,理论上说,你是在一个activity里调用的话,都应该是调用的当前activity的findViewById方法,find的是加载到当前window的view,只要是在setcontentview后调用,效果应该是一样的。
    代码已经发了,帮忙看看
      

  11.   

    错误日志显示在151行空指针,说明你initData时那个TextView txtOperator还没初始化
      

  12.   

    或者是username没取到值,是null
      

  13.   

    话说你username本来就是String,还来个toString干啥
      

  14.   


    username是前面activity传过来的值,现在值都有,你能给我个邮箱吗?我做了个例子 你帮我瞧瞧,行不
      

  15.   

    应该把每个页面加载的控件放到对应的这里面;控件与页面同时初始化
    /**
             * 初始化ViewPager
             */
            private void InitViewPager() {}