以下のコードは、実行時に nullPointerException で終了します。
logcat が最初で、コードが 2 番目です。
ログキャット:
07-27 09:58:13.705: D/AndroidRuntime(18164): Shutting down VM
0
7-27 09:58:13
.705: W/dalvikvm(18164): threadid=1: thread exiting with uncaught exception (group=0x40a3c1f8)
07-27 09:58:13.710: E/AndroidRuntime(18164): FATAL EXCEPTION: main
07-27 09:58:13.710: E/AndroidRuntime(18164): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liamwli.smsbusy/com.liamwli.smsbusy.Sms_busyActivity}: java.lang.NullPointerException
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.ActivityThread.access$600(ActivityThread.java:132)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.os.Looper.loop(Looper.java:137)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.ActivityThread.main(ActivityThread.java:4575)
07-27 09:58:13.710: E/AndroidRuntime(18164): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 09:58:13.710: E/AndroidRuntime(18164): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 09:58:13.710: E/AndroidRuntime(18164): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-27 09:58:13.710: E/AndroidRuntime(18164): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-27 09:58:13.710: E/AndroidRuntime(18164): at dalvik.system.NativeStart.main(Native Method)
07-27 09:58:13.710: E/AndroidRuntime(18164): Caused by: java.lang.NullPointerException
07-27 09:58:13.710: E/AndroidRuntime(18164): at com.liamwli.smsbusy.Sms_busyActivity.doCheck(Sms_busyActivity.java:142)
07-27 09:58:13.710: E/AndroidRuntime(18164): at com.liamwli.smsbusy.Sms_busyActivity.onCreate(Sms_busyActivity.java:60)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.Activity.performCreate(Activity.java:4465)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
07-27 09:58:13.710: E/AndroidRuntime(18164): ... 11 more
07-27 09:58:14.035: I/dalvikvm(18164): threadid=3: reacting to signal 3
07-27 09:58:14.055: I/dalvikvm(18164): Wrote stack traces to '/data/anr/traces.txt'
07-27 09:58:14.260: I/dalvikvm(18164): threadid=3: reacting to signal 3
07-27 09:58:14.275: I/dalvikvm(18164): Wrote stack traces to '/data/anr/traces.txt'
07-27 10:01:17.450: D/AndroidRuntime(18326): Shutting down VM
07-27 10:01:17.455: W/dalvikvm(18326): threadid=1: thread exiting with uncaught exception (group=0x40a3c1f8)
07-27 10:01:17.455: E/AndroidRuntime(18326): FATAL EXCEPTION: main
07-27 10:01:17.455: E/AndroidRuntime(18326): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liamwli.smsbusy/com.liamwli.smsbusy.Sms_busyActivity}: java.lang.NullPointerException
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.ActivityThread.access$600(ActivityThread.java:132)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.os.Looper.loop(Looper.java:137)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.ActivityThread.main(ActivityThread.java:4575)
07-27 10:01:17.455: E/AndroidRuntime(18326): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 10:01:17.455: E/AndroidRuntime(18326): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 10:01:17.455: E/AndroidRuntime(18326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-27 10:01:17.455: E/AndroidRuntime(18326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-27 10:01:17.455: E/AndroidRuntime(18326): at dalvik.system.NativeStart.main(Native Method)
07-27 10:01:17.455: E/AndroidRuntime(18326): Caused by: java.lang.NullPointerException
07-27 10:01:17.455: E/AndroidRuntime(18326): at com.liamwli.smsbusy.Sms_busyActivity.doCheck(Sms_busyActivity.java:142)
07-27 10:01:17.455: E/AndroidRuntime(18326): at com.liamwli.smsbusy.Sms_busyActivity.onCreate(Sms_busyActivity.java:60)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.Activity.performCreate(Activity.java:4465)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
07-27 10:01:17.455: E/AndroidRuntime(18326): ... 11 more
07-27 10:01:17.855: I/dalvikvm(18326): threadid=3: reacting to signal 3
07-27 10:01:17.860: I/dalvikvm(18326): Wrote stack traces to '/data/anr/traces.txt'
07-27 10:01:17.990: I/dalvikvm(18326): threadid=3: reacting to signal 3
07-27 10:01:17.995: I/dalvikvm(18326): Wrote stack traces to '/data/anr/traces.txt'
07-27 10:04:32.645: D/AndroidRuntime(18561): Shutting down VM
07-27 10:04:32.645: W/dalvikvm(18561): threadid=1: thread exiting with uncaught exception (group=0x40a3c1f8)
07-27 10:04:32.645: E/AndroidRuntime(18561): FATAL EXCEPTION: main
07-27 10:04:32.645: E/AndroidRuntime(18561): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liamwli.smsbusy/com.liamwli.smsbusy.Sms_busyActivity}: java.lang.NullPointerException
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.ActivityThread.access$600(ActivityThread.java:132)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.os.Looper.loop(Looper.java:137)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.ActivityThread.main(ActivityThread.java:4575)
07-27 10:04:32.645: E/AndroidRuntime(18561): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 10:04:32.645: E/AndroidRuntime(18561): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 10:04:32.645: E/AndroidRuntime(18561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-27 10:04:32.645: E/AndroidRuntime(18561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-27 10:04:32.645: E/AndroidRuntime(18561): at dalvik.system.NativeStart.main(Native Method)
07-27 10:04:32.645: E/AndroidRuntime(18561): Caused by: java.lang.NullPointerException
07-27 10:04:32.645: E/AndroidRuntime(18561): at com.liamwli.smsbusy.Sms_busyActivity.doCheck(Sms_busyActivity.java:140)
07-27 10:04:32.645: E/AndroidRuntime(18561): at com.liamwli.smsbusy.Sms_busyActivity.onCreate(Sms_busyActivity.java:58)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.Activity.performCreate(Activity.java:4465)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
07-27 10:04:32.645: E/AndroidRuntime(18561): ... 11 more
07-27 10:04:33.060: I/dalvikvm(18561): threadid=3: reacting to signal 3
07-27 10:04:33.065: I/dalvikvm(18561): Wrote stack traces to '/data/anr/traces.txt'
07-27 10:04:33.185: I/dalvikvm(18561): threadid=3: reacting to signal 3
07-27 10:04:33.215: I/dalvikvm(18561): Wrote stack traces to '/data/anr/traces.txt'
07-27 10:04:36.915: I/Process(18561): Sending signal. PID: 18561 SIG: 9
行 140 は mChecker.checkAccess 行で、最後の括弧の下にある新しい行です。
private void doCheck() {
mChecker.checkAccess(mLicenseCheckerCallback);
}
58 行目は doCheck メソッドの呼び出しです。
doCheck();
クラスの残りの部分は次のとおりです。
package com.liamwli.smsbusy;
import com.google.android.vending.licensing.LicenseChecker;
import com.google.android.vending.licensing.LicenseCheckerCallback;
import com.google.android.vending.licensing.Policy;
import com.google.android.vending.licensing.StrictPolicy;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.provider.Settings.Secure;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
public class Sms_busyActivity extends Activity {
IntentFilter intentFilter;
ToggleButton endis;
EditText message;
Button smessage;
SharedPreferences getPrefs;
SharedPreferences.Editor editor;
private LicenseCheckerCallback mLicenseCheckerCallback;
private LicenseChecker mChecker;
private Handler mHandler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String android_id = Secure.getString(getBaseContext()
.getContentResolver(), Secure.ANDROID_ID);
// if (!android_id.contentEquals("56d330123953677d")) {
// Log.e("SMS Busy App", "Device ID not allowed. Exiting.");
// finish();
// } else {
// Log.d("SMS Busy App", "Device ID Allowed");
// }
// Intent i = new Intent("com.liamwli.smsbusy.PREFS");
// startActivity(i);
setContentView(R.layout.def);
doCheck();
String KEY = "abcdefgijklmnopqrstuvqxyz"; //made up to post online
endis = (ToggleButton) findViewById(R.id.enableddis);
smessage = (Button) findViewById(R.id.savemess);
message = (EditText) findViewById(R.id.message);
getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
editor = getPrefs.edit();
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
final byte[] SALT = new byte[] { -46, 65, 30, -128, -103, -57, 74, -64,
51, 88, -95, -45, 77, -117, -36, -113, -11, 32, -64, 89 };
mChecker = new LicenseChecker(this, new StrictPolicy(), KEY);
mHandler = new Handler();
// ---intent to filter for SMS messages received---
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
Boolean state = getPrefs.getBoolean("enabled", false);
// String stext = getPrefs.getString("text", "");
// message.setText(stext);
message.setText(getPrefs.getString("text", ""));
if (message.getText().toString().contentEquals("")) {
Toast.makeText(this, "Unable to get saved message. Please resave.",
Toast.LENGTH_LONG).show();
}
endis.setChecked(state);
endis.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Log.d("SMS Busy App", "onCheckedChanged Called");
// Toast.makeText(Sms_busyActivity.this, "App state changed",
// Toast.LENGTH_LONG).show();
editor = getPrefs.edit();
if (endis.isChecked()) {
editor.putBoolean("enabled", true);
} else {
editor.putBoolean("enabled", false);
}
editor.putString("text", message.getText().toString());
editor.commit();
}
});
smessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
editor = getPrefs.edit();
editor.putString("text", message.getText().toString());
editor.commit();
Log.d("smessage", "Message saved & commited");
Toast.makeText(Sms_busyActivity.this, "Message Saved",
Toast.LENGTH_SHORT).show();
}
});
}
private void doCheck() {
mChecker.checkAccess(mLicenseCheckerCallback);
}
@SuppressWarnings("deprecation")
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
Log.d("SMS Busy App", "onKeyDown Called");
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
// public void onBackPressed() {
// Log.d("SMS Busy App", "onBackPressed Called");
// finish();
// }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
Log.d("SMS Busy App", "onCreateOptionsMenu Called");
getMenuInflater().inflate(R.menu.mmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.aboutme:
Intent i = new Intent(this, AboutMe.class);
startActivity(i);
break;
}
return true;
}
private void displayResult(final String result) {
mHandler.post(new Runnable() {
public void run() {
Button mCheckLicenseButton = (Button) findViewById(R.id.button1);
mCheckLicenseButton.setText("App Licenced. Click to continue.");
mCheckLicenseButton.setEnabled(true);
setContentView(R.layout.main);
setProgressBarIndeterminateVisibility(false);
}
});
}
class MyLicenseCheckerCallback extends Activity implements
LicenseCheckerCallback {
public void allow(int reason) {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
// Should allow user access.
displayResult(getString(R.string.allow));
}
@SuppressWarnings("deprecation")
public void dontAllow(int reason) {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
AlertDialog alert = new AlertDialog.Builder(Sms_busyActivity.this)
.create();
if (reason == Policy.RETRY) {
// If the reason received from the policy is RETRY, it was
// probably
// due to a loss of connection with the service, so we should
// give the
// user a chance to retry. So show a dialog to retry.
alert.setTitle("Please retry license check");
alert.setMessage("The connection to the licensing server was lost. Please click retry below");
alert.setButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
});
alert.show();
} else {
// Otherwise, the user is not licensed to use this app.
// Your response should always inform the user that the
// application
// is not licensed, but your behavior at that point can vary.
// You might
// provide the user a limited access version of your app or you
// can
// take them to Google Play to purchase the app.f
setContentView(R.layout.def);
alert.setTitle("Not Licensed");
alert.setMessage("This app is not licensed. Please buy it off google play.");
alert.setButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
});
alert.show();
}
}
@Override
public void applicationError(int errorCode) {
// TODO Auto-generated method stub
}
}
}
編集:行番号がオフでした。新しい logcat が生成されました。もう一度見てください。