我正在开发一个项目,我需要在单独的类中访问arrayList。我有NumberListActivity Activity,我创建一个列表,我想在SmsLock类中访问此列表,该类由BroadcastReceiver扩展。在SmsLock类中,我初始化一个ArrayAdapter并将该列表分配给ArrayAdapter。但它遇到了问题。请任何人帮助我..
列表类在这里。
NumberListActivity.java
public class NumberListActivity extends Activity {
SharedPreferences preferences1;
SharedPreferences.Editor spEditor1;
int count1 = 0;
ListView numList1;
Button btnAdd1;
public static ArrayList<String> list1 = new ArrayList<String>();
public ArrayAdapter<String> adapter1;
public static final String Place1 = "placeKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_list);
preferences1 = getSharedPreferences("Place1", Context.MODE_PRIVATE);
spEditor1 = preferences1.edit();
count1 = preferences1.getInt("count1", 0);
if (count1 > 0) {
for (int i = 0; i < count1; i++) {
list1.add(preferences1.getString("Value1[" + i + "]", ""));
}
}
final EditText edit = (EditText) findViewById(R.id.Item);
numList1 = (ListView) findViewById(R.id.Smslist);
btnAdd1 = (Button) findViewById(R.id.Add);
adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list1);
numList1.setAdapter(adapter1);
numList1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
count1 = preferences1.getInt("count1", 0);
// if (count > 0) {
for (int i = position; i < count1; i++) {
// list.add();
if (i < count1)
spEditor1.putString(
"Value1[" + i + "]",
preferences1.getString("Value1[" + (i + 1)
+ "]", ""));
spEditor1.commit();
}
// }
list1.remove(position);
count1 -= 1;
spEditor1.putInt("count1", count1);
spEditor1.commit();
// preferences.edit().remove(position);
adapter1.notifyDataSetChanged();
}
});
btnAdd1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// EditText edit = (EditText) findViewById(R.id.txtItem);
spEditor1.putString("Value1[" + count1 + "]", edit.getText()
.toString());
spEditor1.commit();
list1.add(preferences1.getString("Value1[" + count1 + "]", ""));
count1 += 1;
spEditor1.putInt("count1", count1);
spEditor1.commit();
adapter1.notifyDataSetChanged();
}
});
}
}
我想在此课程中列出上述列表 SmsLock.java
public class SmsLock extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
Context c;
String phoneNumber;
String senderNum;
EditText my;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(c,
android.R.layout.simple_list_item_1, NumberListActivity.list1);
@Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
Toast.makeText(context, adapter.getCount() + "", Toast.LENGTH_LONG)
.show();
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage
.createFromPdu((byte[]) pdusObj[i]);
phoneNumber = currentMessage.getDisplayOriginatingAddress();
senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
}
for (int i = 0; i < adapter.getCount(); i++) {
if (senderNum.contains(adapter.getItem(i))) {
abortBroadcast();
}
}
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smscalllocker"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.smscalllocker.Main"
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.example.smscalllocker.MainActivity" >
</activity>
<activity android:name="com.example.smscalllocker.NumberListActivity" >
</activity>
<receiver android:name="com.example.smscalllocker.SmsLock" >
<intent-filter android:priority="100" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
logcat的。
03-31 05:05:45.010: V/LGMDMManager(5411): gLN(NULL)
03-31 05:05:45.010: V/LGMDMManager(5411): gLN(NULL)
03-31 05:05:45.100: I/Adreno200-EGLSUB(5411): <ConfigWindowMatch:2078>: Format RGBA_8888.
03-31 05:05:45.110: D/memalloc(5411): ion: Mapped buffer base:0x5cc9a000 size:3768320 offset:0 fd:61
03-31 05:05:45.130: E/(5411): Can't open file for reading
03-31 05:05:45.130: E/(5411): Can't open file for reading
03-31 05:05:45.220: D/memalloc(5411): ion: Mapped buffer base:0x5d540000 size:3768320 offset:0 fd:65
03-31 05:05:47.913: D/memalloc(5411): ion: Mapped buffer base:0x5d9d8000 size:3768320 offset:0 fd:68
03-31 05:05:47.963: V/LGMDMManager(5411): gLN(NULL)
03-31 05:05:47.963: D/Activity(5411): Activity.onPause()
03-31 05:05:47.963: D/Activity(5411): Activity.onPause(), editTextTapSensorList size: 0
03-31 05:05:47.963: V/LGMDMManager(5411): gLN(NULL)
03-31 05:05:48.023: V/LGMDMManager(5411): gLN(NULL)
03-31 05:05:48.023: V/LGMDMManager(5411): gLN(NULL)
03-31 05:05:48.063: I/Adreno200-EGLSUB(5411): <ConfigWindowMatch:2078>: Format RGBA_8888.
03-31 05:05:48.073: D/memalloc(5411): ion: Mapped buffer base:0x5de70000 size:3768320 offset:0 fd:71
03-31 05:05:48.073: D/EditText(5411): EditText.onWindowFocusChanged(), Tap gesture sensor is disabled
03-31 05:05:48.073: D/EditText(5411): EditText.onWindowFocusChanged(), Tap gesture sensor is disabled
03-31 05:05:48.123: D/memalloc(5411): ion: Mapped buffer base:0x5e208000 size:3768320 offset:0 fd:77
03-31 05:05:48.133: D/memalloc(5411): ion: Unmapping buffer base:0x5cc9a000 size:3768320
03-31 05:05:48.133: D/memalloc(5411): ion: Unmapping buffer base:0x5d540000 size:3768320
03-31 05:05:48.133: D/memalloc(5411): ion: Unmapping buffer base:0x5d9d8000 size:3768320
03-31 05:05:48.374: D/Activity(5411): Activity.onstp[()
03-31 05:05:48.374: D/Activity(5411): Activity.onPause(), editTextTapSensorList size: 0
03-31 05:05:48.434: D/memalloc(5411): ion: Mapped buffer base:0x5cc9a000 size:3768320 offset:0 fd:54
03-31 05:06:04.790: W/dalvikvm(5411): threadid=1: thread exiting with uncaught exception (group=0x40dcb1f8)
03-31 05:06:04.800: E/AndroidRuntime(5411): FATAL EXCEPTION: main
03-31 05:06:04.800: E/AndroidRuntime(5411): java.lang.RuntimeException: Unable to instantiate receiver com.example.smscalllocker.SmsLock: java.lang.NullPointerException
03-31 05:06:04.800: E/AndroidRuntime(5411): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2104)
03-31 05:06:04.800: E/AndroidRuntime(5411): at android.app.ActivityThread.access$1500(ActivityThread.java:127)
03-31 05:06:04.800: E/AndroidRuntime(5411): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
03-31 05:06:04.800: E/AndroidRuntime(5411): at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 05:06:04.800: E/AndroidRuntime(5411): at android.os.Looper.loop(Looper.java:137)
03-31 05:06:04.800: E/AndroidRuntime(5411): at android.app.ActivityThread.main(ActivityThread.java:4482)
03-31 05:06:04.800: E/AndroidRuntime(5411): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 05:06:04.800: E/AndroidRuntime(5411): at java.lang.reflect.Method.invoke(Method.java:511)
03-31 05:06:04.800: E/AndroidRuntime(5411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
03-31 05:06:04.800: E/AndroidRuntime(5411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
03-31 05:06:04.800: E/AndroidRuntime(5411): at dalvik.system.NativeStart.main(Native Method)
03-31 05:06:04.800: E/AndroidRuntime(5411): Caused by: java.lang.NullPointerException
03-31 05:06:04.800: E/AndroidRuntime(5411): at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
03-31 05:06:04.800: E/AndroidRuntime(5411): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
03-31 05:06:04.800: E/AndroidRuntime(5411): at com.example.smscalllocker.SmsLock.<init>(SmsLock.java:22)
03-31 05:06:04.800: E/AndroidRuntime(5411): at java.lang.Class.newInstanceImpl(Native Method)
03-31 05:06:04.800: E/AndroidRuntime(5411): at java.lang.Class.newInstance(Class.java:1319)
03-31 05:06:04.800: E/AndroidRuntime(5411): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2099)
03-31 05:06:04.800: E/AndroidRuntime(5411): ... 10 more
先谢谢。
答案 0 :(得分:0)
我不知道这种方法是否可行,但是你可以尝试两件事:
1.创建一个单独的类,其中get
和set
是arraylist。该类可能如下所示:
Class MySingletonClass {
private static MySingletonClass obj = new MySingletonClass();
private static ArrayList<String> listHere;
public MySingletonClass getInstance() {
return obj;
}
// getters and setters for listHere.
}
要访问您可以写的列表:
MySingletonClass.getInstance().getListHere();
getListHere
是getter方法的名称,在设置它之前。否则你可能会得到NPE
。
接收器也可能在列表填充之前启动。所以在使用之前一定要检查null。
2.另一个选项是将列表存储在缓存对象中,如
LruCache<String, Object> obj = ... // create a new obj (again in a singleton class)
obj.put("keyhere", list_obj_here);
在使用之前,即使在这种情况下也请在广播接收器中检查null
。
希望有所帮助!