2

我正在尝试获取Youtube要显示在我的Activity. 我MediaActivity.java用两个方法创建了主要活动,onCreate它们getResult是从类onPostExecute中的方法调用的AsyncTask。这是MediaActivity代码:

    public class MediaActivity extends Activity {
    YoutubeConnect youtubeRequest = new YoutubeConnect(); 
    ViewGroup media;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        youtubeRequest.execute();
        setContentView(R.layout.media);
        media = (ViewGroup) this.findViewById(R.layout.media);

    }

    public void getResult(String result) throws JSONException {


        JSONObject jsonObj = new JSONObject(result);

        JSONObject jsonObj2 = jsonObj.getJSONObject("feed");
        JSONArray the_json_array = jsonObj2.getJSONArray("entry");

        int size = the_json_array.length();

        for (int i = 0; i < size; i++) {

            //get all infos
               [...]
            LinearLayout main = new LinearLayout(this);
            main.setOrientation(0);
            main.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

            //thumb
            WebView thumb = new WebView(this);
            thumb.loadUrl(video_thumb);
            main.addView(thumb);

            LinearLayout right = new LinearLayout(this);
            right.setOrientation(1);
            right.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

            //title
            TextView title = new TextView(this);
            title.setText(video_title);
            right.addView(title);

            //description
            TextView description = new TextView(this);
            description.setText(video_description.substring(100)+"...");
            right.addView(description);

            //views
            TextView views = new TextView(this);
            views.setText("Views: "+video_views);
            right.addView(views);

            main.addView(right);                

            this.media.addView(main);
        }


     }      

}

这是我的自定义AsyncTask类代码:

 public class YoutubeConnect extends AsyncTask<String, String, String> {

    public YoutubeConnect() {
    }

    @Override
    protected String doInBackground(String... params) {
         try {
             String APIurl = "https://gdata.youtube.com/feeds/api/videos?author=author&alt=json";
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet request = new HttpGet(APIurl);
                HttpResponse response = httpClient.execute(request);
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                StringBuilder builder = new StringBuilder();
                for (String line = null; (line = reader.readLine()) != null;) {
                    builder.append(line).append("\n");
                }
                return builder.toString();

            } catch (Exception e) {
                return null;
            }

    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        MediaActivity mainthread = new MediaActivity();
        try {
            mainthread.getResult(result);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }




 }

这是日志:

 05-17 11:01:26.383: E/AndroidRuntime(2687): FATAL EXCEPTION: main
05-17 11:01:26.383: E/AndroidRuntime(2687): java.lang.NullPointerException
05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.view.View.<init>(View.java:2696)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.view.ViewGroup.<init>(ViewGroup.java:374)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.widget.LinearLayout.<init>(LinearLayout.java:166)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at wachipi.palio.MediaActivity.getResult(MediaActivity.java:47)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at wachipi.palio.YoutubeConnect.onPostExecute(YoutubeConnect.java:41)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at wachipi.palio.YoutubeConnect.onPostExecute(YoutubeConnect.java:1)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.os.AsyncTask.finish(AsyncTask.java:602)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.os.Looper.loop(Looper.java:137)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at java.lang.reflect.Method.invoke(Method.java:511)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-17 11:01:26.383: E/AndroidRuntime(2687):     at dalvik.system.NativeStart.main(Native Method)

在各种尝试之前,setContenViewViewGroup线在getResult方法内部,但它给了我其他错误。我也尝试使用调试器,但它给了我错误"source not found",我认为我的安装中缺少一些东西。我真的不明白为什么它会抛出这个错误并且找不到任何关于它的东西。

4

1 回答 1

7

该异常可能来自您实例化一个新MediaActivity的而不是使用Activity您启动线程的实例:

MediaActivity mainthread = new MediaActivity();//don't do this

MediaActivity而是在任务中传递您的引用YoutubeConnect并使用该引用调用getResult

private MediaActivity activityRef;

public YoutubeConnect(MediaActivity activityRef) {
     this.activityRef = activityRef;
}

并在onPosteExecute方法中:

@Override
protected void onPostExecute(String result) {
    try {
        activityRef.getResult(result); // you should have some ckecks to see if the activity is still available.
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

然后做一个新YoutubeConnect任务:

    YoutubeConnect youtubeRequest;
    ViewGroup media;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.media);
        youtubeRequest = new YoutubeConnect(this); 
        youtubeRequest.execute();
        media = (ViewGroup) this.findViewById(R.layout.media);
    }

另外,有了这个:

media = (ViewGroup) this.findViewById(R.layout.media);

您正在View通过布局文件名而不是id. 它应该是这样的:

media = (ViewGroup) this.findViewById(R.id.view_id_from_the_layout);
于 2012-05-17T09:44:19.463 回答