不幸的是app停止了,doinbackground()

时间:2015-11-20 07:05:57

标签: android parse-platform android-listview

我正在测试一个从解析中将数据检索到listview的应用程序,但是当我运行该应用程序时,它会崩溃,说“不幸的是app已停止”。不知道为什么? 这是我的代码

main activity.java

public class MainActivity extends Activity 
{
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
FinalAdapter adapter;
private List<CodeList> codelist = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.beg_layout);

    new RemoteDataTask().execute();
}

private class RemoteDataTask extends AsyncTask<Void, Void, Void>
{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(MainActivity.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Testing");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading. Please wait this may take few moments ....");
        mProgressDialog.setIndeterminate(true);
        // Show progressdialog
        mProgressDialog.show();
    }


    @Override
    protected Void doInBackground(Void... params)
    {
        codelist = new ArrayList<CodeList>();
        try{
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                "BeginnerActivity");
            query.orderByAscending("_created_at");
            ob = query.find();
            for(ParseObject begactivity : ob){

                ParseFile listimg = (ParseFile) begactivity.get("alphabetimg");
                ParseFile levelimg = (ParseFile) begactivity.get("levelimg");
                ParseFile apiimg = (ParseFile) begactivity.get("apiimg");
                ParseFile descimgone = (ParseFile) begactivity.get("descimgone");
                ParseFile descimgtwo = (ParseFile) begactivity.get("descimgtwo");


                CodeList map = new CodeList();
                map.setListHeading((String) begactivity.get("listheading"));
                map.setSingleItemHeading((String) begactivity.get("heading"));
                map.setDescription((String) begactivity.get("subheading"));
                map.setApiText((String) begactivity.get("apitext"));
                map.setFinalCodeText((String) begactivity.get("codetext"));

                map.setAlphabetimg(listimg.getUrl());
                map.setLevelImg(levelimg.getUrl());
                map.setApiImg(apiimg.getUrl());
                map.setPreviewImgOne(descimgone.getUrl());
                map.setPreviewImgTwo(descimgtwo.getUrl());
                codelist.add(map);
            }
        }catch(ParseException e){
            Log.e("Error", e.getMessage());
            e.printStackTrace();

        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.beg_layoutListView);
        // Pass the results into ListViewAdapter.java
        adapter = new FinalAdapter(MainActivity.this,
                                   codelist);
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }



}

}

codelist.java

public class CodeList
{
private String alphabetimg;
private String listHeading;
private String levelImg;
private String singleItemHeading;
private String description;
private String apiImg;
private String apiText;

private String previewImgOne;
private String previewImgTwo;
private String finalCodeText;



public void setAlphabetimg(String alphabetimg)
{
    this.alphabetimg = alphabetimg;
}

public String getAlphabetimg()
{
    return alphabetimg;
}

public void setListHeading(String listHeading)
{
    this.listHeading = listHeading;
}

public String getListHeading()
{
    return listHeading;
}

public void setLevelImg(String levelImg)
{
    this.levelImg = levelImg;
}

public String getLevelImg()
{
    return levelImg;
}

public void setSingleItemHeading(String singleItemHeading)
{
    this.singleItemHeading = singleItemHeading;
}

public String getSingleItemHeading()
{
    return singleItemHeading;
}

public void setDescription(String description)
{
    this.description = description;
}

public String getDescription()
{
    return description;
}

public void setApiImg(String apiImg)
{
    this.apiImg = apiImg;
}

public String getApiImg()
{
    return apiImg;
}

public void setApiText(String apiText)
{
    this.apiText = apiText;
}

public String getApiText()
{
    return apiText;
}


public void setPreviewImgOne(String previewImgOne)
{
    this.previewImgOne = previewImgOne;
}

public String getPreviewImgOne()
{
    return previewImgOne;
}

public void setPreviewImgTwo(String previewImgTwo)
{
    this.previewImgTwo = previewImgTwo;
}

public String getPreviewImgTwo()
{
    return previewImgTwo;
}

public void setFinalCodeText(String finalCodeText)
{
    this.finalCodeText = finalCodeText;
}

public String getFinalCodeText()
{
    return finalCodeText;
}

}

