Tags:

พอดีว่า ผมเขียนโปรแกรมบน Android แบบแยกกหน้าต่างๆออกเป็น TabLayout แยกกออกเป็นสามอัน Image Hostingตามรูปครับ

ซึ่งแต่ล่ะ Tab จะใช้ Layout คนล่ะไฟล์กัน(ไฟล์.xmlที่อยู่ในโฟรเดอร์ layout)
จากนั้น ผมทำการเขียน class java ขึ้นมาอีกเพื่อเรียกใช้ Class Activity ของแต่ล่ะ Tab
เช่น
SetupActivity sa = new SetupActivity();//เรียก Class SetupActivity(Tabที่ 3)
แล้วเรียก Function setFalse(); ที่มีอยู่ใน Class SetupActivity เพื่อ setEnabled(false)
ให้กับ button ที่อยู่ใน Class SetupActivity

แล้วมันก็เด้ง Error Image Hostingตามรูปครับ ผมลองดูค่าของ button ปรากฏว่ามันเป้น null ครับ

ผมเลยลองหลายๆวิธีเช่น ประกาศ button ให้เป็น public ใน Class SetupActivity เช่น
public Button connectBtn;
ใน onCreate ก็เรียก connectBtn = (Button)findViewById(R.id.connectBtn);

แล้ว เรียกแบบ sa.connectBtn.setEnabled(false); มันก็ไม่ได้

ทำยังไงผมถึงจะเรียกใช้งาน TextView button หรือ อะไรต่างๆ ของ class อื่นๆได้ครับ
คือผมพยายามจะแยกส่วนโปรแกรมออกเป็น Tab ซึ่งมีสาม Tab ซึ่งพอกดปุ่ม Connect ของ Tab ที่3 จะไปเรียกไฟล์ Class Connect ที่ผมเขียนไว้ ซึ่งถ้า Connect ผ่าน มันจะ set button ให้เท่ากับ False เพื่อไม่ให้กดได้ แต่ มันเรียกช้ามไปแล้ว Set ค่าให้ไม่ได้ครับ

รบกวนขอคำแนะนำอีกสักกระทู้

Get latest news from Blognone
By: bongikairu
ContributoriPhone
on 4 August 2011 - 19:47 #318476

อยากเห็นว่า connectServer ใน class ConnectServer เรียกใช้ setFalse ยังไงอ่ะครับ พอจะให้ดูได้ไหมครับ

By: bobe
iPhoneWindows PhoneAndroid
on 5 August 2011 - 09:27 #318683
bobe's picture

นี้ Class ConnectServer ตัดส่วนที่เรียก Class SetupActivity มาให้ดูเพราะถ้าเอาทั้งหมดมันเยอะ

public class ConnectServer {

private String userName = null;
private String serverName = null;
private int serverPort = 0;
private Socket socket = null;

//Class Connection
PrivateChatZone pc = new PrivateChatZone();//Class PrivateChatZone
SetupActivity sa = new SetupActivity();//Class SetupActivity
MainActivity ma = new MainActivity(); //Class MainActivity

ConnectServer(String server,int port,String user)
{
    this.serverName = server;
    this.serverPort = port;
    this.userName = user;
}

public void connectServer(String eventStr){
    String str = eventStr;

    if(str.equals("Connect"))
    {
        try
        {
            socket = new Socket(serverName, serverPort);// Server and Port

            PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket                                   
                     .getOutputStream())), true);
            pw.println(userName);
            pw.flush();

            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String ms=br.readLine();

            if(ms.equals("ok")){

                Thread cThread = new Thread(new ClientThread(socket));//Class ClientThread
                cThread.start();

                sa.setFalse("connectBtn");
                sa.setFalse("userText");
                sa.setFalse("serverText");
                sa.setFalse("portText");
                sa.setTrue("disconnectBtn");

                ma.setTrue("sendBtn");
                ma.setTrue("msgText");

                pc.tabHost.setCurrentTab(0);
                sa.statusServerText.setText("your connected in server.");
            }
            else{
                socket.close();
                Log.e("Error Connect","Username already in use Error");
                sa.status = "Username already in use Error";
            }
        }
        catch(IOException e){
            e.printStackTrace();
            Log.e("Error Connect", e.toString());
            sa.status = "Error connectServer() :"+e.toString();
        }
    }
        }

}

