Android表达式期望JsonObject

时间:2017-07-09 20:59:49

标签: android json android-intent

大家好我正在尝试将json对象传递给第二个活动以显示其他信息,但是会出现“预期表达式”错误。

我的课程:

public class Pokemon implements Parcelable {

    private String name;
    private String type;
    private Integer attack;
    private Integer defense;
    private Integer evolveLevel;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public Integer getattack() { return attack; }

    public Integer getdefense() { return defense; }

    public Integer getEvolveLevel() { return evolveLevel; }

    protected Pokemon(Parcel in) {
        name = in.readString();
        type = in.readString();
        attack = in.readByte() == 0x00 ? null : in.readInt();
        defense = in.readByte() == 0x00 ? null : in.readInt();
        evolveLevel = in.readByte() == 0x00 ? null : in.readInt();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(type);
        if (attack == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeInt(attack);
        }
        if (defense == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeInt(defense);
        }
        if (evolveLevel == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeInt(evolveLevel);
        }
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<Pokemon> CREATOR = new Parcelable.Creator<Pokemon>() {
        @Override
        public Pokemon createFromParcel(Parcel in) {
            return new Pokemon(in);
        }

        @Override
        public Pokemon[] newArray(int size) {
            return new Pokemon[size];
        }
    };
}

意图:

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(view.getContext(), Main2Activity.class);
           intent.putExtra("Pokemon", Pokemon);
            view.getContext().startActivity(pokemon);
        }
    });

我在这一行收到错误:intent.putExtra(“口袋妖怪”,口袋妖怪); 它强调口袋妖怪并说“表达期望”

我无法启动我的项目,我不明白这个错误,任何人都有任何想法?

[编辑] 完整数据适配器:

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> implements Filterable {

private ArrayList<Pokemon> mArrayList;
private ArrayList<Pokemon> mFilteredList;



public DataAdapter(ArrayList<Pokemon> arrayList) {
    mArrayList = arrayList;
    mFilteredList = arrayList;

}

@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, final int i) {

    viewHolder.tv_name.setText(mFilteredList.get(i).getName());
    viewHolder.tv_version.setText(mFilteredList.get(i).getName());
    viewHolder.tv_api_level.setText(mFilteredList.get(i).getType());

    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(view.getContext(), Main2Activity.class);
            intent.putExtra("Pokemon", mFilteredList.get(i));
            intent.putParcelableArrayListExtra("Pokemon_List", mFilteredList);
            view.getContext().startActivity(intent);
        }
    });
}

@Override
public int getItemCount() {

    return mFilteredList.size();
}

@Override
public Filter getFilter() {

    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {

            String charString = charSequence.toString();

            if (charString.isEmpty()) {

                mFilteredList = mArrayList;
            } else {

                ArrayList<Pokemon> filteredList = new ArrayList<>();

                for (Pokemon marraylist : mArrayList) {

                    if (marraylist.getName().toLowerCase().contains(charString) ||  marraylist.getType().toLowerCase().contains(charString)) {

                        filteredList.add(marraylist);
                    }
                }

                mFilteredList = filteredList;
            }

            FilterResults filterResults = new FilterResults();
            filterResults.values = mFilteredList;
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            mFilteredList = (ArrayList<Pokemon>) filterResults.values;
            notifyDataSetChanged();
        }
    };
}

public class ViewHolder extends RecyclerView.ViewHolder{
    private TextView tv_name,tv_version,tv_api_level;
    public ViewHolder(View view) {
        super(view);

        tv_name = (TextView)view.findViewById(R.id.tv_name);
        tv_version = (TextView)view.findViewById(R.id.tv_version);
        tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);

    }
}

}

[EDIT2]

我的Main2Activity: 公共类Main2Activity扩展了AppCompatActivity {

private TextView attack;

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

    Pokemon pokemon = getIntent().getParcelableExtra("Pokemon");
    attack.setText(pokemon.getattack().toString());



}

}

在我的第一个活动中显示姓名和类型:

List of Pokemons

堆栈追踪:

/AndroidRuntime: FATAL EXCEPTION: main
                  Process: fr.clementineferreol.myteamrocket, PID: 3137
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.clementineferreol.myteamrocket/fr.clementineferreol.myteamrocket.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                      at fr.clementineferreol.myteamrocket.Main2Activity$override.onCreate(Main2Activity.java:17)
                      at fr.clementineferreol.myteamrocket.Main2Activity$override.access$dispatch(Main2Activity.java)
                      at fr.clementineferreol.myteamrocket.Main2Activity.onCreate(Main2Activity.java:0)
                      at android.app.Activity.performCreate(Activity.java:6237)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

3 个答案:

答案 0 :(得分:1)

startActivity需要意图作为输入而不是pokemon实例。 pokemon是想要传递第二个活动的对象实例,可以通过意图检索。

您既可以在onclick方法中构建口袋妖怪,也可以在onClick方法之外构建口袋妖怪,并通过在MainActivity类中声明它来使其可用。

OPTION1:在onclick

中构建口袋妖怪
view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // construct pokemon here
        Pokemon pokemon = null;
        Intent intent = new Intent(view.getContext(), Main2Activity.class);
        intent.putExtra("Pokemon", pokemon);
        view.getContext().startActivity(intent);
    }
});

OPTION2:在onclick

之外构建口袋妖怪
Pokemon pokemon = null;

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

    // construct pokemon here

    TextView view = (TextView) findViewById(R.id.text);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(view.getContext(), Main2Activity.class);
            intent.putExtra("Pokemon", pokemon);
            view.getContext().startActivity(intent);
        }
    });

}

答案 1 :(得分:0)

你正在通过Capital P传递类名Pokemon,我想你想用小写p传递pokemon。这导致错误,你没有传递该类的对象。

希望这有帮助。

答案 2 :(得分:0)

您应该{intent} startActivity并将对象添加到extra。但是你将pokemon对象传递给startActivity并将Pokemon类添加到extra。改变它会正常工作。

Intent intent = new Intent(view.getContext(), Main2Activity.class);
intent.putExtra("Pokemon", pokemon);
view.getContext().startActivity(intent);

编辑:

你可以从ViewHolder中删除onclickListener,你可以像这样添加bindView,然后你发送它应该通过putParcelableArrayListExtra和putExtra发送的Parcelable列表,如果你想发送单个元素,你可以从列表中获取并且你可以发送。

@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, final int i) {

    viewHolder.tv_name.setText(mFilteredList.get(i).getName());
    viewHolder.tv_version.setText(mFilteredList.get(i).getName());
    viewHolder.tv_api_level.setText(mFilteredList.get(i).getType());

    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(view.getContext(), Main2Activity.class);
            intent.putExtra("Pokemon", mFilteredList.get(i));
            intent.putParcelableArrayListExtra("Pokemon_List", mFilteredList);
            view.getContext().startActivity(intent);
        }
    });
}