我尝试做什么
我有一个ListView,里面有一个OnItemClick,在那里我得到了我存储的URL,用这个URL我设置了一个Intent到另一个Activity(WebView)打开,网页。
问题
当我试图获得意图时,我总是得到一个强制关闭错误,但我不明白为什么。如果你发现错误会很好:(
您在此处找到的两项活动的代码:
代码
test2.java - >在这里,我创建了Intent
package de.stepforward;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import de.stepforward.web.ShowVideo;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class test2 extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String result = "";
String line = null;
final ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
//get the Data from URL
try{
URL url = new URL("http://gdata.youtube.com/feeds/mobile/users/TheStepForward/uploads?alt=json&format=1");
URLConnection conn = url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//read d response till d end
while ((line = rd.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
Log.v("log_tag", "Append String " + result);
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try{
JSONObject json = new JSONObject(result);
JSONObject feed = json.getJSONObject("feed");
JSONArray entrylist = feed.getJSONArray("entry");
for(int i=0;i<entrylist.length();i++){
//Get Title
JSONObject movie = entrylist.getJSONObject(i);
JSONObject title = movie.getJSONObject("title");
String txtTitle = title.getString("$t");
Log.d("Title", txtTitle);
//Get Description
JSONObject content = movie.getJSONObject("content");
String txtContent = content.getString("$t");
Log.d("Content", txtContent);
//Get Link
JSONArray linklist = movie.getJSONArray("link");
JSONObject link = linklist.getJSONObject(0);
String txtLink = link.getString("href");
Log.d("Link", txtLink);
//Get Thumbnail
JSONObject medialist = movie.getJSONObject("media$group");
JSONArray thumblist = medialist.getJSONArray("media$thumbnail");
JSONObject thumb = thumblist.getJSONObject(2);
String txtThumb = thumb.getString("url");
Log.d("Thumb", txtThumb.toString());
//String Array daraus machen und in Hashmap füllen
HashMap<String, String> map = new HashMap<String, String>();
map.put("Thumb", txtThumb);
map.put("Title", txtTitle);
map.put("Content", txtContent);
map.put("Link", txtLink);
mylist.add(map);
}
//ListView füllen
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.lit,
new String[] { "Thumb","Title","Content","Link"},
new int[] { R.id.img_video,R.id.txt_title,R.id.txt_subtitle});
setListAdapter(adapter);
//OnClickLister um Youtube-Video zu öffnen
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Video-Link auslesen
Map<String, String> map = mylist.get(position);
String link = map.get("Link");
Log.d("Link", link);
final Intent Showvideo = new Intent(test2.this, ShowVideo.class);
Showvideo.putExtra("VideoLink", link);
final Intent i = new Intent(test2.this, ShowVideo.class);
startActivity(i);
}
});
}catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
}
}
Showvideo.java - &gt;在这里,我收到了意图
package de.stepforward.web;
import de.stepforward.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ShowVideo extends Activity {
WebView mWebView;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showvideo);
//Video-Link abfangen
Bundle extras = getIntent().getExtras();
String VideoLink = extras.getString("ShowVideo");
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(VideoLink);
}
}
提前感谢您的帮助!
最好的问候
狩猎
注意:抱歉忘了错误日志。你在这里:
错误-登录
11-29 08:57:57.258: W/dalvikvm(10944): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
11-29 08:57:57.258: E/AndroidRuntime(10944): FATAL EXCEPTION: main
11-29 08:57:57.258: E/AndroidRuntime(10944): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.stepforward/de.stepforward.web.ShowVideo}: java.lang.NullPointerException
11-29 08:57:57.258: E/AndroidRuntime(10944): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
11-29 08:57:57.258: E/AndroidRuntime(10944): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
11-29 08:57:57.258: E/AndroidRuntime(10944): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
11-29 08:57:57.258: E/AndroidRuntime(10944): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
11-29 08:57:57.258: E/AndroidRuntime(10944): at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 08:57:57.258: E/AndroidRuntime(10944): at android.os.Looper.loop(Looper.java:143)
11-29 08:57:57.258: E/AndroidRuntime(10944): at android.app.ActivityThread.main(ActivityThread.java:4196)
11-29 08:57:57.258: E/AndroidRuntime(10944): at java.lang.reflect.Method.invokeNative(Native Method)
11-29 08:57:57.258: E/AndroidRuntime(10944): at java.lang.reflect.Method.invoke(Method.java:507)
11-29 08:57:57.258: E/AndroidRuntime(10944): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-29 08:57:57.258: E/AndroidRuntime(10944): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-29 08:57:57.258: E/AndroidRuntime(10944): at dalvik.system.NativeStart.main(Native Method)
11-29 08:57:57.258: E/AndroidRuntime(10944): Caused by: java.lang.NullPointerException
11-29 08:57:57.258: E/AndroidRuntime(10944): at de.stepforward.web.ShowVideo.onCreate(ShowVideo.java:33)
11-29 08:57:57.258: E/AndroidRuntime(10944): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
11-29 08:57:57.258: E/AndroidRuntime(10944): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
11-29 08:57:57.258: E/AndroidRuntime(10944): ... 11 more
答案 0 :(得分:1)
在你写这行的时间
Showvideo.putExtra("VideoLink", link);
在你写的时候
Bundle extras = getIntent().getExtras();
String VideoLink = extras.getString("ShowVideo");
因为您必须使用相同的键来获取和设置时间,因此您将VideoLink设置为关键并使用未设置的ShowVideo获取时间
试试这种方式
Bundle extras = getIntent().getExtras();
String VideoLink = extras.getString("VideoLink");
答案 1 :(得分:1)
final Intent Showvideo = new Intent(test2.this, ShowVideo.class);
Showvideo.putExtra("VideoLink", link);
final Intent i = new Intent(test2.this, ShowVideo.class);
startActivity(i);
在这个ShowVideo活动中,您将获得额外内容,因为关键是“ShowVideo”。但你传递的是“VideoLink”。
String VideoLink = extras.getString("ShowVideo");
这就是为什么你得到nullpointer来获取额外的第33行。
答案 2 :(得分:1)
首先,请在开头写一个带有小写字母和类名的变量名,并带一个大字母。每个名字都有Camel-case;
所以对于你的问题,我认为这两个答案应该解决它。只是将它放在一个地方,你的onItemClick方法应该是这样的。
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Video-Link auslesen
Map<String, String> map = myList.get(position);
String link = map.get("Link");
Log.d("Link", link);
Intent i = new Intent(test2.this, ShowVideo.class);
i.putExtra("VideoLink", link);
startActivity(i);
}
首先,您不必创建2个意图。您只需创建一个并在构造函数中提供上下文和类,并将数据放在相同的intent中。 ShowVideo活动对您的第二个Intent一无所知。您可以使用“VideoLink”键保存额外内容,并希望使用“ShowVideo”键获得额外的功能。你必须使用相同的密钥,否则你将无法获得任何东西。该错误是因为您的额外内容为空,这可能是因为您没有向您的真实意图添加任何数据。您必须为类和数据使用一个intent。
onCreate应如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showvideo);
//Video-Link abfangen
Bundle extras = getIntent().getExtras();
String videoLink = extras.getString("VideoLink");
....
}
其次,我会在ShowVideo活动中为你的额外名字制作一个静态常量。 所以在ShowVideo的顶部添加
public static final String EXTRA_VIDEO_LINK = "VideoLink";
而不是你的看法中的“VideoLink”而是你写的更多内容ShowVideo.EXTRA_VIDEO_LINK 这样可以防止您无意中拥有不同的密钥。
答案 3 :(得分:0)
试试这个:
final Intent Showvideo = new Intent(test2.this, ShowVideo.class);
Showvideo.putExtra("VideoLink", link);
startActivity(Showvideo);