Android nullpointerException - listview - java

I've seen other people who have had the same issue, but they were missing setContentView().
Any ideas as to why the below isn't working? NPE occurs when I call setAdapter because listView is null.
public class DisplayMessageActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
ArrayList<String> message = getArray();
ListView listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, message);
listView.setAdapter(adapter);
};
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity" >
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
<ListView android:id="#+id/listView"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<TextView
android:id="#+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
</LinearLayout>

You are referencing R.layout.activity_display_message when you call setContentView meaning that (ListView) findViewById(R.id.listView); will return null
Instead of setContentView(R.layout.activity_display_message) call setContentView(R.layout.activity_main)

Related

Android App crash when using custom Listview and logcat doesnt show anything

I'm creating a Listview that shows the names of the days in a week. The app crashes and the logcat doesn't show anything. How to solve this issue?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv_classTimetable = (ListView) findViewById(R.id.lv_classTimetable);
ArrayList<String> lvDays = new ArrayList<>();
lvDays.add("Monday");
lvDays.add("Tuesday");
lvDays.add("Wednesday");
lvDays.add("Thursday");
lvDays.add("Friday");
lvDays.add("Saturday");
lvDays.add("Sunday");
ArrayAdapter classListAdapter = new ArrayAdapter(MainActivity.this, R.layout.class_timetable_listview_layout, lvDays);
lv_classTimetable.setAdapter(classListAdapter);
}
}
activity_main.xml
<RelativeLayout 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"
android:padding="16dp"
tools:context=".MainActivity">
<ListView
android:id="#+id/lv_classTimetable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:dividerHeight="5dp"
android:divider="#d1d1d1"></ListView>
</RelativeLayout>
class_timetable_listview_layout.xml this is the layout for the custom listview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<TextView
android:id="#+id/lv_day"
android:gravity="center"
android:textAlignment="center"
android:text="Day"
android:textStyle="bold"
android:textSize="35sp"
android:layout_width="match_parent"
android:layout_height="60dp"/>
</LinearLayout>
Credit to #Mike M. Solution:
The LinearLayout should change to TextView. class_timetable_listview_layout.xml should change to
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="76dp"
android:layout_margin="10dp"
android:gravity="center"
android:textAlignment="center"
android:text="Day"
android:textStyle="bold"
android:textSize="35sp">
</TextView>
MainActivity.java
ArrayAdapter classListAdapter = new ArrayAdapter(this, R.layout.class_timetable_listview_layout, lvDays );
lv_classTimetable.setAdapter(classListAdapter);

I cannot get OnClickListeners to work with my Android Activity containing two different Fragments

I am very new to Android Development and have been trying to get this to work for a while now. The basic structure of my Activity is two fragments between which you can switch by swiping. Both fragments have different layouts and contain different buttons and I am in the process of getting the buttons to work.
When I try to set the OnClickListener for any button in onCreate(), the App no longer starts. Here is my onCreate() Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
imgButton = (ImageButton)findViewById(R.id.imageButton);
OnClickListener btnListener = new OnClickListener() {
#Override
public void onClick(android.view.View view) {
//Do Something
}
};
imgButton.setOnClickListener(btnListener);
}
The App Crashes with a null object reference, saying that imgButton's value is null. I certainly am doing this wrong, but I wasn't able to find the correct way of setting up button listeners for two fragments. Can anybody point me in the right direction?
EDIT: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.dansoft.daily_surprise.MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
fragment_main.xml:
<RelativeLayout 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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/background"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.dansoft.daily_surprise.MainActivity$PlaceholderFragment">
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:src="#drawable/box_1"
android:background="#android:color/transparent"
/>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/img_1"
/>
</RelativeLayout>
fragment_sec.xml:
<RelativeLayout 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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/background"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.dansoft.daily_surprise.MainActivity$PlaceholderFragment">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/img_2"
/>
<Button
android:id="#+id/button"
android:layout_width="290dp"
android:layout_height="220dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:background="#android:color/transparent"
/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="151dp"
android:text="22"
android:textColor="#android:color/black"
android:textSize="200sp" />
</RelativeLayout>
Your should track button clicks on your fragments, not on your activities.
Your fragments are not laid-out after onCreate. So your findViewById returns null.
Move your listener code to your fragments.

Android : Get fragment's background color

