Zizo Ahmad Zizo Ahmad - 6 months ago 41
Android Question

the app crashes and i don't know why?

Can you help me, please?

I have no errors in my app until I launch the app it and it crashes screenshot. I am using Android Studio 1, because I am in a course and they use Android Studio 1, I tried android studio 2.1 but 1 is better in that course.

package com.example.android.sunshine.app;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends ActionBarActivity {

@Override
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();
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

private ArrayAdapter<String> mForecastAdapter;

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

String[] data={
"day-1",
"day-2",
"day-3",
"day-4",
"day-5",
"day-6",
"day-7"};
List<String> weekforecast=new ArrayList<String>(Arrays.asList(data));


mForecastAdapter=new ArrayAdapter<String>(
getActivity(),
R.layout.list_item_forecast,
R.id.list_view_forecast,
weekforecast
);

View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ListView listView= (ListView) rootView.findViewById(R.id.list_view_forecast);

listView.setAdapter(mForecastAdapter);



return rootView;
}
}
}


My logcat (errors):


05-25 22:35:11.391 26002-26002/com.example.android.sunshine.app
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.sunshine.app, PID: 26002
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a
null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:399)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
at android.widget.AbsListView.obtainView(AbsListView.java:2421)
at android.widget.ListView.makeAndAddView(ListView.java:1875)
at android.widget.ListView.fillDown(ListView.java:702)
at android.widget.ListView.fillFromTop(ListView.java:763)
at android.widget.ListView.layoutChildren(ListView.java:1684)
at android.widget.AbsListView.onLayout(AbsListView.java:2223)
at android.view.View.layout(View.java:16727)
at android.view.ViewGroup.layout(ViewGroup.java:5452)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16727)
at android.view.ViewGroup.layout(ViewGroup.java:5452)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16727)
at android.view.ViewGroup.layout(ViewGroup.java:5452)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16727)
at android.view.ViewGroup.layout(ViewGroup.java:5452)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
at android.view.View.layout(View.java:16727)
at android.view.ViewGroup.layout(ViewGroup.java:5452)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16727)
at android.view.ViewGroup.layout(ViewGroup.java:5452)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16727)
at android.view.ViewGroup.layout(ViewGroup.java:5452)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2728)
at android.view.View.layout(View.java:16727)
at android.view.ViewGroup.layout(ViewGroup.java:5452)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2288)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2025)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1174)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6241)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:873)
at android.view.Choreographer.doCallbacks(Choreographer.java:676)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:859)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

Answer
<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="schemas.android.com/apk/res/android"
    android:id="@+id/list_item_forecast_textview"
    android:layout_centerVertical="true"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />

From your layout the id of text view is list_item_forecast_textview not list_view_forecast

So change from

mForecastAdapter=new ArrayAdapter<String>(
                getActivity(),
                R.layout.list_item_forecast,
                R.id.list_view_forecast,
                weekforecast
        );

to

mForecastAdapter=new ArrayAdapter<String>(
                getActivity(),
                R.layout.list_item_forecast,
                R.id.list_item_forecast_textview,
                weekforecast
        );