我发现了一个奇怪的错误,当我在4.0以上的设备上吃午餐时,我在搜索我的视频时迷恋,当我在2.3上使用它时一切都好。
这是一个Youtube搜索/播放器。
这是我的错误:
07-01 17:30:21.324: E/AndroidRuntime(23429): FATAL EXCEPTION: main
07-01 17:30:21.324: E/AndroidRuntime(23429): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.searcher1/com.example.searcher1.JsonParsingActivity}: java.lang.NullPointerException
07-01 17:30:21.324: E/AndroidRuntime(23429): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
07-01 17:30:21.324: E/AndroidRuntime(23429): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2225)
07-01 17:30:21.324: E/AndroidRuntime(23429): at android.app.ActivityThread.access$600(ActivityThread.java:151)
07-01 17:30:21.324: E/AndroidRuntime(23429): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
07-01 17:30:21.324: E/AndroidRuntime(23429): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 17:30:21.324: E/AndroidRuntime(23429): at android.os.Looper.loop(Looper.java:153)
07-01 17:30:21.324: E/AndroidRuntime(23429): at android.app.ActivityThread.main(ActivityThread.java:5070)
07-01 17:30:21.324: E/AndroidRuntime(23429): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 17:30:21.324: E/AndroidRuntime(23429): at java.lang.reflect.Method.invoke(Method.java:511)
07-01 17:30:21.324: E/AndroidRuntime(23429): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-01 17:30:21.324: E/AndroidRuntime(23429): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-01 17:30:21.324: E/AndroidRuntime(23429): at dalvik.system.NativeStart.main(Native Method)
07-01 17:30:21.324: E/AndroidRuntime(23429): Caused by: java.lang.NullPointerException
07-01 17:30:21.324: E/AndroidRuntime(23429): at com.example.searcher1.JsonParsingActivity.onCreate(JsonParsingActivity.java:78)
07-01 17:30:21.324: E/AndroidRuntime(23429): at android.app.Activity.performCreate(Activity.java:5207)
07-01 17:30:21.324: E/AndroidRuntime(23429): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
07-01 17:30:21.324: E/AndroidRuntime(23429): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
07-01 17:30:21.324: E/AndroidRuntime(23429): ... 11 more
错误在:
for (Video l : lst)
{
alrts.add(l);
}
完整代码:
public class JsonParsingActivity extends Activity {
//ListView that will hold our items references back to main.xml
ListView lstTest;
//Array Adapter that will hold our ArrayList and display the items on the ListView
AlertsAdapter arrayAdapter;
//List that will host our items and allow us to modify that array adapter
ArrayList<Video> alrts=null;
private TextView button;
private EditText result;
final Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_tab);
String username = null;
Bundle extras = getIntent().getExtras();
if(extras != null)
{
username = extras.getString("searchterm");
}
//Initialize ListView
lstTest= (ListView)findViewById(R.id.list);
button = (TextView) findViewById(R.id.frontImage);
result = (EditText) findViewById(R.id.editTextResult);
//Initialize our ArrayList
alrts = new ArrayList<Video>();
//Initialize our array adapter notice how it references the listitems.xml layout
arrayAdapter = new AlertsAdapter(JsonParsingActivity.this, R.layout.youtube_list_item,alrts);
//Set the above adapter as the adapter of choice for our list
lstTest.setAdapter(arrayAdapter);
List<Video> lst = null;
try {
lst = new YouTube().getVideos(username);
} catch (YouTubeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (Video l : lst)
{
alrts.add(l);
}
arrayAdapter.notifyDataSetChanged();
// listening to single list item on click
lstTest.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String s = Video.getId(position);
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("vnd.youtube://"
+ s ));
startActivity(intent);
}
});
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String Data = result.getText().toString();
Intent i = new Intent(context,JsonParsingActivity.class);
Bundle extras = new Bundle();
extras.putString("searchterm", Data);
i.putExtras(extras);
startActivityForResult(i, 1);
}
});
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Intent i = new Intent(context,SearchActivity.class);
this.finish();
startActivityForResult(i, 1);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
答案 0 :(得分:-1)
我发现了如何修复它:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
由于Json异常,我不得不添加它。