当我处于另一种布局时,可以更改布局可见性吗?

时间:2013-07-11 12:55:11

标签: android

在这里,我创建了四个 xml 文件main.xmltest1.xmltest2.xmltest3.xml

他们是在spinner访问的,但我收到了一个例外。 我也给了 logcat

请帮帮我。

String[] lan = { "test1", "test2", "test3" };

Spinner spn = (Spinner) findViewById(R.id.spinner1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, lan);

spn.setAdapter(adapter);

spn.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
        if (pos == 0) {
            findViewById(R.layout.test1).setVisibility(View.VISIBLE);
        } else if (pos == 1) {
            findViewById(R.layout.test2).setVisibility(View.VISIBLE);
        } else if (pos == 2) {
            findViewById(R.layout.test3).setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    }
});

logcat的:

07-11 18:09:39.480: E/AndroidRuntime(21200): FATAL EXCEPTION: main
07-11 18:09:39.480: E/AndroidRuntime(21200): java.lang.NullPointerException
07-11 18:09:39.480: E/AndroidRuntime(21200):    at com.venky.innverlayoutchangevisibility.MainActivity$1.onItemSelected(MainActivity.java:30)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at android.widget.AdapterView.access$200(AdapterView.java:42)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at android.os.Handler.handleCallback(Handler.java:587)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at android.os.Looper.loop(Looper.java:130)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at android.app.ActivityThread.main(ActivityThread.java:3691)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at java.lang.reflect.Method.invokeNative(Native Method)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at java.lang.reflect.Method.invoke(Method.java:507)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

我几乎肯定你不能这样做。根据{{​​3}}

  

查找由onCreate(Bundle)中处理的XML中的id属性标识的视图。

这用于查找当前虚增View内的layout,我将其理解为单独的文件。您需要使用the docs。或者在这种情况下,如果您在选择项目后不需要使用任何当前setContentView(),请致电Views

 @Override
    public void onItemSelected(AdapterView<?> parent, View v, int pos,
            long id) {
        int layout;   // create int for layout to inflate
        if (pos == 0) {
            layout = R.layout.test1;   // assign int according to the layout needed
        } else if (pos == 1) {
            layout = R.layout.test2;
        } else if (pos == 2) {
           layout = R.layout.test3;
        }
        setContentView(layout);      // inflate the layout
    }

注意这样做会将View layout inflate中的View设置为默认值layoutonCreate() s layoutsinclude中膨胀的1}}将不存在。我不确定这是不是你要找的。如果没有,请告诉我。

修改

现在我知道这些layout findViewById(R.layout.test3) 已在您的主<include>中进行了layout,您不想使用

id

当您使用id标记将其插入主 findViewById(R.id.test3) 时,请为他们{{1}}并在

中使用{{1}}
{{1}}

答案 1 :(得分:0)

在此处输入代码您无法使用来自不同布局的组件。您需要做的是定义LayoutInflator并从中开始工作。 R.id.yourComponent只能访问onCreate方法中定义的组件(大部分),如:

setContentView(R.layout.main);

要执行您需要的操作,请为每个布局创建一个充气器(注意:要使其工作,请为test1.xml,test2.xml,test3.xml中的每个主要LinearLayouts提供一个ID。访问,而不是文件本身):

String[] lan = { "test1", "test2", "test3" };

Spinner spn = (Spinner) findViewById(R.id.spinner1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_dropdown_item, lan);

//Duplicate this code for every .xml layout inside your onItemSelected
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View firstView = inflater.inflate(R.layout.test1, null); //inflates test1.xml
LinearLayout test1Layout = (LinearLayout) firstView.findViewById(R.id.idOfTheLayout); //This finds a component inside your test1.xml file. As mentioned, give the highest Layout which holds all your components an ID.
test1Layout.setVisibility(View.VISIBLE);


spn.setAdapter(adapter);

spn.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int pos,
            long id) {
        if (pos == 0) {
            findViewById(R.layout.test1).setVisibility(View.VISIBLE);
        } else if (pos == 1) {
            findViewById(R.layout.test2).setVisibility(View.VISIBLE);
        } else if (pos == 2) {
            findViewById(R.layout.test3).setVisibility(View.VISIBLE);
        }
    }