2015-05-04 63 views
-2

我想從安卓:setVisibility不行

類更改視圖 linearlayout

但不起作用此代碼

main.java

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ComNet.readDb(); 
    } 
} 

和:

ComNet.java

public class ComNet { 
    public static Context context; 

    public static void readDb() { 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View viewMain = inflater.inflate(R.layout.main, null); 
     LinearLayout lnrPart = (LinearLayout) viewMain.findViewById(R.id.lnrPart); 
     lnrPart.setVisibility(View.GONE); 
    } 
} 

如何從main.xml類(ComNet)改變lnrPart

回答

2

將活動rootView發送到readDB函數。

main.java

public class MainActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ComNet.readDb(findViewById(R.id.main)); 
    } 
} 

ComNet.java

public static void readDb(View viewMain) { 

    LinearLayout lnrPart = (LinearLayout) viewMain.findViewById(R.id.lnrPart); 
    viewMain.setVisibility(View.GONE); 
} 
+0

謝謝,但此行錯誤:android.R.layout.main,錯誤:主要解決不了或無法在現場 – Mohammad

+1

修正了代碼...只是抹去android。在R.layout.main之前 – ilan

3
View viewMain = inflater.inflate(R.layout.main, null); 

這一行,你要創建一個新View,隨着main.xml內容。該對象與您在MainActivity屏幕上看到的對象不同。 setVisibility正在工作。您在錯誤的實例上調用它