2017-04-02 95 views
0

我创建了一个简单的ImageView,并尝试点击时启动一个活动,但当我在模拟器中单击它时,该应用只是崩溃并给我(等待或在这里关闭应用程序提示) 是我:android应用关闭时没有错误,当点击imageview时

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fillViewport="true" 
android:orientation="vertical"> 

<ImageView 
    android:id="@+id/about" 
    android:layout_width="match_parent" 
    android:layout_height="450dp" 
    android:background="@color/colorPrimaryDark" 
    android:src="@drawable/twoth" /> 

</LinearLayout> 

这里是我的Java代码

public class MainUi extends AppCompatActivity { 

    //Variables Declaration. 
    private Button btn1, btn2, btn3, btn4; 
    private ImageView img; 


    //Called when the activity is first created. 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main_ui); 
     //Variables Initialization and OnClick Method 


     img = (ImageView) findViewById(R.id.about); 
     img.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       //Start another activity 
       Intent myIntent = new Intent(MainUi.this, Query.class); 
       startActivity(myIntent); 
      } 
     }); 


    } 


} 
+1

是您的清单中定义的查询?你的logcat显示任何错误? –

+0

你的图像的分辨率@ drawable/twoth?如果它太大,而您没有调整它的大小以更正可绘制的文件夹,则可能是内存问题。 –

+0

“等待或关闭”的事情让我认为查询活动中有一些东西在主线程上做了很多工作。您可以发布任何可能需要很长时间才能处理并处于查询活动状态的代码吗? –

回答

2

的 '等待或接近' 的事情线索我认为查询活动中有一些东西在主线程上做了很多工作。

尝试使用AsyncTask并将所有长时间运行的进程放在doInBackground部分中,然后在onPostExecute中处理响应。

我猜这取决于你实际做了什么,但AsyncTask应该是一个好的开始。

+0

我做到了,实际上这里是问题,serversocket已关闭,我尝试连接到它(在asyncTask doInBackground中),导致“等待或关闭”。我想以某种方式处理这种情况,如果套接字连接失败,它会显示敬酒,请你指导我如何做到这一点? –

+0

您可以提供一些关于如何连接到套接字的额外信息吗?或者你在onPostExecute/onPreExecute中做什么? –

+0

我会更好地回答这篇文章。 –

0

这里是完整的代码,我添加了其余的代码,以防万一您可以从外部(查询)类处理它,而不是在Async类中处理它。

public class Query extends AppCompatActivity { 
    private ArrayList<String[]> BB; 
    private ArrayList<String[]> AA = new ArrayList<String[]>(); 
    private Socket socket = null; 
    private ObjectInputStream in = null; 
    private DataOutputStream out = null; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_query); 
     if (android.os.Build.VERSION.SDK_INT > 9) { 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
     } 
     try { 
      AA = new AsyncAction().execute().get(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (socket != null) { 
       try { 
        socket.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      setTextViews(); 


     } 
    } 

    public void setTextViews() { 
     View linearLayout = findViewById(R.id.info); 
     for (int y = 0; y < AA.size(); y++) { 
      // for (int x = 0; x < AA.get(0).length; x++) {} 
      TextView name = new TextView(this); 
      name.setText(AA.get(y)[0]); 
      name.setId(y); 
      name.setGravity(Gravity.CENTER); 
      name.setTextSize(30); 
      name.setPadding(0, 30, 0, 0); 
      name.setTextColor(this.getResources().getColor(R.color.icons)); 
      name.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0)); 

      ProgressBar pb = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal); 
      Drawable drawable = pb.getProgressDrawable(); 
      drawable.setColorFilter(new LightingColorFilter(0xFF000000, this.getResources().getColor(R.color.colorPrimary))); 
      pb.setProgress(100); 

      final int finalY = y; 
      name.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View arg0) { 
        Bundle b = new Bundle(); 
        b.putStringArray("list", AA.get(finalY)); 
        Intent i = new Intent(Query.this, Info.class); 
        i.putExtras(b); 
        startActivity(i); 
       } 
      }); 
      ((LinearLayout) linearLayout).addView(name); 
      ((LinearLayout) linearLayout).addView(pb); 

     } 
    } 


    private class AsyncAction extends AsyncTask<String, Void, ArrayList> { 

     protected ArrayList doInBackground(String... args) { 
      try { 


       socket = new Socket(port, 8888); 
       out = new DataOutputStream(socket.getOutputStream()); 
       in = new ObjectInputStream(socket.getInputStream()); 
//    action 
       out.writeInt(2); 
       try { 

        in = new ObjectInputStream(socket.getInputStream()); 
        BB = (ArrayList<String[]>) in.readObject(); 


        in.close(); 
        socket.close(); 

       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } catch (UnknownHostException e1) { 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 


      return BB;//returns what you want to pass to the onPostExecute() 
     } 

     protected void onPostExecute(ArrayList result) { 
      AA = result; 

     } 
    } 
} 
+0

从AA = new AsyncAction()。execute()。get();让它就像AA = new AsyncAction()。execute(); –

+0

错误:(47,47)错误:不兼容的类型:AsyncTask 不能转换为ArrayList ,并且正在从服务器获取数组列表。 –

+0

修改您的类以适应您接收的数据:AsyncAction扩展AsyncTask >或ArrayList 。与onPostExecute()一样的事情 在onPostExecute中,尝试并使用一个接口与您的活动交流结果。 –

相关问题