พอดีว่า ผมเขียนโปรแกรมบน 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 ค่าให้ไม่ได้ครับ

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

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

bobe Fri, 05/08/2011 - 09:27

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

}

ส่วนนี้เป็น 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);}
}

}

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

  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}

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

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

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

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

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

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

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

ลองแล้วครับ เหมือนจะดีขึ้น เพราะ 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นี้

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

bobe Fri, 05/08/2011 - 14:12

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

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

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

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

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

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

startActivityForResult(Intent,IPC_ID);

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

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

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