By: bobe
iPhoneWindows PhoneAndroid
on 5 August 2011 - 09:31 #318686
bobe's picture

ส่วนนี้เป็น Class SetupActivity ที่ผมเรียกมาจาก Class ConnectServer ซึ่ง Class ConnectServer ก็ถูกเรียกมาจาก Class SetupActivity นี้แหละครับ ลักษณะเหมือนการเรียกไปเรียกกลับ

public class SetupActivity extends Activity {

public Button connectBtn;
public Button disconnectBtn;
public EditText userText;
public EditText serverText;
public EditText portText;
public TextView statusServerText;

public String userName = null;
public String status = "server..";
public String serverName = null;
public int serverPort = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab3);

    userText = (EditText)findViewById(R.id.userText);
    serverText = (EditText)findViewById(R.id.serverText);
    portText = (EditText)findViewById(R.id.portText);
    statusServerText = (TextView)findViewById(R.id.statusServerText);
    connectBtn = (Button)findViewById(R.id.connectBtn);
    disconnectBtn = (Button)findViewById(R.id.disconnectBtn);

    statusServerText.setText(status);
    disconnectBtn.setEnabled(false);

    //Set Event Button
    connectBtn.setOnClickListener(
            new OnClickListener() {
                public void onClick(View v) {
                    userName = userText.getText().toString();
                    serverName = serverText.getText().toString().trim();
                    serverPort = Integer.parseInt(portText.getText().toString().trim());

                    connServer("Connect");

                }
            }
    );

    disconnectBtn.setOnClickListener(
            new OnClickListener() {
                public void onClick(View v) {
                    connServer("Disconnect");
                }
            }
    );
}
public void connServer(String evenstr){

    ConnectServer conS = new ConnectServer(serverName,serverPort,userName);// Call Class ConnectServer
                  conS.connectServer(evenstr);

}
public void setFalse(String itemName){

    if(itemName.equals("userText")){ userText.setEnabled(false);}
    if(itemName.equals("serverText")){ serverText.setEnabled(false);}
    if(itemName.equals("portText")){ portText.setEnabled(false);}
    if(itemName.equals("connectBtn")){ connectBtn.setEnabled(false);}
    if(itemName.equals("disconnectBtn")){ disconnectBtn.setEnabled(false);}
}
public void setTrue(String itemName){

    if(itemName.equals("userText")){ userText.setEnabled(true);}
    if(itemName.equals("serverText")){ serverText.setEnabled(true);}
    if(itemName.equals("portText")){ portText.setEnabled(true);}
    if(itemName.equals("connectBtn")){ connectBtn.setEnabled(true);}
    if(itemName.equals("disconnectBtn")){ disconnectBtn.setEnabled(true);}
}

}

By: bongikairu
ContributoriPhone
on 5 August 2011 - 10:24 #318716

เอาวิธีแก้แบบลัดเลยนะครับ ถ้าอยากได้คำอธิบายเดี๋ยวเย็นๆ มาเพิ่มให้ครับ

  1. ปรับ Constructor ใน class ConnectServer โดยให้รับตัวแปรประเภท SetupActivity ครับ ก็ได้เป็นแบบนี้

{syntaxhighlighter brush:java}
ConnectServer(String server,int port,String user,SetupActivity that) {
...
this.sa = that;
}
{/syntaxhighlighter}

  1. ตัด new ออกครับ

{syntaxhighlighter brush:java}
SetupActivity sa = new SetupActivity();//Class SetupActivity
{/syntaxhighlighter}

