0

My problem is, I am transferring two arrays between activities. with an return from the one to another Activity. There I am getting these two Activities by the constructor( I think ). Is this correct? Why I am getting the error: no empty constructor?

Here is my class where the Activites are coming from:

public PlanOutputActivity fetchallRoutes(String startStop, String endStop, String time) {
.
.
.
return new PlanOutputActivity(convertArray(),routenArray);

Here is my Activity where i wanna get these two Arrays:

public class PlanOutputActivity extends Activity {

    Intent intent;
    Object[][] routenArray;
    String[][] itemsArray;
    DatabaseHelperActivity mdbH;
    public int RESULT_OK = 123;
    public int REQUEST_OK = 456;

    public PlanOutputActivity(String[][] itemsArray, String[][] routenArray){
        setContentView(R.layout.planoutputlayout);
        this.routenArray = routenArray;
        this.itemsArray = itemsArray;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated constructor stub
        super.onCreate(savedInstanceState);
     }
     public void getRoute() {

        intent = getIntent();
        mdbH = new DatabaseHelperActivity(this);
        mdbH.fetchallRoutes(intent.getStringExtra("StartHaltestelle"),intent.getStringExtra("ZielHaltestelle"),intent.getStringExtra("Zeit"));
      ListView lvList = (ListView)findViewById(R.id.ListOutput);
        ArrayAdapter<DefineRouteActivity> adapter = new RouteAdapterActivity(this, route);
        lvList.setAdapter(adapter);

        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int gKennung = view.getId();
                Intent intent = new Intent(getApplicationContext(),DetailOutputActivity.class);
                intent.putExtra("Kennung",gKennung);
                intent.putExtra("routenArray",routenArray);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }

1 Answer 1

2

You can't create an instance of an Android Activity using new - it simply doesn't work.

The only ways to create an Activity are either when it is launched from the Apps launcher or if you call startActivity(...) (or one of the other methods such as startActivityForResult(...) etc).

Because of that you shouldn't ever create constructors for an Activity and you should never create public static fields or methods with the intention of accessing data or calling methods in an Activity from any other application class.

If you want to pass data from one Activity to another do it using the extras in an Intent or simply persist the data in SharedPreferences or a database. Alternatively create a 'helper' class to hold the data - using the Singleton pattern is quite common for this.

Sign up to request clarification or add additional context in comments.

1 Comment

is it possible to add an String[][] array to an intent and get this array after all in the second array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.