我的主要活动扩展了ListActivity并显示了一个List。我正在使用名为DefinitionAdapter的类中定义的自定义Listitems。 我还有一个单独的类来实现OnClickListener。
在类DefinitionAdapter中,我将OnClickListener设置为ListItems:
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
...
v.setOnClickListener(new OnItemClickListener(position) );
return v;
}
我的自定义OnClickListener应该启动另一个活动。
public class OnItemClickListener implements OnClickListener extends Activity {
private int position;
public OnItemClickListener(int p) {
position = p;
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, ShowDefinition.class);
startActivity(intent);
}
}
我不确定我是否正确行事。我将我的新活动添加到清单,以及我使用以下内容扩展了监听器的活动:
<activity android:name="ShowDefinition" android:label="@string/app_name">
</activity>
<activity android:name="OnItemClickListener" android:label="@string/app_name">
</activity>
但是,如果我单击列表中的项目,应用程序始终会以NullPointerException中断。我想我的意图错了......任何想法?
关于Nikita Beloglazov的评论:
创建意图时它会中断:
Intent intent = new Intent(this, ShowDefinition.class);
这是堆栈跟踪:
05-23 22:56:46.629: ERROR/AndroidRuntime(258): Uncaught handler: thread main exiting due to uncaught exception
05-23 22:56:46.659: ERROR/AndroidRuntime(258): java.lang.NullPointerException
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.content.ComponentName.<init>(ComponentName.java:75)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.content.Intent.<init>(Intent.java:2551)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at com.andiandy.juradefinitions.OnItemClickListener.onClick(OnItemClickListener.java:22)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.view.View.performClick(View.java:2364)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.view.View.onTouchEvent(View.java:4179)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.view.View.dispatchTouchEvent(View.java:3709)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.os.Looper.loop(Looper.java:123)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at java.lang.reflect.Method.invoke(Method.java:521)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-23 22:56:46.659: ERROR/AndroidRuntime(258): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:22)
您的OnItemClickListener不应该扩展Activity。相反,您应该安排OnItemClickListener访问ListActivity实例。任何已经属于活动的视图(如ListView)都可以通过getContext()
访问ListActivity实例。然后像这样实施onClick
:
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ShowDefinition.class);
context.startActivity(intent);
}
答案 1 :(得分:3)
有两种方法可以实现onItemClickListener。您不需要创建新类。也许你现在可以尝试简单的方法:)
让ListActivity实现AdapterView.OnItemClickListener
public class MyActivity extends ListActivity implements AdapterView.OnItemClickListener
然后在ListActivity的onCreate中设置你的onClickListener:
ListView lstvwContacts = getListView();
lstvwContacts.setOnItemClickListener(this);
然后你可以像这样对点击作出反应:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch (parent.getId()) {
case android.R.id.list: {
Log.v(TAG, "onItemClick");
Intent intent = new Intent(this, ShowDefinition.class);
startActivity(intent);
}
}
}
这应该是为点击而做的。如果您需要知道何时选择了一个项目,您可以查看AdapterView.OnItemSelectedListener
-I_Artist
答案 2 :(得分:0)
你的概念是错误的,你的OnItemClickListener不应该扩展Activity,你不应该每次都建立一个新的onClickListener。
public class MainActivity extends Activity {
OnItemClickListener = null;
MainActivity() {
//Build your OnItemClickListener with a valid context
OnItemClickListener = new OnItemClickListener(this);
}
public View getView(int position, View convertView, ViewGroup parent) {
...
//Here user the OnItemClickListener
}
}