저는 별도의 클래스에서 arrayList에 액세스해야하는 프로젝트를 개발하고 있습니다. 내가 NumberListActivity 활동을 어디서 목록을 만들고, BroadcastReceiver에 의해 확장됩니다 SmsLock 클래스에서이 목록을 acess 싶습니다. SmsLock 클래스에서 ArrayAdapter를 초기화하고 해당 목록을 ArrayAdapter에 할당합니다. 그러나 문제가 발생합니다. 어느 누구도 도와주세요.
List Class is 여기입니다.
NumberListActivity.java별도의 클래스에있는 Access ArrayList는 BroadcastReceiver에 의해 확장됩니다.
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>
로그 캣에 위의 목록을 원한다.
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
감사합니다. 이 방법이 가능 여부 될 경우
선생님 내가 싱글 톤 클래스에 대해 알지 못합니다. 선생님. 일단 내가 그리스트에 액세스했는데 문제가 발생하면 내 코드에서 나를 돕습니다. –
싱글 톤 클래스는 다른 모든 클래스와 마찬가지로 그것이 정적이기 때문에 오직 하나의 인스턴스 만 사용된다는 것입니다. :) .. 수신기에서 null을 확인해보십시오. 그게 내 생각에 일을 shud, U 귀하의 코드를 변경할 필요가 없을 수도 있습니다 –