I am trying to get the color set on android:background attribute from a fragment's XML file through an activity's onCreate method.
I used both the getSolidColor() and the getgetDrawingCacheBackgroundColor() methods but both of them return the value of the color corresponding to the Layout containing the fragment..
Here is the Activities code part :
private SeekBar seekBar;
private View fragmentOne;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fragmentOne = findViewById(R.id.fragment1);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
fragmentOne.getDrawingCacheBackgroundColor()
the fragment's xml file,
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/colorAccent"
android:padding="5dp"/>
And the activity_main xml file containing the above, and also 4 other fragments and a seekbar:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout 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"
android:layout_weight="2"
android:orientation="vertical"
android:padding="5dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.maryland.modernartui_nikos.MainActivity">
<fragment
android:id="#+id/fragment1"
android:name="com.maryland.modernartui_nikos.FragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="#layout/fragment_fragment_one" />
<fragment
android:id="#+id/fragment2"
android:name="com.maryland.modernartui_nikos.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="117dp"
tools:layout="#layout/fragment_fragment_two" />
</LinearLayout>
<LinearLayout 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"
android:layout_weight="2"
android:orientation="vertical"
android:padding="2dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:showDividers="beginning|middle|end"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.maryland.modernartui_nikos.MainActivity">
<fragment
android:id="#+id/fragment3"
android:name="com.maryland.modernartui_nikos.FragmentThree"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="#layout/fragment_fragment_three" />
<fragment
android:id="#+id/fragment4"
android:name="com.maryland.modernartui_nikos.FragmentFour"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
tools:layout="#layout/fragment_fragment_four" />
<fragment
android:id="#+id/fragment5"
android:name="com.maryland.modernartui_nikos.FragmentFive"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
tools:layout="#layout/fragment_fragment_five" />
</LinearLayout>
</LinearLayout>
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
The method returns the value from the RelativeLayout of activity_main.xml and not from fragment_one.xml .Why?
Found a solution to this problem :
ColorDrawable colorDrawableOne = (ColorDrawable) fragmentOne.getBackground();
ColorDrawable colorDrawableTwo = (ColorDrawable) fragmentTwo.getBackground();
ColorDrawable colorDrawableFour = (ColorDrawable) fragmentFour.getBackground();
ColorDrawable colorDrawableFive = (ColorDrawable) fragmentFive.getBackground();
final int colorOne = colorDrawableOne.getColor();
final int colorTwo = colorDrawableTwo.getColor();
final int colorFour = colorDrawableFour.getColor();
final int colorFive= colorDrawableFive.getColor();
The fragment's getBackground() method must be called and stored to a ColorDrawable variable with type casting.
Then you can call the getColor() methdod of this object to get the desired color.
put id on a relative_layout, use findViewById() to get relative_layout, where you can use view.getBaackground().

Animating button with object animator

I just want to animate my button.
I am getting:
W/PropertyValuesHolder: Method setAlpha() with type int not found on target class class android.support.v7.widget.AppCompatButton
Mainactivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
ObjectAnimator objectAnimator;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView mListView= (ListView) findViewById(R.id.listView);
Button button= (Button) findViewById(R.id.button);
objectAnimator=ObjectAnimator.ofInt(button,"alpha",0,1).setDuration(1000);
objectAnimator.setTarget(button);
objectAnimator.start();
adapter myAdapter = new adapter(this);
AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(myAdapter);
animationAdapter.setAbsListView(mListView);
mListView.setAdapter(animationAdapter);
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="autogenie.mp.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:id="#+id/listView"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
</RelativeLayout>
Why is it not animating?
What comes under property name parameter of method ofint() of objectanimator class?
The alpha property is of type float. You are trying to animate it with int values. The object animator tries to find a setAlpha(int) method in the View class and can't do it - hence the exception.
You either need to use ObjectAnimator.ofFloat() or the simpler button.animate().alpha() variant. I would personally recommend the second option - it's much cleaner and removed some boilerplate code.

ListView causes NullPointerException

This is my first Android app, i am trying to put a listview on and i have followed numerous tutorials but i always get the same result, a crash upon starting the app.
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:gravity="top"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.mattigins.mediacenter.MainActivity$PlaceholderFragment" >
<View
android:id="#+id/view1"
android:layout_width="550dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/View01"
android:background="#drawable/semi_trans_box" />
<View
android:id="#+id/View01"
android:layout_width="550dp"
android:layout_height="wrap_content"
android:layout_marginRight="58dp"
android:layout_toLeftOf="#+id/view1"
android:background="#drawable/semi_trans_box" />
<ListView
android:id="#+id/List1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/View01"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/View01" >
</ListView>
Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
ListView listView = (ListView) findViewById(R.id.List1);
String[] test_items = {"1","2","3","4"};
ArrayAdapter<String> test_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, test_items);
listView.setAdapter(test_adapter);
}
Looks like your ListView belongs to the Fragment layout. Looking at
In onCreateView of Fragment.
ListView listView = (ListView) rootView.findViewById(R.id.List1);
String[] test_items = {"1","2","3","4"};
ArrayAdapter<String> test_adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, test_items);
listView.setAdapter(test_adapter);

Categories

Resources