Java.lang.IllegalStateException: Already attached

18

我正在尝试构建一个应用程序,该应用程序可以将来自先前活动的输入粘贴并显示来自数据库的一些内容(当按下ButtonGet时有效)。问题是,在我尝试运行项目时,我收到Java.lang.IllegalStateException: Already attached。我的代码有什么问题?

 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();
     }
 }

2
如果您遇到了崩溃,请在提问时务必在logcat中发布堆栈跟踪。 - Doug Stevenson
4个回答

50

在你的onCreate方法中,你调用了两次super.onCreate()和两次setContentView()。我非常确定这不是你想要做的。


很可能是超级调用。在单个函数中调用setContentView两次是毫无意义的,但总体上是完全合法的。 - Gabe Sechan
移除super.onCreate之后,我得到了AppCompat不支持当前主题特性的错误。尽管我已经安装了该主题。 - user3026270
1
@user3026270,现在看起来你有一个完全无关的问题,可能需要另外提出一个新的问题。 - Doug Stevenson
如果您认为这个答案有帮助,请接受它作为正确答案,并在另一个问题中提出不同的问题。 - Doug Stevenson
谢谢,我的代码在多个实例中定义了super.onCreate方法,你的解决方案解决了我的问题。 - Surya Tej

0
问题在于您首先初始化了textView1,然后执行按钮点击,此时您只是通过再次调用onCreate()来重置任何先前的设置,在perfomClick方法命中getData()方法之前,在此处内部也尝试访问来自textView1的文本,但是您在此之后调用了onCreate并从头开始设置视图。这就是为什么您无法使其工作,删除重复代码即可。

现在我明白了,java.lang.NullPointerException。 - user3026270

0
请附上logcat消息,如果可能的话,请附上DB文件。 这可能是问题所在,您可能正在插入表时重新分配已经分配的db对象。

这应该是一个注释。 - ig0774
https://docs.google.com/document/d/1h4Ix0cwarGglggKK1HPZ_1syDO6Jk2hy01SW_Zwp7wg/edit?usp=sharing - user3026270
我去除了重复项,现在出现了 java.lang.NullPointerException。 - user3026270
这是你从中调用DB类的活动的最终代码吗?如果是,你还没有初始化你的数据库,请先初始化它。如果可能的话,请也粘贴一下你错误的第二个活动的onCreate方法。 - Chintan Desai
我在另一个类中初始化了数据库。你说的“将第二个活动的onCreate方法错误粘贴过来”是什么意思? - user3026270
我看到了你的logcat错误信息,它说第二个活动的第58行出现了错误。 - Chintan Desai

0
如果这里的任何答案都没有帮助,而且日志信息也毫无意义,我建议您选择“文件”->“无效缓存”,然后再次运行应用程序,希望这次日志将显示正确的堆栈跟踪。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接