Ich erhalte eine NullPointerException, wenn ich versuche, eine clickListener
zu ImageView zu setzen.
java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.access$800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
activity_list_funny.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="@+id/id1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="4dip"
android:textStyle="bold"
android:textIsSelectable="true"
android:textSize="15sp"
android:textColor="@android:color/holo_purple"/>
<TextView
android:id="@+id/name1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="1dip"
android:textStyle="bold"
android:textIsSelectable="true"
android:textSize="20sp"
/>
<ImageView
android:id="@+id/imgbtn_favorite1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="end"
android:layout_alignBottom="@id/name1"
android:src="@drawable/ic_favorite_border_black_24dp"
android:contentDescription="@string/add_favr" />
</LinearLayout>
Und hier, als ich Initiieren Image durch ein Objekt in meinem MainActivity machen und es zu einem OnClickListener Bereitstellung Und wenn ich laufe It Kraft schließt und zeigt null Objektreferenzen
MainActivity.java
ArrayList<HashMap<String, String>> quotes;
ArrayAdapter adapter;
TextView t1,t2;
ListView list;
ImageView i;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_funny_quotes);
cd = new ConnectionDetector(getApplicationContext());
isInternetPresent = cd.isConnectingToInternet();
c = (CoordinatorLayout) findViewById(R.id.coordinatorLayout1);
**i=(ImageView)findViewById(R.id.imgbtn_favorite1);**
adapter = new ArrayAdapter<>(this, R.layout.activity_list_funny);
i.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
Teile hinzufügen (wo man mit imgbtn_favorite1 Image beschäftigen) – W0rmH0le
Bitte fügen der ** relevante ** Java-Code. Und hör auf, das zu schreiben, bitte. – Vucko
@Vucko Aktualisiert. –