我在尝试运行我的应用时遇到空指针异常。在清单xml文件中似乎没问题。这是活动java代码:
package com.shale.activities;
import java.util.Arrays;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.facebook.FacebookException;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.facebook.widget.LoginButton.OnErrorListener;
import com.shale.services.DBConnection;
import com.shale.services.Login;
import com.shale.shaleapp.R;
public class MainActivity extends Activity {
private String TAG = "MainActivity";
private Context context;
private Button loginBtn;
private Intent mainPage = new Intent(this, MainPage.class);
private EditText userEmail;
private EditText userPass;
private LoginButton fbLoginBtn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DBConnection.initialize(this);
loginBtn = (Button) findViewById(R.id.loginBtn);
userEmail = (EditText) findViewById(R.id.emailInput);
userPass = (EditText) findViewById(R.id.passwordInput);
fbLoginBtn = (LoginButton) findViewById(R.id.authButton);
context = this;
loginBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
Login login = new Login(userEmail.getText().toString(), userPass.getText().toString());
boolean userAllowed = login.loginUser();
if(userAllowed)
{
startActivity(mainPage);
}
else
{
Toast.makeText(context, "Invalide email and password", Toast.LENGTH_LONG).show();
}
}
});
fbLoginBtn.setOnErrorListener(new OnErrorListener()
{
@Override
public void onError(FacebookException error)
{
Log.i(TAG, "Error " + error.getMessage());
}
});
// set permission list, Don't foeget to add email
fbLoginBtn.setReadPermissions(Arrays.asList("basic_info","email"));
// session state call back event
fbLoginBtn.setSessionStatusCallback(new Session.StatusCallback()
{
@Override
public void call(Session session, SessionState state, Exception exception)
{
if (session.isOpened())
{
Log.i(TAG,"Access Token"+ session.getAccessToken());
Request.executeMeRequestAsync(session, new Request.GraphUserCallback()
{
@Override
public void onCompleted(GraphUser user,Response response)
{
if (user != null)
{
Log.i(TAG,"User ID "+ user.getId());
Log.i(TAG,"Email "+ user.asMap().get("email"));
Login login = new Login(user.asMap().get("email").toString(), "facebookPass");
logIn();
}
}
});
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
private void logIn()
{
Intent mainPage = new Intent(this, MainPage.class);
startActivity(mainPage);
}
}
这是清单xml代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.shale.shaleapp"
android:versionCode="1"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.shale.activities.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.shale.activities.MainPage"
android:label="@string/title_activity_main_page" >
</activity>
<meta-data android:value="@string/fb_app_id" android:name="com.facebook.sdk.ApplicationId"/>
<activity android:label="@string/app_name" android:name="com.facebook.LoginActivity"></activity>
</application>
</manifest>
这是logCat输出:
05-06 06:25:01.618: E/AndroidRuntime(1690): FATAL EXCEPTION: main
05-06 06:25:01.618: E/AndroidRuntime(1690): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.shale.shaleapp/com.shale.activities.MainActivity}: java.lang.NullPointerException
05-06 06:25:01.618: E/AndroidRuntime(1690): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
05-06 06:25:01.618: E/AndroidRuntime(1690): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-06 06:25:01.618: E/AndroidRuntime(1690): at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-06 06:25:01.618: E/AndroidRuntime(1690): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-06 06:25:01.618: E/AndroidRuntime(1690): at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 06:25:01.618: E/AndroidRuntime(1690): at android.os.Looper.loop(Looper.java:137)
05-06 06:25:01.618: E/AndroidRuntime(1690): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-06 06:25:01.618: E/AndroidRuntime(1690): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 06:25:01.618: E/AndroidRuntime(1690): at java.lang.reflect.Method.invoke(Method.java:511)
05-06 06:25:01.618: E/AndroidRuntime(1690): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-06 06:25:01.618: E/AndroidRuntime(1690): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-06 06:25:01.618: E/AndroidRuntime(1690): at dalvik.system.NativeStart.main(Native Method)
05-06 06:25:01.618: E/AndroidRuntime(1690): Caused by: java.lang.NullPointerException
05-06 06:25:01.618: E/AndroidRuntime(1690): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
05-06 06:25:01.618: E/AndroidRuntime(1690): at android.content.ComponentName.<init>(ComponentName.java:75)
05-06 06:25:01.618: E/AndroidRuntime(1690): at android.content.Intent.<init>(Intent.java:3301)
05-06 06:25:01.618: E/AndroidRuntime(1690): at com.shale.activities.MainActivity.<init>(MainActivity.java:33)
05-06 06:25:01.618: E/AndroidRuntime(1690): at java.lang.Class.newInstanceImpl(Native Method)
05-06 06:25:01.618: E/AndroidRuntime(1690): at java.lang.Class.newInstance(Class.java:1319)
05-06 06:25:01.618: E/AndroidRuntime(1690): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
05-06 06:25:01.618: E/AndroidRuntime(1690): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
05-06 06:25:01.618: E/AndroidRuntime(1690): ... 11 more
它表示MainActivity存在问题,但我找不到它。感谢!!!
答案 0 :(得分:4)
这是因为
private Intent mainPage = new Intent(this, MainPage.class);
将其移至onCreate()
答案 1 :(得分:4)
在以下代码行中,
private Intent mainPage = new Intent(this, MainPage.class);
你试图在启动活动之前传递MainActivity的上下文(即this)。所以你可以在onCreate之前声明意图,如果你想要它是全局的并在onCreate中定义它,以便它获得一个适当的上下文来启动另一个活动。
答案 2 :(得分:2)
尚未调用构造函数,因此使用this
将给出nullpointerexception。请勿尝试初始化mainPage
。相反,移动你的
Intent mainPage = new Intent(MainActivity.this, MainPage.class);
就在startActivity(mainPage);
答案 3 :(得分:1)
private Intent mainPage;// before onCreate() inside activity class
在onCreate()
mainPage = new Intent(MainActivity.this, MainPage.class);
你有NullPointerException因为你在onCreate()之前初始化了你的意图;