当我尝试使用ImageButton的setOnClickListener时,我收到了java.lang.NullPointerException,whisch是行的项目:
public class FragmentTab1 extends SherlockFragment {
ListView list;
LazyAdapter adapter;
ImageButton ib;
Fragment1 frag1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragmenttab1, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
list = (ListView) view.findViewById(android.R.id.list);
final EditText editText = (EditText)view.findViewById(R.id.editText);
Button buttFind = (Button)view.findViewById(R.id.buttFind);
buttFind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editText.getText().toString() != null){
final String url = strBldr(editText.getText().toString());
new Thread(new Runnable() {
@Override
public void run() {
Parser parser = null;
try {
parser = new Parser(doRequest(url));
} catch (Exception e) {
e.printStackTrace();
}
mSource = parser.getSource();
Message msg = mHandler.obtainMessage();
mHandler.sendMessage(msg);
}
}).start();
}
}
});
adapter = new LazyAdapter(getActivity(), mSource);
list.setAdapter(adapter); // all is OK
ib = (ImageButton)list.findViewById(android.R.id.icon);
ib.setOnClickListener(new View.OnClickListener() { //here error
@Override
public void onClick(View view) {
//
}
});
}
我还尝试了getActivity()。finViewById,view.finViewById,但结果相同。 这是xml的行:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@android:id/icon""
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="left|top"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/title1"
android:text="qweqwrqw"
android:layout_width="200dp"
android:layout_height="100dp"
android:paddingLeft="15dp"
android:textSize="15sp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imageButton1"
android:layout_toEndOf="@+id/imageButton1"
android:layout_alignBottom="@+id/imageButton1" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save to database"
android:id="@+id/checkBox1"
android:layout_below="@+id/imageButton1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
有人可以遏制我,我做错了什么?
EDIT 对不起,我在FragmentTab1中发布了错误的示例ID。我在FragmentTab1中指出:ib =(ImageButton)list.findViewById(android.R.id.icon)和xml android:id =&#34; @android:id / icon&#34;,但崩溃时出现错误java.lang.NullPointerException 举个例子,引用日志:
10-21 17:17:26.672 1730-1730/com.tim_kriuchkov.task E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.tim_kriuchkov.task.FragmentTab1.onCreateView(FragmentTab1.java:98)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
at android.view.View.measure(View.java:12723)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12723)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
at android.view.View.measure(View.java:12723)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
更改ib = (ImageButton)list.findViewById(android.R.id.icon);
到ib = (ImageButton)list.findViewById(android.R.id.imageButton1);
Radoslav找到你的错误:
以调试模式启动应用程序以查看更多详细信息。
答案 1 :(得分:0)
你显然要复制一些你不理解的代码。
我看到的其中一个错误就在
行 ib = (ImageButton)list.findViewById(android.R.id.icon);
你的ImageButton id是“imageButton1”而不是“android.R.id.icon”
答案 2 :(得分:0)
这一行错了:
list.findViewById(android.R.id.icon);
您应该使用:
list.findViewById(android.R.id.imageButton1);