我想做什么
我得到一个ListView,其中带有一个OnItemClick,在那里我得到了存储的URL,使用这个URL我将一个Intent设置为另一个Activity(WebView)以打开该网页。
问题
当我尝试获得Intent时,我总是得到一个Force-Close-Error,但我不明白为什么。如果发现什么地方不对,那就太好了:(
您在此处找到的两个 Activity 的代码:
代码
test2.java->在这里我创建意图
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->在这里我收到意图
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
最佳答案
首先,请在变量名开头写一个小写字母,在类名前写一个大字母。每个名字都有 Camel 套;
因此,对于您的问题,我认为这两个答案应该可以解决。只是为了将它放在一个地方,您的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个意图。您只需创建一个,并在构造函数中提供上下文和类,然后将数据放在相同的意图中即可。 ShowVideo Activity 对您的第二个Intent不了解。您可以使用“VideoLink”键保存其他内容,并希望通过“ShowVideo”键获取其他内容。您必须使用相同的密钥,否则将无法获得任何 yield 。该错误是因为您的Extras为null,这可能是因为您没有向实际意图添加任何数据。您必须对类和数据使用一种意图。
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 Activity中为您的额外名称创建一个静态常量。
因此,在ShowVideo的顶部添加
public static final String EXTRA_VIDEO_LINK = "VideoLink";
而不是放入“VideoLink”并获得额外的 yield ,您可以编写ShowVideo.EXTRA_VIDEO_LINK
这样可以防止您无意中使用了不同的密钥。
关于android - 通过接收 Intent 强制关闭错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8307467/