繁体   English   中英

Android应用因错误关闭

[英]Android app closes with error

我的手机上安装了android应用。 每当我尝试打开该应用程序时,它将关闭并显示一条消息“该应用程序已意外停止”。 我试图从错误日志中找到原因,但是我无法理解出了什么问题。 这是日志中的错误部分:

E/Web Console(13393): Uncaught TypeError: Object [object Object] has no method 'scroller' at https://xxxx.xxxx.com/assets/js/m/common.js:2

E/Web Console(13393): Uncaught ReferenceError: module is not defined at https://xxxx.xxxx.com/assets/js/m/common.js:4

E/AndroidRuntime(13393): at com.myapp.MainActivity.urlSplit(MainActivity.java:490)

E/AndroidRuntime(13393): at com.myapp.MainActivity$WebFragment$1.onPageFinished(MainActivity.java:470)

谁能帮助我了解可能出了什么问题? 这是我的MainActivity代码:

private DrawerLayout mDrawerLayout;
private CharSequence mTitle;
private SparseArray<Group> groups = new SparseArray<Group>();
private ExpandableListView drawerList;
private static SlidingMenu menu;
static String holdchildname;
private MyExpandableListAdapter adapter;
private static int screenWidth;
private static LayoutParams params;
static Intent in;
static String holdurl;
private static long back_pressed;
private WebView webView;
private SharedPreferences mSharedPrefernce1;
static Editor e;
private static String checkable = null;

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();
    setContentView(R.layout.activity_main);
    Display display = getWindowManager().getDefaultDisplay();
    screenWidth = display.getWidth();
    createData();
    mTitle = getTitle();
    final Context context1 = this;
    mSharedPrefernce1 = context1.getSharedPreferences("MySettings1", 0);
    e = mSharedPrefernce1.edit();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerList = (ExpandableListView) findViewById(R.id.left_drawer);
    params = drawerList.getLayoutParams();
    mDrawerLayout.removeView(drawerList);
    setBehindContentView(drawerList);
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);
    adapter = new MyExpandableListAdapter(this, groups);
    drawerList.setAdapter(adapter);
    drawerList.setOnGroupClickListener(new DrawerGroupClickListener());
    drawerList.setOnChildClickListener(new DrawerChildClickListener());
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    menu = this.getSlidingMenu();
    params.width = 0;
    menu.setBehindWidth(params.width);
    drawerList.setLayoutParams(params);
    if (savedInstanceState == null) {
        selectItem(12);
    }
}

