条件达成时不显示Android正确视图

时间:2014-07-02 21:07:30

标签: android view

我有几个问题需要回答这个问题,但我会先说出最初的问题,然后再说出其他问题。我的应用程序是一个实用/商业应用程序供现场使用。我有5个标签。联系我们标签有问题。在此选项卡中,我加载了一个名为About.class的活动,此类加载一个线性布局(垂直),带有一个Web视图和一个或两个按钮(调用|| email),具体取决于该设备是平板电脑还是手机(按钮以水平线性布局包裹)当然这是确定购买屏幕尺寸。我在About.class中执行此操作,但无论结果是否加载了相同的xml文件。这是About.class:

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.webkit.WebView;
import android.util.Log;
public class About extends Activity 
{
    WebView mbrowser;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Integer c = (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK);
        Integer sml = Configuration.SCREENLAYOUT_SIZE_SMALL;
        Integer lrg = Configuration.SCREENLAYOUT_SIZE_LARGE;
        Integer nrm = Configuration.SCREENLAYOUT_SIZE_NORMAL;
        Integer xlrg = Configuration.SCREENLAYOUT_SIZE_XLARGE;

        Boolean is_tab = isTablet(c,lrg);

        Log.w("SMALL: ", sml.toString());
        Log.w("NORMAL: ", nrm.toString());
        Log.w("LARGE: ", lrg.toString());
        Log.w("XLARGE: ", xlrg.toString());
        Log.w("DEVICE_SIZE", c.toString()); 
        Log.w("IS_TABLET: ", is_tab.toString());

        if (is_tab == true)
        {
            setContentView(R.layout.contact_tablet);
            mbrowser = (WebView) findViewById(R.id.view_about); 
            mbrowser.loadUrl("file:///android_asset/about/about.html", null);
        }
        else if (is_tab == false)
        {
            setContentView(R.layout.contact);
            mbrowser = (WebView) findViewById(R.id.view_about); 
            mbrowser.loadUrl("file:///android_asset/about/about.html", null);
        }



    }
     public static boolean isTablet(Integer c, Integer lrg) 
        {

            if (c >= lrg)
            {

            return true;
            }
            else
            {

                return false;
            }
         }

}

现在有趣的是要注意那些日志在log cat中呈现!!!所以有可能没有分配这些值。但是我在MainActivity类中发布了相同的代码(这会带来另一个问题;为什么会从那里显示日志?)并且在平板电脑(Nexus 7)上运行时会呈现以下内容:

小:1 正常:2
大:3
XLARGE:4
DEVICE_SIZE 3
IS_TABLET:真实

现在,如果未在About.class中分配值,如果if / if else语句没有被转换,那么如何呈现视图?

提前感谢您的帮助。我还有另外一个问题,但是我会发表另一个问题:)

STACK TRACE

07-03 07:55:29.283: I/LOADED:(8031): SPLASHSCREEN
07-03 07:55:34.293: I/ACTION:(8031): STARTING MAIN INTENT
07-03 07:55:34.343: W/SMALL:(8031): 1
07-03 07:55:34.343: W/NORMAL:(8031): 2
07-03 07:55:34.343: W/LARGE:(8031): 3
07-03 07:55:34.343: W/XLARGE:(8031): 4
07-03 07:55:34.343: W/DEVICE_SIZE(8031): 3
07-03 07:55:34.343: W/IS_TABLET:(8031): true
07-03 07:55:34.373: I/ORIENTATION:(8031): HOME PORTRAIT: 1
07-03 07:55:34.373: I/LOADED:(8031): TAB HOME SET
07-03 07:55:34.383: I/LOADED:(8031): TAB FB SET
07-03 07:55:34.383: I/LOADED:(8031): TAB TW SET
07-03 07:55:34.393: I/LOADED:(8031): TAB CONTACT SET
07-03 07:55:34.393: I/LOADED:(8031): TAB LOCATIONS SET
07-03 07:55:45.173: I/LibraryLoader(8031): Expected native library version number "",actual native library version number ""
07-03 07:55:45.173: I/chromium(8031): [INFO:library_loader_hooks.cc(116)] Chromium logging enabled: level = 0, default verbosity = 0
07-03 07:55:45.183: I/BrowserStartupController(8031): Initializing chromium process, renderers=0
07-03 07:55:45.183: E/AudioManagerAndroid(8031): BLUETOOTH permission is missing!
07-03 07:55:45.253: W/chromium(8031): [WARNING:proxy_service.cc(890)] PAC support disabled because there is no system implementation
07-03 07:55:45.263: W/dalvikvm(8031): threadid=1: thread exiting with uncaught exception (group=0x41870ba8)
07-03 07:55:45.273: E/AndroidRuntime(8031): FATAL EXCEPTION: main
07-03 07:55:45.273: E/AndroidRuntime(8031): Process: com.laserpros.tech_to_go, PID: 8031
07-03 07:55:45.273: E/AndroidRuntime(8031): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.laserpros.tech_to_go/com.laserpros.tech_to_go.FourthActivity}: java.lang.NullPointerException
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2024)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.widget.TabHost.setCurrentTab(TabHost.java:413)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.view.View.performClick(View.java:4438)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.view.View$PerformClick.run(View.java:18422)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.os.Handler.handleCallback(Handler.java:733)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.os.Looper.loop(Looper.java:136)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.app.ActivityThread.main(ActivityThread.java:5001)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at java.lang.reflect.Method.invokeNative(Native Method)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at java.lang.reflect.Method.invoke(Method.java:515)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at dalvik.system.NativeStart.main(Native Method)
07-03 07:55:45.273: E/AndroidRuntime(8031): Caused by: java.lang.NullPointerException
07-03 07:55:45.273: E/AndroidRuntime(8031):     at com.laserpros.tech_to_go.About.onCreate(FourthActivity.java:24)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.app.Activity.performCreate(Activity.java:5231)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-03 07:55:45.273: E/AndroidRuntime(8031):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-03 07:55:45.273: E/AndroidRuntime(8031):     ... 18 more
07-03 07:55:49.743: I/Process(8031): Sending signal. PID: 8031 SIG: 9  <br/>
  <br/>

在代码解决方案中:

 setContentView(R.layout.contact); 
    mbrowser = (WebView) findViewById(R.id.view_about); 
    mbrowser.loadUrl("file:///android_asset/about/about.html", null);
    if (!is_tab)
    {
        View callBTN = findViewById(R.id.call_btn);
        callBTN.setOnClickListener(this);
    }
    View emailBTN = findViewById(R.id.email_btn);
    emailBTN.setOnClickListener(this);
    Log.w("LOADED: ", "CONTACT TAB");

1 个答案:

答案 0 :(得分:0)

您知道您可以为平板电脑创建单独的布局,让Android决定为您加载哪一个。这真的很擅长。只需将您的平板电脑布局放在名为layouts-sw600dp的文件夹中,Android就会自动将它用于平板电脑7“和更大版本。所以它看起来像这样:

res
  |-layouts
  |    |-contact.xml
  |
  |-layouts-sw600dp
  |    |-contact.xml

以下是有关资源定位的更多信息。 http://developer.android.com/guide/topics/resources/providing-resources.html