解析application.java

public class ParseApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();

    // Add your initialization code here
    Parse.initialize(this, "4rOrqK23m0nVq187Of3rhDe0TK1R7FObdBWhfX10", "afKA4B9ohvk3M9pZNv9qK2ypbrdOpXi0FPnrXOu6");

    ParseUser.enableAutomaticUser();
    ParseACL defaultACL = new ParseACL();

    // If you would like all objects to be private by default, remove this line.
    defaultACL.setPublicReadAccess(true);

    ParseACL.setDefaultACL(defaultACL, true);
}

 }

final adapter.java

public class FinalAdapter extends BaseAdapter
{
Context context;
    LayoutInflater inflater;
    ImageLoader imageLoader;
private List<CodeList> codeList = null;
private ArrayList<CodeList> arraylist;

public FinalAdapter(Context context,
                    List<CodeList> codeList) {
    this.context = context;
    this.codeList = codeList;
    inflater = LayoutInflater.from(context);
    this.arraylist = new ArrayList<CodeList>();
    this.arraylist.addAll(codeList);
    imageLoader = new ImageLoader(context);

}

public class ViewHolder{
    TextView listHeading;
    ImageView alphabetList;
}


@Override
public int getCount()
{
    return codeList.size();
}

@Override
public Object getItem(int position)
{
    return codeList.get(position);
}

@Override
public long getItemId(int position)
{
    return position;
}

@Override
public View getView(final int position, View view, ViewGroup parent)
{
    final ViewHolder holder;
    if(view == null){
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.beg_list_item,null);
        holder.listHeading = (TextView) view.findViewById(R.id.beg_list_itemTextView);
        holder.alphabetList = (ImageView) view.findViewById(R.id.beg_list_itemImageView);
        view.setTag(holder);

    }else{
        holder = (ViewHolder) view.getTag();
    }
    holder.listHeading.setText(codeList.get(position).getListHeading());
    imageLoader.DisplayImage(codeList.get(position).getAlphabetimg(),
                             holder.alphabetList);



    view.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0){
                Intent intent = new Intent(context, SingleItemView.class);

                intent.putExtra("listheading",
                                (codeList.get(position).getListHeading()));
                intent.putExtra("alphabetimg",
                                (codeList.get(position).getAlphabetimg()));
                // Start SingleItemView Class
                context.startActivity(intent);

            }
        });                 

    return view;
}

 }

singleitemview.java

public class SingleItemView extends Activity.  
{
    String heading;
    String subHeading;
    String levelImg;
String apiImg;
    String apiText;
    String descImgOne;
    String descImgTwo;
String codeText;
ImageLoader imgloader = new ImageLoader(this);

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.singleitemview);

    Intent i = getIntent();

    heading= i.getStringExtra("heading");
    subHeading = i.getStringExtra("subheading");
    levelImg = i.getStringExtra("levelimg");
    apiImg = i.getStringExtra("apiimg");
    apiText = i.getStringExtra("apitext");
    descImgOne = i.getStringExtra("descimgone");
    descImgTwo = i.getStringExtra("descimgtwo");
    codeText = i.getStringExtra("codetext");


    TextView headingtxt = (TextView)findViewById(R.id.singleitemheading);
    TextView subheadingtxt = (TextView)findViewById(R.id.singleitemsubheading);
    TextView apitxt = (TextView)findViewById(R.id.singleitemviewapitext);
    TextView codetxt = (TextView) findViewById(R.id.singleitemviewcodetext);

    ImageView level =(ImageView) findViewById(R.id.levelimg);
    ImageView api =(ImageView) findViewById(R.id.singleitemviewapiimg);
    ImageView descone =(ImageView) findViewById(R.id.descriptionimgone);
    ImageView desctwo =(ImageView) findViewById(R.id.descriptionimgtwo);

    headingtxt.setText(heading);
    subheadingtxt.setText(subHeading);
    apitxt.setText(apiText);
    codetxt.setText(codeText);

    imgloader.DisplayImage(levelImg, level);
    imgloader.DisplayImage(apiImg, api);
    imgloader.DisplayImage(descImgOne, descone);
    imgloader.DisplayImage(descImgTwo, desctwo);

}
 }

android manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:name="ParseApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SingleItemView">
    </activity>
</application>

</manifest>

logcat的

  

11-20 12:04:38.716 14897 14937 E AndroidRuntime致命异常:AsyncTask#1     11-20 12:04:38.716 14897 14937 E AndroidRuntime进程:com.mycompany.myapp,PID:14897   11-20 12:04:38.716 14897 14937 E AndroidRuntime java.lang.RuntimeException:执行doInBackground()时发生错误   11-20 12:04:38.716 14897 14937 E AndroidRuntime at android.os.AsyncTask $ 3.done(AsyncTask.java:304)   11-20 12:04:38.716 14897 14937 E AndroidRuntime at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)   11-20 12:04:38.716 14897 14937 E AndroidRuntime at java.util.concurrent.FutureTask.setException(FutureTask.java:222)   11-20 12:04:38.716 14897 14937 E AndroidRuntime at java.util.concurrent.FutureTask.run(FutureTask.java:242)   11-20 12:04:38.716 14897 14937 E AndroidRuntime at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)   11-20 12:04:38.716 14897 14937 E AndroidRuntime at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)   11-20 12:04:38.716 14897 14937 E AndroidRuntime at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)   11-20 12:04:38.716 14897 14937 E AndroidRuntime at java.lang.Thread.run(Thread.java:818)   11-20 12:04:38.716 14897 14937 E AndroidRuntime引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String com.parse.ParseFile.getUrl()'   11-20 12:04:38.716 14897 14937 E AndroidRuntime at com.mycompany.myapp.MainActivity $ RemoteDataTask.doInBackground(MainActivity.java:73)   11-20 12:04:38.716 14897 14937 E AndroidRuntime at com.mycompany.myapp.MainActivity $ RemoteDataTask.doInBackground(MainActivity.java)   11-20 12:04:38.716 14897 14937 E AndroidRuntime at android.os.AsyncTask $ 2.call(AsyncTask.java:292)   11-20 12:04:38.716 14897 14937 E AndroidRuntime at java.util.concurrent.FutureTask.run(FutureTask.java:237)   11-20 12:04:38.716 14897 14937 E AndroidRuntime ...还有4个   11-20 12:04:51.596 15592 15592 D AndroidRuntime&gt;&gt;&gt;&gt;&gt;&gt; START com.android.internal.os.RuntimeInit uid 0&lt;&lt;&lt;&lt;&lt;&lt;&lt;   11-20 12:04:51.612 15592 15592 D AndroidRuntime CheckJNI关闭   11-20 12:04:51.958 15592 15592 D AndroidRuntime呼叫主要入口com.android.commands.am.Am   11-20 12:04:51.976 15592 15592 D AndroidRuntime关闭虚拟机

1 个答案:

答案 0 :(得分:2)

你应该听你的调试器:D

这是你的错误。

 ...AndroidRuntime Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.parse.ParseFile.getUrl()' on a null object reference

所以其中一条线:

        map.setAlphabetimg(listimg.getUrl());
        map.setLevelImg(levelimg.getUrl());
        map.setApiImg(apiimg.getUrl());
        map.setPreviewImgOne(descimgone.getUrl());
        map.setPreviewImgTwo(descimgtwo.getUrl());

发送错误,因为调用getUrl()的对象为空。使用调试器检查哪一个或尝试/捕获NullPointerException