เหลือ

{syntaxhighlighter brush:java}
SetupActivity sa;//Class SetupActivity
{/syntaxhighlighter}

ก็น่าจะได้หละครับ

By: bobe
iPhoneWindows PhoneAndroid
on 5 August 2011 - 11:14 #318727
bobe's picture

แล้ว Constructor ตัวนี้่ ( SetupActivity that ) ผมต้องส่งอะไรจาก Class SetupActivity เข้ามาครับ

ต้องเรียก ประมานนี้หรือปล่าวครับ

Image Hosting ผมเรียกตามในรูปครับ แล้วมันก็เด้ง Error มาตามรูป

By: bongikairu
ContributoriPhone
on 5 August 2011 - 11:58 #318758

อุปส์ ตกไปอีกขั้นตอนครับ ตรงนั้นใส่ this ครับ

{syntaxhighlighter brush:java}
ConnectServer conS = new ConnectServer(serverName,serverPort,userName,this);
{/syntaxhighlighter}

ไม่ต้อง new sa ไปให้ แต่ส่งตัวเองเข้าไปแทนครับ

By: bobe
iPhoneWindows PhoneAndroid
on 5 August 2011 - 12:21 #318770
bobe's picture

ลองแล้วครับ เหมือนจะดีขึ้น เพราะ item ต่างๆของ Class SetupActivity ดูจะไม่ใช่ค่าว่างแล้ว

แต่พอกด Connect มันก็เด้ง แบบนี้

Image Hosting

พอมาดู ตรง Fucntion ที่ใช้เรียก Class ConnectServer ก็จะเป้นแบบนี้ครับ

Image Hosting

แล้วผมต้องเรียก ไปหา Class อื่นด้วยครับ ไม่ใช่แค่เรียกใช้ Class SetupActivity อย่างเดี่ยว
เหมือนกับว่า Class SetupActivity เรียกหา Class ConnectServer จากนั้น ConnectServer ก็เรียกไปหา Class PrivateChatZone และ Class MainActivity ด้วยเพื่อสั่งการทำงานใน 2 classนี้

พอมีวิธีแนะนำไหมครับ

By: bongikairu
ContributoriPhone
on 5 August 2011 - 13:37 #318804

ทำคล้ายเดิมเลยครับ ส่ง SetupActivity ตัวเดิมไปให้คลาสที่ต้องใช้เลยครับ

By: bobe
iPhoneWindows PhoneAndroid
on 5 August 2011 - 14:12 #318830
bobe's picture

คือตอนนี้ เรียกแบบนี้

ConnectServer conS = new ConnectServer(serverName,serverPort,userName,this);

มันยังไม่ได้เลยครับ มันขึ้น Error ตามรูปข้างบน

By: bongikairu
ContributoriPhone
on 5 August 2011 - 16:45 #318900

มันบอกไหมครับว่าเกิด Exception อะไร

By: bobe
iPhoneWindows PhoneAndroid
on 5 August 2011 - 17:30 #318914
bobe's picture

ไม่ได้บอกครับ

แต่เท่าที่ อ่านจากเวปนอกดู รู้สึกว่า ถ้าต้องการเรียก ข้าม Activity Class ต้องใช้

Intent intent = new Intent(Activity1.this,Activity2.class);

startActivityForResult(Intent,IPC_ID);

ประมานนี้ครับ

ตามตัวอย่างนี้ครับ

ตอนนี้ก็พอจะเรียกได้แล้ว แต่มีปัญหา ตรงที่ ว่า Class มันตีกันมั่วๆ อยุ่ ทำให้ ตัว Connection ผมมัน งงๆ กำลังแกะๆ แงะๆ งมๆอยู่ครับ

By: bongikairu
ContributoriPhone
on 5 August 2011 - 23:30 #319009

ครับ ผมก็เคยเขียน Android App อยู่ตัวนึง แต่ไม่เคยใช้หลาย Activity หน่ะครับ