我有一个自定义弹出窗口,在我的活动中有自己的布局(负责弹出窗口工作的代码在一个单独的类PopupWindowActivity中)。我想在此弹出窗口打开活动中指定一个按钮。我尝试了一些方法,但应用程序崩溃了NullPointerException。
此活动的代码如下:
public class FirstActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView popupButton = (ImageView) this.findViewById(R.id.popup_btn);
likemenuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupWindow dw = new PopupWindow(v);
dw.showLikePopDownMenu();
}
});
}
// my stuff
//popup window
private class PopupWindow extends PopupWindowActivity implements OnClickListener {
public PopupWindow(View anchor) {
super(anchor);
}
@Override
protected void onCreate() {
// inflate layout
LayoutInflater inflater =
(LayoutInflater) this.anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.popup, null);
// setup button events
ImageView aboutButton = (ImageView) findViewById(R.id.btn_about);
aboutButton.setOnClickListener(this);
this.setContentView(root);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_about: {
Intent activity = new Intent(FistActivity.this, About.class);
startActivity(activity);
break;
}
}
}}
}
另一种方式:
private static class PopupWindow extends PopupWindowActivity implements OnClickListener {
public PopupWindow(View anchor) {
super(anchor);
}
@Override
protected void onCreate() {
// inflate layout
LayoutInflater inflater =
(LayoutInflater) this.anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.popup, null);
// setup button events
ImageView searchButton = (ImageView) findViewById(R.id.btn_about);
algebraButton.setOnClickListener(this);
this.setContentView(root);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_about: {
Intent intent = new Intent();
intent.setClass(this, About.class);
startActivity(intent);
break;
}
default:
break;
}
}}
eclipse说起初我应该从static
删除private static class PopupWindow extends PopupWindowActivity implements
,然后我跟随intent.setClass(this, About.class);
:
The method setClass(Context, Class<?>) in the type Intent is not applicable for the arguments
(FirstActivity.PopupWindow, Class<About>)
请帮助。
为第一次崩溃添加了日志:
03-07 00:28:07.750: D/AndroidRuntime(12472): Shutting down VM
03-07 00:28:07.750: W/dalvikvm(12472): threadid=1: thread exiting with uncaught exception (group=0x40018578)
03-07 00:28:07.759: E/AndroidRuntime(12472): FATAL EXCEPTION: main
03-07 00:28:07.759: E/AndroidRuntime(12472): java.lang.NullPointerException
03-07 00:28:07.759: E/AndroidRuntime(12472): at com.example.FirstActivity$PopupWindow.onCreate(FirstActivity.java:110)
03-07 00:28:07.759: E/AndroidRuntime(12472): at com.quickaction.PopupWindowActivity.<init>(PopupWindowActivity.java:52)
03-07 00:28:07.759: E/AndroidRuntime(12472): at com.example.FirstActivity$PopupWindow.<init>(FirstActivity.java:97)
03-07 00:28:07.759: E/AndroidRuntime(12472): at com.example.FirstActivity$1.onClick(FirstActivity.java:30)
03-07 00:28:07.759: E/AndroidRuntime(12472): at android.view.View.performClick(View.java:2485)
03-07 00:28:07.759: E/AndroidRuntime(12472): at android.view.View$PerformClick.run(View.java:9080)
03-07 00:28:07.759: E/AndroidRuntime(12472): at android.os.Handler.handleCallback(Handler.java:587)
03-07 00:28:07.759: E/AndroidRuntime(12472): at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 00:28:07.759: E/AndroidRuntime(12472): at android.os.Looper.loop(Looper.java:130)
03-07 00:28:07.759: E/AndroidRuntime(12472): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-07 00:28:07.759: E/AndroidRuntime(12472): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 00:28:07.759: E/AndroidRuntime(12472): at java.lang.reflect.Method.invoke(Method.java:507)
03-07 00:28:07.759: E/AndroidRuntime(12472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
03-07 00:28:07.759: E/AndroidRuntime(12472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
03-07 00:28:07.759: E/AndroidRuntime(12472): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
按下按钮:
Intent intent = new Intent("<your-intent-goes-here");
<context>.startActivity(intent);
答案 1 :(得分:0)
有很多方法可以解决这个问题。一种方法是通过Dialog的构造函数传递对Context的引用。