public void createData() {
    Group group0 = new Group("Home");
    groups.append(0, group0);

    Group group1 = new Group("Sales");
    group1.children.add("Leads");
    group1.children.add("Accounts");
    group1.children.add("Contacts");
    group1.children.add("Deals");
    groups.append(1, group1);

    Group group2 = new Group("Marketing");
    group2.children.add("Campaigns");
    groups.append(2, group2);

    Group group3 = new Group("Collaboration");
    group3.children.add("Notifications");
    groups.append(3, group3);

    Group group4 = new Group("Activities");
    groups.append(4, group4);

    Group group5 = new Group("Calendar");
    groups.append(5, group5);

    Group group6 = new Group("Reports");
    groups.append(6, group6);

    Group group7 = new Group("Bookmarks");
    groups.append(7, group7);

    Group group8 = new Group("Logout");
    groups.append(8, group8);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.layout.activity_menu, menu);
    MenuItem autoLeadMenuItem = menu.findItem(R.id.menu_set);
    Intent intent = new Intent(this, PhoneService.class);
    try {
        checkable = mSharedPrefernce1.getString("checkable", null);
        if (checkable == null) {
            startService(intent);
            e.putString("checkable", "start");
            e.commit();
            autoLeadMenuItem.setChecked(true);
            Toast.makeText(MainActivity.this, "Lead by incoming call On",
                    Toast.LENGTH_SHORT).show();
        }
        if (checkable == "start") {
            startService(intent);
            autoLeadMenuItem.setChecked(true);
        }
        if (checkable == "stop") {
            stopService(intent);
            autoLeadMenuItem.setChecked(false);
        }
    } catch (Exception ex) {
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public void onResume() {
    super.onResume();
    if (holdurl != null) {
        selectItem(0);
    }
}

@Override
public void onPause() {
    super.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    holdurl = null;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    return super.onPrepareOptionsMenu(menu);
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_refresh:
        selectItem(12);
        Toast.makeText(MainActivity.this, "Reload", Toast.LENGTH_SHORT)
                .show();
        return true;
    case R.id.menu_cache:
        try {
            android.webkit.CookieManager.getInstance().removeAllCookie();
            Toast.makeText(MainActivity.this, "Clean Cache",
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        selectItem(12);
        return true;
    case R.id.menu_set:
        Intent intent = new Intent(this, PhoneService.class);
        if (item.isChecked()) {
            item.setChecked(false);
            stopService(intent);
            e.putString("checkable", "stop");
            Toast.makeText(MainActivity.this, "Lead by incoming call Off",
                    Toast.LENGTH_SHORT).show();
        } else {
            item.setChecked(true);
            startService(intent);
            e.putString("checkable", "start");
            Toast.makeText(MainActivity.this, "Lead by incoming call On",
                    Toast.LENGTH_SHORT).show();
        }
        e.commit();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

private class DrawerChildClickListener implements OnChildClickListener {

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        if (groupPosition == 1) {
            if (childPosition == 0) {
                selectItem(1);
                holdchildname = "leads";
            }
            if (childPosition == 1) {
                holdchildname = "accounts";
                selectItem(2);
            }
            if (childPosition == 2) {
                holdchildname = "contacts";
                selectItem(3);
            }
            if (childPosition == 3) {
                holdchildname = "deals";
                selectItem(4);
            }
        } else if (groupPosition == 2) {
            if (childPosition == 0) {
                holdchildname = "campaigns";
                selectItem(5);
            }
        } else if (groupPosition == 3) {
            if (childPosition == 0) {
                holdchildname = "notifications";
                selectItem(6);
            }
        }
        return false;
    }
}

private class DrawerGroupClickListener implements OnGroupClickListener {

    @Override
    public boolean onGroupClick(ExpandableListView arg0, View arg1,
            int arg2, long arg3) {
        if (arg2 == 0) {
            holdchildname = "dashboard";
            selectItem(0);
        }
        if (arg2 == 4) {
            holdchildname = "activities";
            selectItem(7);
        }
        if (arg2 == 5) {
            holdchildname = "calendar";
            selectItem(8);
        }

        if (arg2 == 6) {
            holdchildname = "reports";
            selectItem(9);
        }

        if (arg2 == 7) {
            holdchildname = "bookmarks";
            selectItem(10);
        }

        if (arg2 == 8) {
            holdchildname = "logout";
            selectItem(11);
        }
        return false;
    }
}

public void selectItem(int position) {
    FragmentManager fragmentManager;
    fragmentManager = getSupportFragmentManager();
    Fragment fragment = new WebFragment(position);
    fragmentManager.beginTransaction()
            .replace(R.id.content_frame, fragment).commit();
    setTitle("My App");
    if (position != 11)
        setBehindContentView(drawerList, params);
    drawerList.setItemChecked(position, true);
    if (menu.isMenuShowing())
        menu.toggle();
}

public void sideLayout(Boolean status) {
    if (status == true) {
        params.width = (int) (screenWidth / 1.5);
    } else {
        params.width = 0;
    }
    setBehindContentView(drawerList);
    menu.setBehindWidth(params.width);
    drawerList.setLayoutParams(params);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getSupportActionBar().setTitle(mTitle);
}

@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

private class WebFragment extends Fragment {
    private String currentURL = "https://xxxx.xxxx.com";
    private ProgressBar progressBar;
    private boolean logoutStatus = false;

    private WebFragment(int position) {
        if (position == 0) {
            currentURL = currentURL + "/dashboard";
        }
        if (position == 1) {
            currentURL = currentURL + "/leads";
        }
        if (position == 2) {
            currentURL = currentURL + "/accounts";
        }
        if (position == 3) {
            currentURL = currentURL + "/contacts";
        }
        if (position == 4) {
            currentURL = currentURL + "/deals";
        }
        if (position == 5) {
            currentURL = currentURL + "/campaigns";
        }
        if (position == 6) {
            currentURL = currentURL + "/notifications";
        }
        if (position == 7) {
            currentURL = currentURL + "/events/activities";
        }
        if (position == 8) {
            currentURL = currentURL + "/calendar";
        }
        if (position == 9) {
            currentURL = currentURL + "/reports";
        }
        if (position == 10) {
            currentURL = currentURL + "/bookmarks";
        }
        if (position == 11) {
            currentURL = currentURL + "/users/logout";
        }
        if (position == 12) {
            currentURL = "https://xxxx.xxxx.com/?agent=apk";
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.mainfragment_layout, container,
                false);
        if (currentURL != null) {
            webView = (WebView) v.findViewById(R.id.webView1);
            webView.removeAllViews();
            progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setBackgroundColor(Color.parseColor("#4B5973"));
            webView.setHorizontalScrollBarEnabled(false);
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setUseWideViewPort(true);
            webView.getSettings().setSupportZoom(true);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(
                    true);
            webView.getSettings().setSupportMultipleWindows(true);
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageStarted(WebView webview, String url,
                        Bitmap favicon) {
                    progressBar.setVisibility(View.VISIBLE);
                    super.onPageStarted(webview, url, favicon);
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView webview,
                        String url) {
                    progressBar.setVisibility(View.VISIBLE);
                    if (logoutStatus == true) {
                        webview.loadUrl("https://xxxx.xxxx.com/?agent=apk");
                        logoutStatus = false;
                        holdchildname = "home";
                        sideLayout(false);
                    } else {
                        webview.loadUrl(url);
                        sideLayout(true);
                    }
                    return true;
                }

                @Override
                public void onReceivedError(WebView view, int errorCode,
                        String description, String failingUrl) {
                    String white = "#FFFFFF";
                    String summary = "<html><body><center><strong><font size="
                            + 10
                            + "><br><br><br><br><br><br><br><br><br><br><font color="
                            + white
                            + ">Could not connect to the server.</font><br><a href="
                            + failingUrl
                            + ">Reload</a></font></strong></center></body></html>";
                    webView.loadData(summary, "text/html", null);
                    sideLayout(false);
                }

                @Override
                public void onPageFinished(WebView webview, String url) {
                    super.onPageFinished(webview, url);
                    progressBar.setVisibility(View.INVISIBLE);
                    if (url.equals("https://xxxx.xxxx.com/dashboard")) {
                        sideLayout(true);
                    }
                    urlSplit(url);
                }
            });
            webView.setWebChromeClient(new WebChromeClient());
            webView.requestFocus(View.FOCUS_DOWN);
            webView.loadUrl(currentURL);
            params.width = (int) (screenWidth / 1.5);
            if (currentURL
                    .equals("https://xxxx.xxxx.com/users/logout")) {
                logoutStatus = true;
            }
        }
        return v;
    }
}

public void urlSplit(String str) {
    String[] temp = null;
    String delimeter = "/";
    temp = str.split(delimeter);
    if (!temp[3].equals(null)) {
        holdchildname = temp[3];
        drawerList.setItemChecked(6, true);
    }
}

@Override
public void onBackPressed() {
    if (back_pressed + 2000 > System.currentTimeMillis())
        super.onBackPressed();
    else
        Toast.makeText(getBaseContext(), "Press again to exit",
                Toast.LENGTH_SHORT).show();
    back_pressed = System.currentTimeMillis();
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    View view = getCurrentFocus();
    boolean ret = super.dispatchTouchEvent(event);
    if (view instanceof EditText) {
        View w = getCurrentFocus();
        int scrcoords[] = new int[2];
        w.getLocationOnScreen(scrcoords);
        float x = event.getRawX() + w.getLeft() - scrcoords[0];
        float y = event.getRawY() + w.getTop() - scrcoords[1];
        if (event.getAction() == MotionEvent.ACTION_UP
                && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w
                        .getBottom())) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus()
                    .getWindowToken(), 0);
        }
    }
    return ret;
}

有没有一种方法可以避免上述错误,使应用程序可以正常运行?

您的commons.js中的javascript代码在commons.js文件的第2行和第4行中包含错误。 这就是日志输出所显示的内容。 修复它们后,该应用程序将不再崩溃。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM