很抱歉,我刚开始使用android并自学习它.. 我有一个用于循环的选项卡列表..并且所有选项卡都链接到同一个活动,但只需传递一些不同的URL仅用于数据。 我试过很多像
的方式TabSpec page = tabHost.newTabSpec(title);
Intent pageIntent = new Intent(this, mainpage.class);
pageIntent.putExtra("url", url);
page.setIndicator(title);
page.setContent(pageIntent);
并通过
获取值String text = getIntent().getStringExtra("url");
但它不能很好地运行并且logcat显示了很多错误..
这是我的标签代码..
for(int i = 0; i < category.length(); i++){
JSONObject c = category.getJSONObject(i);
String title = c.getString(TAG_TITLE);
String url = "http://www.abcde.com.my/list3.php?json&"+c.getString(TAG_URL);
TabSpec page = tabHost.newTabSpec(title);
Intent pageIntent = new Intent(this, mainpage.class);
pageIntent.putExtra("url", url);
page.setIndicator(title);
page.setContent(pageIntent);
tabHost.addTab(page);
}
这里是活动代码......
String text = getIntent().getStringExtra("url");
private String url = text;
来自logcat的错误代码
10-22 02:14:24.313: E/AndroidRuntime(1660): FATAL EXCEPTION: main
10-22 02:14:24.313: E/AndroidRuntime(1660): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.jsonparsing/com.androidhive.jsonparsing.kwongwahtab}: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.androidhive.jsonparsing/com.androidhive.jsonparsing.mainpage}: java.lang.NullPointerException
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.os.Looper.loop(Looper.java:123)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-22 02:14:24.313: E/AndroidRuntime(1660): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 02:14:24.313: E/AndroidRuntime(1660): at java.lang.reflect.Method.invoke(Method.java:507)
10-22 02:14:24.313: E/AndroidRuntime(1660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-22 02:14:24.313: E/AndroidRuntime(1660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-22 02:14:24.313: E/AndroidRuntime(1660): at dalvik.system.NativeStart.main(Native Method)
10-22 02:14:24.313: E/AndroidRuntime(1660): Caused by: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.androidhive.jsonparsing/com.androidhive.jsonparsing.mainpage}: java.lang.NullPointerException
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.widget.TabHost.addTab(TabHost.java:216)
10-22 02:14:24.313: E/AndroidRuntime(1660): at com.androidhive.jsonparsing.kwongwahtab.onCreate(kwongwahtab.java:55)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-22 02:14:24.313: E/AndroidRuntime(1660): ... 11 more
10-22 02:14:24.313: E/AndroidRuntime(1660): Caused by: java.lang.NullPointerException
10-22 02:14:24.313: E/AndroidRuntime(1660): at com.androidhive.jsonparsing.mainpage.<init>(mainpage.java:25)
10-22 02:14:24.313: E/AndroidRuntime(1660): at java.lang.Class.newInstanceImpl(Native Method)
10-22 02:14:24.313: E/AndroidRuntime(1660): at java.lang.Class.newInstance(Class.java:1409)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-22 02:14:24.313: E/AndroidRuntime(1660): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
10-22 02:14:24.313: E/AndroidRuntime(1660): ... 20 more
任何人都可以帮助我吗? 这是我的标签活动
package com.androidhive.jsonparsing;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
@SuppressWarnings("deprecation")
public class kwongwahtab extends TabActivity {
/** Called when the activity is first created. */
private static String url = "http://www.kwongwah.com.my/channels2.plist";
private static final String TAG_CATEGORY = "categorylist";
private static final String TAG_TITLE = "title";
private static final String TAG_URL = "url";
public static String url2;
JSONArray category = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
category = json.getJSONArray(TAG_CATEGORY);
// looping through All Contacts
for(int i = 0; i < category.length(); i++){
JSONObject c = category.getJSONObject(i);
// Storing each json item in variable
String title = c.getString(TAG_TITLE);
String url = "http://www.kwongwah.com.my/plist3.php?json&"+c.getString(TAG_URL);
TabSpec page = tabHost.newTabSpec(title);
Intent pageIntent = new Intent(this, mainpage.class);
pageIntent.putExtra("url", url);
page.setIndicator(title);
page.setContent(pageIntent);
tabHost.addTab(page);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
mainpage.java 包com.androidhive.jsonparsing;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class mainpage extends ListActivity {
// url to make request
String text = getIntent().getStringExtra("url");
private String url = text;
// JSON Node names
private static final String TAG_CONTACTS = "new_list";
private static final String TAG_SUBJECT = "subject";
private static final String TAG_HTML = "html";
private static final String TAG_ADDED = "added";
// contacts JSONArray
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String subject = c.getString(TAG_SUBJECT);
//String html = "<img src='http://www.kwongwah.com.my/news/2012/10/18/32_1.jpg'>";
String html = c.getString(TAG_HTML);
String added = c.getString(TAG_ADDED);
// Phone number is agin JSON Object
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_SUBJECT, subject);
map.put(TAG_HTML, html);
map.put(TAG_ADDED, added);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_SUBJECT, TAG_HTML, TAG_ADDED }, new int[] {
R.id.subject, R.id.html, R.id.added});
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String subject = ((TextView) view.findViewById(R.id.subject)).getText().toString();
String html = ((TextView) view.findViewById(R.id.html)).getText().toString();
String added = ((TextView) view.findViewById(R.id.added)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_SUBJECT, subject);
in.putExtra(TAG_HTML, html);
in.putExtra(TAG_ADDED, added);
startActivity(in);
}
});
}
}
答案 0 :(得分:0)
这就是问题所在。您无法在声明成员变量的位置引用getIntent()
方法。它将在类的构造函数中隐式调用。构造函数将在活动每次绑定到Context或赋予intent之前发生。
public class mainpage extends ListActivity {
// url to make request ... -- This will only be valid from onCreate() to onDestroy()
String text = getIntent().getStringExtra("url");
将此类代码移至生命周期方法,例如onCreate()
:
public class mainpage extends ListActivity {
// url to make request
String text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// At this point the Context is bound and the Intent is set on it.
text = getIntent().getStringExtra("url");
setContentView(R.layout.layout);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();