有人可以告诉我在我的小部件中做错了什么

时间:2015-10-15 13:35:50

标签: java android rss

我正在开发一个RSS feed文件.rss阅读器小部件,用于从news24中检索供稿。当我运行应用程序时,它说有一个我不理解的nullpointer异常。

这是我的小部件提供程序类

  RemoteViews views;
Intent mIn;
AppWidgetManager appWidgetManagerGlobal;
static ArrayList<String> mStorytitles = new ArrayList<String>();
static ArrayList<String> mLinks = new ArrayList<String>();
private static ArrayList<NewsItem> mNews = new ArrayList<NewsItem>();
private static final String READ_MORE = "ReadMoreTag";
static Random random = new Random();
private ProcessInBackground pr = new ProcessInBackground();

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    final int count = appWidgetIds.length;


    for (int i = 0; i < count; i++) {
        int widgetId = appWidgetIds[i];
        pr.execute();
        //new ProcessInBackground().execute();
        views = new RemoteViews(context.getPackageName(),
                R.layout.activity_main);
        views = new RemoteViews(context.getPackageName(), R.id.tvHeading);
        views = new RemoteViews(context.getPackageName(), R.id.tvTitle);
        int randomValue = random.nextInt(mNews.size());
        mIn = new Intent(Intent.ACTION_VIEW);
        mIn.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mIn.setData(Uri.parse(mNews.get(randomValue).getLink()));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, mIn, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.btnreadmore, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, views);
    }
}

private class ProcessInBackground extends AsyncTask<String,Void, Void> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        views.setTextViewText(R.id.tvHeading, "Getting stories");
        views.setTextViewText(R.id.tvTitle, "Please wait..");
        appWidgetManagerGlobal.updateAppWidget(mIn.getIntExtra("id", 0), views);
    }

    @Override
    protected Void doInBackground(String...params) {
        mNews = new ArrayList<NewsItem>();
        try{
            URL url = new URL(params[0]);
            URLConnection conn = url.openConnection();

            HttpURLConnection httpConn = (HttpURLConnection)conn;
            int response = httpConn.getResponseCode();

            if (response == HttpURLConnection.HTTP_OK){
                InputStream inStream = httpConn.getInputStream();

                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(inStream);
                Element element = doc.getDocumentElement();

                NodeList nList = element.getElementsByTagName("item");
                if (nList != null && nList.getLength() > 0){
                    for(int i = 0; i < nList.getLength(); i++){
                        Node item = nList.item(i);
                        String title = item.getChildNodes().item(1).getTextContent();
                        String link = item.getChildNodes().item(5).getTextContent();
                        mNews.add(new NewsItem(title,link));
                    }
                }
            }
        }
        catch(MalformedURLException e){
            Log.d("MainActivity", e.getMessage());
        }
        catch(IOException e){
            Log.d("MainActivity", e.getMessage());
        }
        catch(ParserConfigurationException e){
            Log.d("MainActivity", e.getMessage());
        }
        catch(SAXException e){
            Log.d("MainActivity", e.getMessage());
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {


        int ranVal = random.nextInt(mNews.size());
        views.setTextViewText(R.id.tvHeading, "Top Story");
        views.setTextViewText(R.id.tvTitle, mNews.get(ranVal).getTitle());
        appWidgetManagerGlobal.updateAppWidget(mIn.getIntExtra("id", 0), views);
    }

}

我在mainfest文件中添加了正确的信息,并在元数据中引用了我的priver信息

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.labl2.news24" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_action_name"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <receiver
        android:name=".MainActivity"
        android:icon="@mipmap/ic_action_name"
        android:label="News24" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/new_app_widget_info" />
    </receiver>
</application>

我的应用程序抛出的错误是这个

1 个答案:

答案 0 :(得分:0)

您过早地初始化RemoteViews views。在for循环中,首先执行任务,然后初始化视图对象。在preExecute()中,您可以引用null对象。

因此,首先给视图充气,然后执行任务。