我有一个问题,我无法让我的应用程序处理说明8 完美地在Galaxy S4上完美运行,没有任何错误,以及旧的HTC手机。
我得到的错误如下
02-21 09:21:28.352: W/dalvikvm(6130): threadid=1: thread exiting with uncaught exception (group=0x41bab930)
02-21 09:21:28.357: E/AndroidRuntime(6130): FATAL EXCEPTION: main
02-21 09:21:28.357: E/AndroidRuntime(6130): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.taskey.me2team/com.taskey.me2team.StartHere}: java.lang.NullPointerException
02-21 09:21:28.357: E/AndroidRuntime(6130): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
02-21 09:21:28.357: E/AndroidRuntime(6130): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
02-21 09:21:28.357: E/AndroidRuntime(6130): at android.app.ActivityThread.access$700(ActivityThread.java:150)
02-21 09:21:28.357: E/AndroidRuntime(6130): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
02-21 09:21:28.357: E/AndroidRuntime(6130): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 09:21:28.357: E/AndroidRuntime(6130): at android.os.Looper.loop(Looper.java:137)
02-21 09:21:28.357: E/AndroidRuntime(6130): at android.app.ActivityThread.main(ActivityThread.java:5279)
02-21 09:21:28.357: E/AndroidRuntime(6130): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 09:21:28.357: E/AndroidRuntime(6130): at java.lang.reflect.Method.invoke(Method.java:511)
02-21 09:21:28.357: E/AndroidRuntime(6130): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-21 09:21:28.357: E/AndroidRuntime(6130): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-21 09:21:28.357: E/AndroidRuntime(6130): at dalvik.system.NativeStart.main(Native Method)
02-21 09:21:28.357: E/AndroidRuntime(6130): Caused by: java.lang.NullPointerException
02-21 09:21:28.357: E/AndroidRuntime(6130): at com.taskey.me2team.StartHere.isOnline(StartHere.java:303)
02-21 09:21:28.357: E/AndroidRuntime(6130): at com.taskey.me2team.StartHere.onCreate(StartHere.java:124)
02-21 09:21:28.357: E/AndroidRuntime(6130): at android.app.Activity.performCreate(Activity.java:5276)
02-21 09:21:28.357: E/AndroidRuntime(6130): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
02-21 09:21:28.357: E/AndroidRuntime(6130): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
02-21 09:21:28.357: E/AndroidRuntime(6130): ... 11 more
下面是代码:
Splash Activity显示启动画面,然后显示在主活动
上package com.mycompany.myappname;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
public class SplashActivity extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 1600;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//getWindow().getDecorView().getBackground().setDither(true);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
Intent i = new Intent(SplashActivity.this, StartHere.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}
现在我的主要行为如下
package com.mycompany.myappname;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
//import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
@SuppressLint("SetJavaScriptEnabled")
public class StartHere extends Activity {
final Activity activity = this;
MenuItem menu_LogIn, menu_SignUp, go_Back, go_Forward;
private SharedPreferences userCredentials;
private Boolean isLoggedIn;
private String m2tUser;
private String webURL;
WebView webView;
public MyData getUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_here);
getUser = ((MyData)getApplicationContext());
userCredentials = getApplicationContext().getSharedPreferences("me2team", MODE_PRIVATE);
isLoggedIn = userCredentials.getBoolean("LIStatus", false);
m2tUser = userCredentials.getString("User", "username");
if (!isLoggedIn)
{
Editor m2tEditor = userCredentials.edit();
m2tEditor.putBoolean("PUStatus", false);
m2tEditor.putString("Pswd", "5&GEthtFE5");
m2tEditor.commit();
MyData setLoggedOff = ((MyData)getApplicationContext());
setLoggedOff.setCredentialsUser(m2tUser);
setLoggedOff.setStateOff();
setLoggedOff.setPremiumUserOff();
if (m2tUser.contains("username"))
webURL = getResources().getString(R.string.welcomeURL);
else
webURL = getResources().getString(R.string.splashURL);
}
else
{
MyData loggingOn = ((MyData)getApplicationContext());
loggingOn.setCredentialsUser(m2tUser);
loggingOn.setCredentialsPwd(userCredentials.getString("Pswd", "89798978"));
loggingOn.setStateOn();
boolean puStatus = userCredentials.getBoolean("PUStatus", false);
if (puStatus)
loggingOn.setPremiumUserOff();
else
loggingOn.setPremiumUserOn();
}
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSavePassword(false);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(false); //as per Janet's request
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setInitialScale(70);
WebSettings settings = webView.getSettings();
settings.setDomStorageEnabled(true);
webView.loadUrl(webURL);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
{
activity.setTitle(R.string.app_name);
}
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
});
if (!isOnline())
{
Toast msg = Toast.makeText(StartHere.this,
"You dont seem to be connected to the Internet.", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, 0, 0);
msg.show();
}
webView.loadUrl(webURL);
}
public void goToTeamPage()
{
Intent intent = new Intent(this, TeamPage.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.start_here, menu);
menu_LogIn = menu.findItem(R.id.currentlyLoggedOut);
menu_SignUp = menu.findItem(R.id.signUp);
go_Back = menu.findItem(R.id.goBack);
go_Forward = menu.findItem(R.id.goForward);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu_LogIn.setVisible(true);
go_Back.setVisible(true);
go_Forward.setVisible(true);
menu_SignUp.setVisible(true);
return true;
}
@Override
public boolean onOptionsItemSelected( MenuItem item )
{
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case R.id.currentlyLoggedOut:
{
Intent intent = new Intent(this, Me2TeamLogIn.class);
startActivity(intent);
return true;
}
case R.id.signUp:
{
Intent intent = new Intent(this, SignUp.class);
startActivity(intent);
return true;
}
case R.id.goBack:
{
if(webView.canGoBack() == true)
{
webView.goBack();
}
return true;
}
case R.id.goForward:
{
if(webView.canGoForward() == true)
{
webView.goForward();
}
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onStart()
{
//Log.wtf("TimV", "OnStart");
super.onStart();
}
@Override
protected void onResume()
{
//Log.wtf("TimV", "OnResume");
if (isLoggedIn)
{
goToTeamPage();
}
super.onResume();
}
@Override
protected void onPause()
{
//Log.wtf("TimV", "OnPause");
super.onPause();
}
@Override
protected void onRestart()
{
super.onRestart();
if (!isOnline())
{
Toast msg = Toast.makeText(StartHere.this,
"You dont seem to be connected to the Internet.", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, 0, 0);
msg.show();
}
MyData loginData = ((MyData)getApplicationContext());
isLoggedIn = loginData.getState();
if (isLoggedIn)
{
// Log.wtf("TimV", "going to team page");
Intent intent = new Intent(this, TeamPage.class);
startActivity(intent);
}
else
{
// Log.wtf("TimV", "going to splash page");
webURL = getResources().getString(R.string.splashURL);
webView.loadUrl(webURL);
}
}
public boolean isOnline() {
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isWifiConn = networkInfo.isConnected();
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isMobileConn = networkInfo.isConnected();
if (isWifiConn)
return true;
else
{
if (isMobileConn)
return true;
else
return false;
}
}
}
这个问题让我感到难过,因为所有谷歌上的搜索都没有提供答案。
答案 0 :(得分:3)
我假设您的NPE发生在
boolean isMobileConn = networkInfo.isConnected();
?这是因为Note 8是平板电脑。 TYPE_MOBILE可以返回null,因为没有移动连接(并且没有移动连接)。我原本预计其他平板电脑会发生类似的崩溃事件。
来自http://developer.android.com/reference/android/net/ConnectivityManager.html#getNetworkInfo(int)的文档:
返回所请求网络类型的NetworkInfo对象,如果是,则返回null 设备不支持该类型。
将networkInfo(第二个)视为null,因为没有移动连接。
答案 1 :(得分:0)
感谢“La Dee Dah”,我看到了这个问题。
我正在检查平板电脑上的移动连接,因此同意,它永远不会起作用。
这是不正确的方式:
public boolean isOnline() {
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isWifiConn = networkInfo.isConnected();
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isMobileConn = networkInfo.isConnected();
if (isWifiConn)
return true;
else
{
if (isMobileConn)
return true;
else
return false;
}
}
这里是正确的方式,感谢bvanvelsen's回答
public boolean isOnline() {
final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
int type = networkInfo.getType();
String typeName = networkInfo.getTypeName();
boolean connected = networkInfo.isConnected();
if (connected)
return true;
else
return false;
}