当前位置: 编程技术>移动开发
本页文章导读:
▪写Service Demo的时候遇到的一个有关问题小结一上 写Service Demo的时候遇到的一个问题小结一下
我写了一个 Service 的Demo,但是将启动Service的Intent声明为主Activity的成员变量时,
public class MyServiceActivity extends Activity {
private Button startSer1;
priva.........
▪ Activity变换为View Activity转换为View
FrameLayout container = (FrameLayout) findViewById(R.id.Container);ActivityGroup atGroup;Intent intent = new Intent(atGroup, aa.class);// Activity 转为 ViewWindow subActivity = atGroup.getLocalActivityManager().startActivit.........
▪ AutoCompleteTextView自动提示有关问题 AutoCompleteTextView自动提示问题
http://www.iteye.com/problems/69581这个问题写的demo <br><img src="/upload/attachment/532214/37ab924b-c257-367d-b7dd-efbe5a6911f9.png" alt=""> <br><img sr.........
[1]写Service Demo的时候遇到的一个有关问题小结一上
来源: 互联网 发布时间: 2014-02-18
写Service Demo的时候遇到的一个问题小结一下
我写了一个 Service 的Demo,但是将启动Service的Intent声明为主Activity的成员变量时,
public class MyServiceActivity extends Activity { private Button startSer1; private Button stopSer1; private Button startSer2; private Button stopSer2; public static TextView log; private Intent intent = new Intent(MyServiceActivity.this, MyService.class); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startSer1 = (Button) findViewById(R.id.startSer1); stopSer1 = (Button) findViewById(R.id.stopSer1); startSer2 = (Button) findViewById(R.id.startSer2); stopSer2 = (Button) findViewById(R.id.stopSer2); log = (TextView) findViewById(R.id.log); startSer1.setOnClickListener(btnListener); stopSer1.setOnClickListener(btnListener); startSer2.setOnClickListener(btnListener); stopSer2.setOnClickListener(btnListener); } private OnClickListener btnListener = new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.startSer1: updateLog("Start Service 1 pressed"); startService(intent); break; case R.id.startSer2: updateLog("Start Service 2 pressed"); startService(intent); break; case R.id.stopSer1: updateLog("Stop Service 1 pressed"); stopService(intent); break; case R.id.stopSer2: updateLog("Stop Service 2 pressed"); stopService(intent); break; default: break; } } }; public static void updateLog(String text) { CharSequence ch = log.getText(); log.setText(((ch == null || ch.length() == 0) ? text : ch.toString() + "\r\n" + text)); } }
系统总是在启动的报异常:
08-08 12:03:22.894: ERROR/AndroidRuntime(2662): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.archer.rainbow/com.archer.rainbow.MyServiceActivity}: java.lang.NullPointerException 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1570) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1664) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at android.app.ActivityThread.access$1600(ActivityThread.java:118) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:949) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at android.os.Handler.dispatchMessage(Handler.java:99) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at android.os.Looper.loop(Looper.java:130) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at android.app.ActivityThread.main(ActivityThread.java:3711) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at java.lang.reflect.Method.invokeNative(Native Method) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at java.lang.reflect.Method.invoke(Method.java:507) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at dalvik.system.NativeStart.main(Native Method) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): Caused by: java.lang.NullPointerException 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at android.content.ComponentName.<init>(ComponentName.java:75) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at android.content.Intent.<init>(Intent.java:2936) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at com.archer.rainbow.MyServiceActivity$1.<init>(MyServiceActivity.java:40) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at com.archer.rainbow.MyServiceActivity.<init>(MyServiceActivity.java:38) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at java.lang.Class.newInstanceImpl(Native Method) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at java.lang.Class.newInstance(Class.java:1409) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1562) 08-08 12:03:22.894: ERROR/AndroidRuntime(2662): ... 11 more
一开始,我还以为是我的哪个变量没有初始化呢,结果,看来看去,一共没几个变量啊,而且都应该正常赋值了,所以就很是纳闷了。但这个问题不解决不行啊,所以我就在一EOE论坛里发贴求高手解答。虽然有几个热心的网友帮忙给了些建议,但都不奏效啊。没办法,自己再想吧。后来,我换了一种写法:
private final Intent intent = new Intent(); intent.setComponent(new ComponentName(MyServiceActivity.this.getPackageName(), MyService.class.toString()));
并在第二句打了个断点进行了DEBUG,按F5进去之后发现:
public String getPackageName() { return mBase.getPackageName(); }
中的 mBase 竟然为 null ,无语了。这个方法可是ContextWrapper里的啊,Activity继承自它,怎么会没有传这个值给它呢?后来发现,Activity在OnCreate之前进行init的时候,调用了
public ContextWrapper(Context base) { mBase = base; }
此时,传进去的 base 即为 null,所以这样一来,便出现上面的问题。
为了避免这样的问题,需要在Activity在执行到onCreate()方法之后(包括在onCreate()方法),对该Intent进行赋值即可。
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startSer1 = (Button) findViewById(R.id.startSer1); stopSer1 = (Button) findViewById(R.id.stopSer1); startSer2 = (Button) findViewById(R.id.startSer2); stopSer2 = (Button) findViewById(R.id.stopSer2); log = (TextView) findViewById(R.id.log); startSer1.setOnClickListener(btnListener); stopSer1.setOnClickListener(btnListener); startSer2.setOnClickListener(btnListener); stopSer2.setOnClickListener(btnListener); intent = new Intent(MyServiceActivity.this, MyService.class); }
通过这个Demo,吸取到了一个教训:需要使用Context方面的信息,最好在onCreate()方法之后(包括之)。
1 楼
Rainbow702
2011-08-09
大家也可以参考这篇BLOG:http://hi.baidu.com/gaogaf/blog/item/7efdb01185806264ca80c4d9.html
讲得比我这个深入
讲得比我这个深入
[2] Activity变换为View
来源: 互联网 发布时间: 2014-02-18
Activity转换为View
FrameLayout container = (FrameLayout) findViewById(R.id.Container);
ActivityGroup atGroup;
Intent intent = new Intent(atGroup, aa.class);
// Activity 转为 View
Window subActivity = atGroup.getLocalActivityManager().startActivity(aa.getName(), intent);
// 容器添加View
View view = subActivity.getDecorView();
container.addView(view);
FrameLayout container = (FrameLayout) findViewById(R.id.Container);
ActivityGroup atGroup;
Intent intent = new Intent(atGroup, aa.class);
// Activity 转为 View
Window subActivity = atGroup.getLocalActivityManager().startActivity(aa.getName(), intent);
// 容器添加View
View view = subActivity.getDecorView();
container.addView(view);
[3] AutoCompleteTextView自动提示有关问题
来源: 互联网 发布时间: 2014-02-18
AutoCompleteTextView自动提示问题
http://www.iteye.com/problems/69581这个问题写的demo
<br><img src="/upload/attachment/532214/37ab924b-c257-367d-b7dd-efbe5a6911f9.png" alt="">
<br><img src="/upload/attachment/532216/da3da6de-4a52-3cce-ac86-d33869e70ca5.png" alt=""><br> <br>
http://www.iteye.com/problems/69581这个问题写的demo
<br><img src="/upload/attachment/532214/37ab924b-c257-367d-b7dd-efbe5a6911f9.png" alt="">
<br><img src="/upload/attachment/532216/da3da6de-4a52-3cce-ac86-d33869e70ca5.png" alt=""><br> <br>
最新技术文章: