Java.lang.IllegalStateException: Already attached

11,878

In your onCreate, you're calling super.onCreate() twice, and also setContentView() twice. I'm pretty sure that's not what you want to do.

Share:
11,878

Related videos on Youtube

user3026270
Author by

user3026270

Updated on June 17, 2022

Comments

  • user3026270
    user3026270 almost 2 years

    I'm trying to build an app, that pastes an input from a previous activity(works with no problem) and then shows me some things from a database(when ButtonGet is pressed). The problem is that when I try to Run the project, I get Java.lang.IllegalStateException: Already attached. What is wrong with my code?

     package br.exemplozxingintegration;
    
     import android.annotation.SuppressLint;
     import android.app.ProgressDialog;
     import android.content.ClipData;
     import android.content.ClipboardManager;
     import android.os.Bundle;
     import android.support.v7.app.AppCompatActivity;
     import android.view.View;
     import android.widget.Button;
     import android.widget.EditText;
     import android.widget.TextView;
     import android.widget.Toast;
    
     import com.android.volley.RequestQueue;
     import com.android.volley.Response;
     import com.android.volley.VolleyError;
     import com.android.volley.toolbox.StringRequest;
     import com.android.volley.toolbox.Volley;
    
     import org.json.JSONArray;
     import org.json.JSONException;
     import org.json.JSONObject;
    
    
    
     public class SecondActivity extends AppCompatActivity implements View.OnClickListener  {
    
    
    
         private EditText  pastetext;
         private ClipboardManager myClipboard;
         private ClipData myClip;
         private Button btn;
         private EditText textView1;
         private Button buttonGet;
         private TextView textViewResult;
    
         private ProgressDialog loading;
    
    
               @Override
               protected void onCreate(Bundle savedInstanceState) {
                   super.onCreate(savedInstanceState);
                   setContentView(R.layout.activity_second);
                   myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                   pastetext = (EditText) findViewById(R.id.textView1);
                   btn = (Button)findViewById(R.id.buttonPaste);
                   btn.performClick();
                   super.onCreate(savedInstanceState);
                   setContentView(R.layout.activity_main);
    
                   textView1 = (EditText) findViewById(R.id.textView1);
                   buttonGet = (Button) findViewById(R.id.buttonGet);
                   textViewResult = (TextView) findViewById(R.id.textViewResult);
    
                   buttonGet.setOnClickListener(this);
    
               }
    
    
    
               @SuppressLint("NewApi")
               public void paste(View view) {
                   ClipData cp = myClipboard.getPrimaryClip();
                   ClipData.Item item = cp.getItemAt(0);
                   String text = item.getText().toString();
                   pastetext.setText(text);
                   Toast.makeText(getApplicationContext(), "Text Pasted",
                           Toast.LENGTH_SHORT).show();
    
    
               }
    
    
         private void getData() {
             String id = textView1.getText().toString().trim();
             if (id.equals("")) {
                 Toast.makeText(this, "", Toast.LENGTH_LONG).show();
                 return;
             }
             loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
    
             String url = Config.DATA_URL+textView1.getText().toString().trim();
    
             StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
                 @Override
                 public void onResponse(String response) {
                     loading.dismiss();
                     showJSON(response);
                 }
             },
                     new Response.ErrorListener() {
                         @Override
                         public void onErrorResponse(VolleyError error) {
                             Toast.makeText(SecondActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                         }
                     });
    
             RequestQueue requestQueue = Volley.newRequestQueue(this);
             requestQueue.add(stringRequest);
         }
    
         private void showJSON(String response){
             String name="";
             String image = "";
             try {
                 JSONObject jsonObject = new JSONObject(response);
                 JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
                 JSONObject collegeData = result.getJSONObject(0);
                 name = collegeData.getString(Config.KEY_NAME);
                 image = collegeData.getString(Config.KEY_IMAGE);
             } catch (JSONException e) {
                 e.printStackTrace();
             }
             textViewResult.setText("Name:\t"+name+"\nImagine :\t"+ image);
         }
    
         @Override
         public void onClick(View v) {
             getData();
         }
     }
    
    • Doug Stevenson
      Doug Stevenson about 8 years
      If you are ever seeing a crash, be sure to post the stack trace in logcat when stating your question.
  • Gabe Sechan
    Gabe Sechan about 8 years
    Its probably the super call. Calling setContentView twice is pointless in a single function, but is totally legal overall.
  • user3026270
    user3026270 about 8 years
    After removing the super.onCreate, I get this AppCompat does not support the current theme features. Although I have the theme instaled
  • user3026270
    user3026270 about 8 years
    Now I get, java.lang.NullPointerException
  • ig0774
    ig0774 about 8 years
    This should really be a comment.
  • user3026270
    user3026270 about 8 years
  • user3026270
    user3026270 about 8 years
    I took out the duplicates, and now I get , java.lang.NullPointerException
  • Chintan Desai
    Chintan Desai about 8 years
    is it your final code of activity from where you call the DB class. If it is you haven't initialize your db yet please first initialize it. If possible please paste your erroneous onCreate method of Second activity as well.
  • Doug Stevenson
    Doug Stevenson about 8 years
    @user3026270 now it sounds like you have a completely unrelated problem which could now be its own question.
  • user3026270
    user3026270 about 8 years
    I init the DB in a different class. And what do u mean by "paste your erroneous onCreate method of Second activity as well".
  • Chintan Desai
    Chintan Desai about 8 years
    I saw your logcat error message it says error on line 58 of second activity.
  • Doug Stevenson
    Doug Stevenson about 8 years
    @user3026270 if you found this answer to be helpful, please accept it as the correct answer and follow up with different questions in another question.
  • Surya Tej
    Surya Tej over 4 years
    Thanks, my code had the super.onCreate method defined at multiple instances, and your solution solved mine