6

Androidエミュレーターでプロジェクトを実行しているときに、getApplicationContextでjava.lang.NullPointerExceptionとして例外が発生します。この問題の解決にご協力ください。

エラー

07-19 15:08:07.811: D/AndroidRuntime(366): Shutting down VM
07-19 15:08:07.811: W/dalvikvm(366): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-19 15:08:07.841: E/AndroidRuntime(366): FATAL EXCEPTION: main
07-19 15:08:07.841: E/AndroidRuntime(366): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.TfsMutualFund/org.TfsMutualFund.loading}: java.lang.NullPointerException
07-19 15:08:07.841: E/AndroidRuntime(366):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-19 15:08:07.841: E/AndroidRuntime(366):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

07-19 15:08:07.841: E/AndroidRuntime(366): Caused by: java.lang.NullPointerException
07-19 15:08:07.841: E/AndroidRuntime(366):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
07-19 15:08:07.841: E/AndroidRuntime(366):  at org.TfsMutualFund.loading.<init>(loading.java:23)

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 package="org.TfsMutualFund">   
<uses-sdk android:targetSdkVersion="8" />

<application android:name=".globalAdapter" android:icon="@drawable/icon" android:label="@string/app_name">

    <activity android:name=".loading"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.NoTitleBar"
              android:configChanges="orientation|keyboard|keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".TFSManinActivity"
    android:configChanges="orientation|keyboard|keyboardHidden"/>
</application>

Loading.java

package org.TfsMutualFund;

public class loading extends Activity{
private static ArrayAdapter<String> adapter;
private globalAdapter adpt = ((globalAdapter)getApplicationContext());
private String ServicePath = adpt.getServicePath();
private String ServiceName = adpt.getServiceName();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loading);
 if(isOnline())
    new AsyncLoad().execute();
 else
4

2 に答える 2

11

前に初期化しないでください。OnCreate()そこでコンテキストを取得することはできません。で実行してくださいonCreate()

private globalAdapter adpt;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loading);
    adpt  = ((globalAdapter)getApplicationContext());
    ...
}
于 2012-07-19T09:53:53.303 に答える
2

1. onCreateの前に以下を初期化せず、ビューに最初にIDを取得させます

private globalAdapter adpt = ((globalAdapter)getApplicationContext());
private String ServicePath = adpt.getServicePath();
private String ServiceName = adpt.getServiceName();

2.宣言するだけです。

private globalAdapter adpt;
private String ServicePath;  
private String ServiceName;

3.アクティビティが形成されていない場合、どのようにしてそのアクティビティのコンテキストを取得できるので、を使用しgetApplicationContext()て現在のアクティビティコンテキストを取得します。

于 2012-07-19T10:04:24.960 に答える