0

我正在尝试使用 (View.Invisible) 隐藏 2 个线性布局。LinearLayouts 中分别有 1-1 个文本视图。有什么建议么?下面我附上了 MainActivity 类、一个 AsyncTask 类和 Logcat 文本。当我尝试将 LinearLayouts 添加到 Arraylist 时出现错误(我已经研究了很多:P...评论它不会出错)...提前致谢

主要活动

package com.abhishek.ally2;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    TextView textview;
    String header;
    Handler handler;
    ImageView image ;
    asyncNetwork task = null;
    ProgressBar pg;
    List<String> response = null;
    List<LinearLayout> ll =new ArrayList<LinearLayout>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        handler = new Handler();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void login(View v)
    {
        boolean check = true;
        String id = null;
        int length;
        textview = (TextView)findViewById(R.id.allyId);
        if(textview.getText().length() == 0)
        {
            Toast.makeText(this, "Enter Ally ID", Toast.LENGTH_LONG).show();
            check = false;
        }

        for (char c : textview.getText().toString().toCharArray())
        {
            if (!Character.isDigit(c))
            {
                Toast.makeText(this, "Invalid Input", Toast.LENGTH_LONG).show();
                check = false;
                break;
            }
        }
        if(check)
        {
            id = textview.getText().toString();
            id = "id="+id;
            length = id.length();
            setContentView(R.layout.activity_main);
            pg = (ProgressBar)findViewById(R.id.progressBar1);
            textview = (TextView)findViewById(R.id.status); 
            image = (ImageView)findViewById(R.id.imageFrnds);
            ll.add((LinearLayout)findViewById(R.id.emailll1));
            ll.add((LinearLayout)findViewById(R.id.smsll1));
            image.setImageResource(R.drawable.ally);
            task =  new asyncNetwork(this, textview, new Handler(), image, pg, ll);
            task.execute("easyvote.co.in", "80", "POST /ally.php HTTP/1.0\nHost: easyvote.co.in\nContent-Type: application/x-www-form-urlencoded\nContent-Length: "+ length +"\n\n" + id + "\n");   
        }

    }

}

asyncNetwork.java

/**
 * 
 */
