我无法通过使用" TestCrowd.java"

时间:2017-02-08 17:36:19

标签: android

TestCrowd.java

public class TestCrowd extends Application {

    private JSONObject role,details;
    private Bitmap image;

    public JSONObject getRole() {
        return role;
    }
    public void setRole(JSONObject role) {
        this.role = role;
    }

    public JSONObject getDetails() { return details; }
    public void setDetails(JSONObject details) {
        this.details = details;
    }

    public void setImage(Bitmap image) { this.image = image; }
    public Bitmap getImage() {return image;}
}

NewWorkOrderActivity.java

public class NewWorkOrderActivity extends Activity implements MenuItem.OnMenuItemClickListener {

    private ImageButton ibtnEdit;
    SystemPreference systemPreference;
    int a1;
    String s1;
    List<String> arraySpinner_1;

    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nwworder);

        systemPreference = SystemPreference.getInstance(this.getApplicationContext());
        String token = systemPreference.getData(getResources().getString(R.string.stoken));

        Spinner spinner_1 = (Spinner) this.findViewById(R.id.prioritySpinner);

        arraySpinner_1 = new ArrayList<>();

        try {
            JSONObject details = ((TestCrowd) getApplication() ).getDetails(); /*where the error is located*/

            JSONObject priority = details.getJSONObject("priorities");
            JSONArray priority_arry = priority.getJSONArray("value");
            for (int i = 0; i < priority_arry.length(); i++) {
                arraySpinner_1.add(priority_arry.get(i).toString());

            }
            ArrayAdapter<String> spinnerAdepter_1 = new ArrayAdapter<String>(this,R.layout.spinner_item,arraySpinner_1);
            spinner_1.setAdapter(spinnerAdepter_1);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        initialized();
    }

    private void initialized() {

        ibtnEdit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Spinner spinner1 = (Spinner)findViewById(R.id.prioritySpinner);

                a1 = spinner1.getSelectedItemPosition();
                s1 = Integer.toString(a1);

                String token = systemPreference.getData(getResources().getString(R.string.stoken));
                String posturl = getResources().getString(R.string.baseurl) + "workorder/addworkorder";
                HashMap<String, String> data = new HashMap<String, String>();

                    data.put("token",token.toString());
                    data.put("priority", s1);

                    /*volley get request here*/ 
            }
        });
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }
}

当我运行该项目时,它会在&gt;&gt;&gt; JSONObject details = ((TestCrowd) getApplication() ).getDetails();&lt;&lt;&lt;&lt;

上发出错误消息

错误是,

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.malith.testcrowd / com.example.malith.testcrowd.NewWorkOrderActivity}:java.lang.ClassCastException:android.app.Application无法转换为com .example.malith.testcrowd.TestCrowd                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                         在android.app.ActivityThread.-wrap12(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:154)                         在android.app.ActivityThread.main(ActivityThread.java:6077)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)                      引起:java.lang.ClassCastException:android.app.Application无法强制转换为com.example.malith.testcrowd.TestCrowd                         在com.example.malith.testcrowd.NewWorkOrderActivity.onCreate(NewWorkOrderActivity.java:50)

1 个答案:

答案 0 :(得分:0)

在这种情况下,您应该尝试使用方法(TestCrowd) getApplicationContext();
无论如何,你的TestCrowd类应该在清单文件中注册,所以检查一下。