Reputation:
[Edited] This is the mainScreen activity xml as requested by some of you
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:design="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F6F6F6">
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:fontFamily="@font/montserrat"
android:text="Pocket Budget"
android:textColor="#77dd77"
android:textSize="26dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView9"
android:layout_width="138dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="@font/montserrat"
android:gravity="center"
android:text="January"
android:textColor="#443c3c"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/textView8"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView8"
app:layout_constraintTop_toTopOf="@+id/textView8" />
<View
android:id="@+id/view5"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginTop="7dp"
android:background="#FFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView8" />
<View
android:id="@+id/view2"
android:layout_width="116dp"
android:layout_height="50dp"
android:layout_marginBottom="3dp"
android:layout_marginEnd="3dp"
android:layout_marginTop="3dp"
android:background="#77dd77"
app:layout_constraintBottom_toBottomOf="@+id/view5"
app:layout_constraintEnd_toEndOf="@+id/view5"
app:layout_constraintTop_toTopOf="@+id/view5" />
<View
android:id="@+id/view3"
android:layout_width="116dp"
android:layout_height="50dp"
android:layout_marginBottom="3dp"
android:layout_marginStart="3dp"
android:layout_marginTop="3dp"
android:background="#779ecb"
app:layout_constraintBottom_toBottomOf="@+id/view5"
app:layout_constraintStart_toStartOf="@+id/view5"
app:layout_constraintTop_toTopOf="@+id/view5" />
<View
android:id="@+id/view4"
android:layout_width="116dp"
android:layout_height="50dp"
android:layout_marginBottom="3dp"
android:layout_marginEnd="3dp"
android:layout_marginStart="3dp"
android:layout_marginTop="3dp"
android:background="#ff6961"
app:layout_constraintBottom_toBottomOf="@+id/view5"
app:layout_constraintEnd_toStartOf="@+id/view2"
app:layout_constraintStart_toEndOf="@+id/view3"
app:layout_constraintTop_toTopOf="@+id/view5" />
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:fontFamily="@font/montserrat"
android:text="Balance"
android:textColor="#FFF"
android:textSize="9.5dp"
app:layout_constraintBaseline_toBaselineOf="@+id/textView2"
app:layout_constraintStart_toStartOf="@+id/view2" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:fontFamily="@font/montserrat"
android:text="Expense"
android:textColor="#FFF"
android:textSize="9.5dp"
design:layout_constraintBaseline_toBaselineOf="@+id/textView"
design:layout_constraintStart_toStartOf="@+id/view4" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:fontFamily="@font/montserrat"
android:text="Income"
android:textColor="#FFF"
android:textSize="9.5dp"
design:layout_constraintStart_toStartOf="@+id/view3"
design:layout_constraintTop_toTopOf="@+id/view3" />
<TextView
android:id="@+id/textView4"
android:layout_width="116dp"
android:layout_height="25dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:autoSizeTextType="uniform"
android:fontFamily="@font/montserrat"
android:gravity="center"
android:text="₱ 0"
android:textColor="#FFF"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/textView6"
app:layout_constraintEnd_toEndOf="@+id/view3"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/view3"
app:layout_constraintTop_toTopOf="@+id/textView6"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/textView6"
android:layout_width="116dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:autoSizeTextType="uniform"
android:fontFamily="@font/montserrat"
android:gravity="center"
android:text="₱ 0"
android:textColor="#FFF"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/view4"
app:layout_constraintEnd_toEndOf="@+id/view4"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/view4"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView11"
android:layout_width="116dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="8dp"
android:autoSizeTextType="uniform"
android:fontFamily="@font/montserrat"
android:gravity="center"
android:text="₱ 0"
android:textColor="#FFF"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/textView6"
app:layout_constraintEnd_toEndOf="@+id/view2"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/view2"
app:layout_constraintTop_toTopOf="@+id/textView6" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="5dp"
android:fontFamily="@font/montserrat"
android:text="Transactions"
android:textColor="#808080"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view5" />
<ListView
android:id="@+id/transacList"
android:layout_width="344dp"
android:layout_height="438dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:background="#FFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView5"
app:layout_constraintVertical_bias="0.0"></ListView>
</android.support.constraint.ConstraintLayout>
This is the code for my custom ListView I don't know what's wrong because I'm just a beginner in android development
package com.example.admin.test2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Timer().schedule(new TimerTask(){
@Override
public void run(){
startActivity(new Intent(MainActivity.this, ScreenOne.class));
}
}, 2500);
mListView = (ListView) findViewById(R.id.transacList);
CustomAdaptor cAdaptor = new CustomAdaptor();
mListView.setAdapter(cAdaptor);
}
class CustomAdaptor extends BaseAdapter{
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.customlist, null);
ImageView mImageView = (ImageView) view.findViewById(R.id.imageView2);
TextView mTextView = (TextView) view.findViewById(R.id.textView2);
TextView mTextView2 = (TextView) view.findViewById(R.id.textView3);
mImageView.setImageResource(R.drawable.car);
mTextView.setText("Transportation");
mTextView2.setText("₱ 100");
return view;
}
}
}
And this is the code for my custom listview layout that will be implemented in my mainScreen for viewing the image icon with the label and amount
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView2"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="19dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.015"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textView3"
android:layout_width="120dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:autoSizeTextType="uniform"
android:text="Label"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="@+id/imageView2"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="@+id/imageView2"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:layout_width="147dp"
android:layout_height="21dp"
android:layout_marginEnd="18dp"
android:layout_marginStart="8dp"
android:autoSizeTextType="uniform"
android:gravity="right"
android:text="Amount"
app:layout_constraintBottom_toBottomOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="@+id/textView3"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
This is the error that I encountered.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.test2, PID: 9518
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.test2/com.example.admin.test2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.admin.test2.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:7174)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
someone please help solving this problem the application when run is force closing and I don't know what is wrong.
Upvotes: 0
Views: 201
Reputation: 5628
ListView
is null
, so you can't set it's adapter.
Hence you get this error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
findViewById(R.id.transacList)
returns null beacuse the ID is wrong or the casting is wrong or your custom listview is messed up. Can't judge that from the code you provided.
Upvotes: 0
Reputation: 423
You didn't initialize the list view, add this line before setting the adapter.
mListView = findViewById(R.id.*your list view id*);
Upvotes: 0
Reputation: 3167
You don't have any listviews on your layout, hence you get this exception. Try adding something like this:
<ListView
android:id="@+id/transacList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="19dp"
android:layout_marginTop="8dp" />
Margins and paddings should be designed for your usage of the listview.
Upvotes: 0
Reputation: 121
It looks like your ListView "mListView" is not initialized yet, make sure your you are properly initializing it, will be helpful if you share your activity_main.xml
code.
Upvotes: 0