无法启动应用程序 - java.lang.NullPointerException

时间:2014-07-11 16:17:12

标签: java android android-fragments

我在启动应用程序时出现问题(崩溃),而代码中没有错误。 这是在我将List View活动的代码转换为List Fragment ...

之后发生的

我的主要活动:

package com.example.eronetmarket;

import com.preporuceno_app.AppAdapter;

import android.R.drawable;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;


public class MainActivity extends FragmentActivity implements ActionBar.TabListener {


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
     getMenuInflater().inflate(R.menu.itemmenu, menu);
     return true;
    }


    ViewPager viewPager=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        viewPager = (ViewPager) findViewById(R.id.pager);


        final ActionBar actionBar=getActionBar();

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.RED));
        //actionBar.setDisplayShowHomeEnabled(false);

        addTabs(actionBar);
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {
                Log.d("VIVZ","onPageScrolled "+i+" "+v+" "+i2);
            }

            @Override
            public void onPageSelected(int i) {
                actionBar.setSelectedNavigationItem(i);
                Log.d("VIVZ","onPageSelected "+i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {
                if(i==ViewPager.SCROLL_STATE_IDLE)
                Log.d("VIVZ","onPageScrollStateChanged scroll state idle "+i);
                if(i==ViewPager.SCROLL_STATE_DRAGGING)
                    Log.d("VIVZ","onPageScrollStateChanged scroll state dragging "+i);
                if(i==ViewPager.SCROLL_STATE_SETTLING)
                    Log.d("VIVZ","onPageScrollStateChanged scroll state settling "+i);
            }
        });

        }

    private void addTabs(ActionBar actionBar)
    {
        ActionBar.Tab tab1=actionBar.newTab();
        tab1.setText("PREPORUČENO");
        tab1.setTabListener(this);

        ActionBar.Tab tab2=actionBar.newTab();
        tab2.setText("NAJPOPULARNIJE");
        tab2.setTabListener(this);

        ActionBar.Tab tab3=actionBar.newTab();
        tab3.setText("KATEGORIJE");
        tab3.setTabListener(this);

        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
//        Log.d("VIVZ","onTabSelected "+tab.getText());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
//        Log.d("VIVZ","onTabUnselected "+tab.getText());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
//        Log.d("VIVZ","onTabReselected "+tab.getText());
    }


}

class MyAdapter extends FragmentStatePagerAdapter
{

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment=null;
        if(i==0)
        {
            fragment=new FragmentA();
        }
        if(i==1)
        {
            fragment=new Preporuceno();
        }
        if(i==2)
        {
            fragment=new FragmentC();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

我的片段列表:

package com.example.eronetmarket;

import java.io.FileNotFoundException;

import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Preporuceno extends ListFragment  {

    private AppAdapter mAdapter;
    private ListView siteList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.i("mobAppModel", "OnCreate()");    
    View rootView = inflater.inflate(R.layout.activity_preporuceno, container, false);

    siteList = (ListView) getActivity().findViewById(R.id.listView1);   

    siteList.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int pos,long id) {
            String url = mAdapter.getItem(pos).getstoreURL();
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);

        }
    });

        if(isNetworkAvailable()){
            Log.i("mobAppModel", "starting download Task");
            AppDownloadTask download = new AppDownloadTask();
            download.execute();
        }else{
            mAdapter = new AppAdapter(getActivity().getApplicationContext(), -1, XMLsourcePullParser.getmobAppModel(getActivity()));
            siteList.setAdapter(mAdapter);
        }

        return rootView;
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager 
              = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    } 
    private class AppDownloadTask extends AsyncTask<Void, Void, Void>{
        @Override
        protected Void doInBackground(Void... arg0) {
            //Download the file
            try {
                Downloader.DownloadFromUrl("http://minores.info/joomla30/stacksites.xml", getActivity().openFileOutput("XMLsource.xml", Context.MODE_PRIVATE));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            return null;
        }
        @Override
        protected void onPostExecute(Void result){
            //setup our Adapter and set it to the ListView.
            mAdapter = new AppAdapter(getActivity().getApplicationContext(), -1, XMLsourcePullParser.getmobAppModel(getActivity()));
            siteList.setAdapter(mAdapter);
            Log.i("mobAppModel", "adapter size = "+ mAdapter.getCount());
        }

    }

 }

这是我的LogCat消息:

Logcat

在第38行,LogCat显示错误,我有这一行public void onItemClick(AdapterView<?> parent, View v, int pos,long id) {

我不明白我哪里出错了?

1 个答案:

答案 0 :(得分:2)

更改此

siteList = (ListView) getActivity().findViewById(R.id.listView1); 

siteList = (ListView) rootView.findViewById(R.id.listView1); 

视图属于膨胀的布局,您需要使用视图对象来初始化视图。

  

java.lang.RuntimeException:您的内容必须具有ListView   id属性是&#39; android.R.id.list&#39;

ListActivity或ListFragment会有listview占用整个屏幕。如果你想在布局中有其他视图,那么你的布局必须有一个id为andorid.R.id.list的listview

http://developer.android.com/reference/android/app/ListFragment.html

引用文档

  

ListFragment具有由单个列表视图组成的默认布局。   但是,如果需要,可以通过自定义片段布局   从onCreateView返回您自己的视图层次结构(LayoutInflater,   ViewGroup,Bundle)。为此,您的视图层次结构必须包含   具有id&#34; @android:id / list&#34;的ListView对象; (或列出它是否在   代码)