android MainActivity中的空指针异常

时间:2014-12-16 03:43:43

标签: java android nullpointerexception

此代码中某处有NullPointerException,我很难找到它。我尝试使用碎片布局创建天气应用程序,并使用ListView对象创建列表。

LogCat输出

Process: com.androidattack.www.sunshine, PID: 2286
java.lang.NullPointerException
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
        at android.widget.AbsListView.obtainView(AbsListView.java:2263)
        at android.widget.ListView.makeAndAddView(ListView.java:1790)
        at android.widget.ListView.fillDown(ListView.java:691)
        at android.widget.ListView.fillFromTop(ListView.java:752)
        at android.widget.ListView.layoutChildren(ListView.java:1630)
        at android.widget.AbsListView.onLayout(AbsListView.java:2091)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:544)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

这是我的MainActivity类的代码:

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 {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            String[] tempWeatherlist = {"Today - Sunny - 88/63", "Tomorrow - Rainy - 72/56", "Wednesday - Cloudy - 73/62",
                    "Thursday - Sunny - 86/67", "Friday - Rainy - 71/54", "Saturday - Cloudy - 69/56", "Sunday - Sunny - 78/62"};

            ArrayList<String> tempWeather = new ArrayList<>(Arrays.asList(tempWeatherlist));
            /*
            tempWeatherlist.add(0, "Today - Sunny - 88/63");
            tempWeatherlist.add(1, "Tomorrow - Rainy - 72/56");
            tempWeatherlist.add(2, "Wednesday - Cloudy - 73/62");
            tempWeatherlist.add(3, "Thursday - Sunny - 86/67");
            tempWeatherlist.add(4, "Friday - Rainy - 71/54");
            tempWeatherlist.add(5, "Saturday - Cloudy - 69/56");
            tempWeatherlist.add(6, "Sunday - Sunny - 78/62"); */

            ArrayAdapter<String> myAdapter = new ArrayAdapter<>(getActivity(), R.layout.list_item_forecast, R.id.listView_forecast, tempWeather);

            ListView myListView = (ListView) rootView.findViewById(R.id.listView_forecast);
            myListView.setAdapter(myAdapter);


            return rootView;
        }
    }


}

2 个答案:

答案 0 :(得分:1)

作为一种猜测,从ArrayAdapter的第392行开始:

list_item_forecast是否有文本字段?

编辑: 关。您正在使用的构造函数的第三个参数应该是TextView(请参阅documentation

答案 1 :(得分:0)

没关系我发现了问题,因为事实证明我在初始化ArrayAdapter时使用了错误的xml文件,这搞乱了整个程序。