package com.abhishek.ally2;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class asyncNetwork extends AsyncTask<String, Void, String>{

    Context myCtx;
    Friend friend ;
    TextView status;
    ImageView image;
    private Handler handler;
    private Runnable r;
    String responseLine;
    List<String> response = null;
    List<LinearLayout> ll = null;
    ProgressBar pg;
    boolean data = false;
    asyncNetwork(Context context, TextView status, Handler handler, ImageView image, ProgressBar pg, List<LinearLayout> ll)
    {
        this.myCtx = context;
        this.status = status;
        this.handler = handler;
        this.data = false;
        this.image = image;
        this.pg = pg;
        this.ll = ll;
    }
    @Override
    protected String doInBackground(String... mainData) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String host = mainData[0];
        int port = Integer.parseInt(mainData[1]);
        String data = mainData[2];
        Socket client = null;
        DataOutputStream os = null;
        BufferedReader is = null;
        try 
        {
            while(true)
            {
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                this.response = new ArrayList<String>();
                this.data = false;
                client = new Socket(host, port);
                os = new DataOutputStream(client.getOutputStream());
                is= new BufferedReader(new InputStreamReader(client.getInputStream(), "iso-8859-1"), 8);
                if(client != null && os != null && is != null)
                {
                    //Log.d("Response", data);
                    os.writeBytes(data);
                    responseLine = is.readLine();
                    if(!responseLine.contains("200"))
                    {
                        r = new Runnable(){

                            @Override
                            public void run() {
                                status.setText("404 Error :(");

                            }

                        };
                        this.handler.post(r);
                    }
                    while((responseLine = is.readLine()) != null)
                    {
                        Log.d("Response", responseLine);
                        if(this.data)
                        {
                            response.add(responseLine);
                        }
                        if(responseLine.contains("###start"))
                        {
                            this.data = true;
                        }
                    }
                    os.close();
                    is.close();
                    client.close();
                    Log.d("Count value", "value" + response.size());
                    friend =  new Friend(response.get(0));
                    if(friend.isOnline())
                    {
                        Log.d("Count value", friend.name + " is online :)");
                        //status.setText(friend.name + " is online :)");
                        r = new Runnable(){

                            @Override
                            public void run() {
                                status.setText(friend.name + " is Online :)");
                                image.setImageResource(R.drawable.tapus);
                                pg.setVisibility(View.INVISIBLE);

                            }
                        };
                        this.handler.post(r);
                    }
                    else
                    {
                        Log.d("Count value", friend.name + " is Offline :)");

                        r = new Runnable(){

                            @Override
                            public void run() {
                                image.setImageResource(R.drawable.tapus);
                                status.setText(friend.name + " is Offline :(");
                                pg.setVisibility(View.VISIBLE);
                                //ll[0].setVisibility(View.VISIBLE);
                                //ll[1].setVisibility(View.VISIBLE);
                            }
                        };
                        this.handler.post(r);
                    }

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

    }

}

Logcat 数据

07-04 22:39:19.875: E/AndroidRuntime(446): FATAL EXCEPTION: main
07-04 22:39:19.875: E/AndroidRuntime(446): java.lang.IllegalStateException: Could not execute method of the activity
07-04 22:39:19.875: E/AndroidRuntime(446):  at android.view.View$1.onClick(View.java:2072)
07-04 22:39:19.875: E/AndroidRuntime(446):  at android.view.View.performClick(View.java:2408)
07-04 22:39:19.875: E/AndroidRuntime(446):  at android.view.View$PerformClick.run(View.java:8816)
07-04 22:39:19.875: E/AndroidRuntime(446):  at android.os.Handler.handleCallback(Handler.java:587)
07-04 22:39:19.875: E/AndroidRuntime(446):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-04 22:39:19.875: E/AndroidRuntime(446):  at android.os.Looper.loop(Looper.java:123)
07-04 22:39:19.875: E/AndroidRuntime(446):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-04 22:39:19.875: E/AndroidRuntime(446):  at java.lang.reflect.Method.invokeNative(Native Method)
07-04 22:39:19.875: E/AndroidRuntime(446):  at java.lang.reflect.Method.invoke(Method.java:521)
07-04 22:39:19.875: E/AndroidRuntime(446):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-04 22:39:19.875: E/AndroidRuntime(446):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-04 22:39:19.875: E/AndroidRuntime(446):  at dalvik.system.NativeStart.main(Native Method)
07-04 22:39:19.875: E/AndroidRuntime(446): Caused by: java.lang.reflect.InvocationTargetException
07-04 22:39:19.875: E/AndroidRuntime(446):  at com.abhishek.ally2.MainActivity.login(MainActivity.java:70)
07-04 22:39:19.875: E/AndroidRuntime(446):  at java.lang.reflect.Method.invokeNative(Native Method)
07-04 22:39:19.875: E/AndroidRuntime(446):  at java.lang.reflect.Method.invoke(Method.java:521)
07-04 22:39:19.875: E/AndroidRuntime(446):  at android.view.View$1.onClick(View.java:2067)
07-04 22:39:19.875: E/AndroidRuntime(446):  ... 11 more
07-04 22:39:19.875: E/AndroidRuntime(446): Caused by: java.lang.ClassCastException: android.widget.TextView
07-04 22:39:19.875: E/AndroidRuntime(446):  ... 15 more
4

1 回答 1

0

The error appears to be in these two lines

    ll.add((LinearLayout)findViewById(R.id.emailll1));
    ll.add((LinearLayout)findViewById(R.id.smsll1));

You are trying to cast a View to a LinearLayout and by the logcat they seem to be TextViews (at least the first one which would be throwing the error).

If this is the case then you need to change what you are adding here to the ArrayList to add an actual LinearLayout that you have.

于 2013-07-04T17:42:33.017 回答