似乎有许多类似于我的问题,但那里发布的答案对我来说似乎并不合适。我的程序应该只是打开一个允许webview的新页面。 但是,当我尝试打开此页面时,错误显示没有很多关于问题所在的提示。
public class DetailedEventActivity extends Activity {
TextView name;
TextView desc;
TextView link;
String pid;
String myName;
String myDesc;
String myLink;
Button eLink;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single profile url
private static final String url_event_detials = "http://10.0.2.2:8888/week_6/get_all_event.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_EVENT = "event";
private static final String TAG_PID = "eventID";
private static final String TAG_NAME = "eventName";
private static final String TAG_DESC = "eventDescription";
private static final String TAG_LINK = "link";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_event);
// getting profile details from intent
Intent i = getIntent();
// getting profile id (pid) from intent
pid = i.getStringExtra(TAG_PID);
myName = i.getStringExtra(TAG_NAME);
myDesc = i.getStringExtra(TAG_DESC);
myLink = i.getStringExtra(TAG_LINK);
name = (TextView) findViewById(R.id.inputName);
desc = (TextView) findViewById(R.id.inputDesc);
//link = (TextView) findViewById(R.id.inputLink);
name.setText(myName);
desc.setText(myDesc);
link.setText(myLink);
eLink = (Button) findViewById(R.id.eventLink);
eLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
WebView webview = new WebView(DetailedEventActivity.this);
setContentView(webview);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webview.loadUrl(myLink);
}
});
// Getting complete profile details in background thread
}
这些代码可以引导到另一个班级。
public class ViewEventActivity extends ListActivity {
String pid;
String name;
String desc;
String link;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> eventList;
// url to get all profile list
private static String url_all_event = "http://10.0.2.2:8888/week_6/get_all_event.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_EVENT = "event";
private static final String TAG_PID = "eventID";
private static final String TAG_NAME = "eventName";
private static final String TAG_DESC = "eventDescription";
private static final String TAG_LINK = "link";
// profiles JSONArray
JSONArray event = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_event);
// Hashmap for ListView
eventList = new ArrayList<HashMap<String, String>>();
// Loading profiles in Background Thread
new LoadAllEvent().execute();
// Get listview
ListView lv = getListView();
// on seleting single event
// launching Edit event Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
// String pid = ((TextView) view.findViewById(R.id.pid)).getText().toString();
//Roy
String myPid = eventList.get(position).get(TAG_PID);
String myName = eventList.get(position).get(TAG_NAME);
String myDesc = eventList.get(position).get(TAG_DESC);
String myLink = eventList.get(position).get(TAG_LINK);
// Starting new intent
Intent in = new Intent(getApplicationContext(),DetailedEventActivity.class);
// sending pid to next activity
in.putExtra(TAG_PID, myPid);
Log.d("Put Extra ", "id = "+ myPid);
in.putExtra(TAG_NAME, myName);
Log.d("Put Extra ", "name = "+ myName);
in.putExtra(TAG_DESC, myDesc);
Log.d("Put Extra ", "desc = "+ myDesc);
in.putExtra(TAG_LINK, myLink);
Log.d("Put Extra ", "link = "+ myLink);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
// Response from Edit profile Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted profile
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all profile by making HTTP Request
* */
class LoadAllEvent extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ViewEventActivity.this);
pDialog.setMessage("Loading event. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All profiles from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_event, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Event ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// profiles found
// Getting Array of profiles
event = json.getJSONArray(TAG_EVENT);
// looping through All profiles
for (int i = 0; i < event.length(); i++) {
JSONObject c = event.getJSONObject(i);
Log.d("View Json ", "i = "+i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
Log.d("View Json ", "id = "+ id);
String name = c.getString(TAG_NAME);
Log.d("View Json ", "name = "+ name);
String desc = c.getString(TAG_DESC);
Log.d("View Json ", "desc = "+ desc);
String link = c.getString(TAG_LINK);
Log.d("View Json ", "link = "+ link);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_DESC, desc);
map.put(TAG_LINK, link);
// adding HashList to ArrayList
eventList.add(map);
}
} else {
// no profiles found
// Launch Add New profile Activity
Intent i = new Intent(getApplicationContext(),
MainScreenActivity.class);
//Main Page Aagin
pDialog = new ProgressDialog(ViewEventActivity.this);
pDialog.setMessage("Redirecting to main page");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all profiles
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
ViewEventActivity.this, eventList,
R.layout.list_event, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
我在LogCat上遇到的错误是:
11-17 11:21:55.819: E/AndroidRuntime(1391): FATAL EXCEPTION: main
11-17 11:21:55.819: E/AndroidRuntime(1391): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.DetailedEventActivity}: java.lang.NullPointerException
11-17 11:21:55.819: E/AndroidRuntime(1391): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-17 11:21:55.819: E/AndroidRuntime(1391): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-17 11:21:55.819: E/AndroidRuntime(1391): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-17 11:21:55.819: E/AndroidRuntime(1391): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-17 11:21:55.819: E/AndroidRuntime(1391): at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 11:21:55.819: E/AndroidRuntime(1391): at android.os.Looper.loop(Looper.java:137)
11-17 11:21:55.819: E/AndroidRuntime(1391): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-17 11:21:55.819: E/AndroidRuntime(1391): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 11:21:55.819: E/AndroidRuntime(1391): at java.lang.reflect.Method.invoke(Method.java:525)
11-17 11:21:55.819: E/AndroidRuntime(1391): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-17 11:21:55.819: E/AndroidRuntime(1391): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-17 11:21:55.819: E/AndroidRuntime(1391): at dalvik.system.NativeStart.main(Native Method)
11-17 11:21:55.819: E/AndroidRuntime(1391): Caused by: java.lang.NullPointerException
11-17 11:21:55.819: E/AndroidRuntime(1391): at com.example.test.DetailedEventActivity.onCreate(DetailedEventActivity.java:77)
11-17 11:21:55.819: E/AndroidRuntime(1391): at android.app.Activity.performCreate(Activity.java:5133)
11-17 11:21:55.819: E/AndroidRuntime(1391): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-17 11:21:55.819: E/AndroidRuntime(1391): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-17 11:21:55.819: E/AndroidRuntime(1391): ... 11 more
我知道这个问题可能看似微不足道,但我上个月刚开始学习java,所以非常感谢guidiance和帮助。谢谢!
答案 0 :(得分:1)
您正在访问链接对象而未进行适当的初始化:
//link = (TextView) findViewById(R.id.inputLink);
link.setText(myLink);
答案 1 :(得分:0)
你的textview链接导致空指针异常,
link.setText(myLink); // <-- null here
所以改变
//link = (TextView) findViewById(R.id.inputLink);
到
link = (TextView) findViewById(R.id.inputLink);