我正在制作天气应用程序,我想在列表视图中显示其详细信息,我遇到了问题,它给了我错误,我不明白我一直在努力解决它,我想不出任何其他方式。请不要需要帮助我希望我能很好地解释这个问题
这是我的错误
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.helloworldng.bip/com.helloworldng.bip.navigate.Search}: java.lang.InstantiationException: class com.helloworldng.bip.navigate.Search has no zero argument constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.InstantiationException: class com.helloworldng.bip.navigate.Search has no zero argument constructor
at java.lang.Class.newInstance(Class.java:1597)
at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.NoSuchMethodException: <init> []
at java.lang.Class.getConstructor(Class.java:531)
at java.lang.Class.getDeclaredConstructor(Class.java:510)
at java.lang.Class.newInstance(Class.java:1595)
at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
这是第一堂课
package com.helloworldng.bip.navigate;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.helloworldng.bip.GetTrafficReportTask;
import com.helloworldng.bip.R;
import com.helloworldng.bip.data.BIPContract;
public class Search extends ListActivity {
private final String LOG_TAG = GetTrafficReportTask.class.getSimpleName();
public static final String BIP_STATE = "state";
public static final String BIP_CITY = "city";
public static final String BIP_DESC = "description";
public static final String BIP_STATUS = "status";
private static final String URL_CATEGORY = "http://www.helloworldng.com/bip/get_traffic.php";
private final Context mContext;
//public FetchWeatherTask(Context context, ArrayAdapter<String> forecastAdapter) {
public Search(Context context) {
mContext = context;
//mForecastAdapter = forecastAdapter;
}
private BaseAdapter mAdapter;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.droptrafic);
lv = getListView();
lv.setDivider(null);
final Handler handler = new Handler();
Runnable refresh = new Runnable() {
@Override
public void run() {
handler.postDelayed(this, 60 * 1000);
}
};
handler.postDelayed(refresh, 60 * 1000);
}
private void getTrafficDataFromJson(String forecastJsonStr)
throws JSONException {
try {
JSONArray trafficArray = new JSONArray(forecastJsonStr);
//JSONArray trafficArray = forecastJson.getJSONArray(BIP_TRAFFIC);
// Insert the new weather information into the database
Vector<ContentValues> cVVector = new Vector<ContentValues>(trafficArray.length());
Time dayTime = new Time();
dayTime.setToNow();
dayTime = new Time();
for (int i = 0; i < trafficArray.length(); i++) {
String state;
String status;
JSONObject dayTraffic = trafficArray.getJSONObject(i);
state = dayTraffic.getString(BIP_STATE);
status = dayTraffic.getString(BIP_STATUS);
final ListAdapter adapter = new SimpleAdapter(this, (List<? extends Map<String, ?>>) dayTraffic,
R.layout.single_drop, new String[] { state,status
}, new int[] { R.id.title, R.id.desc});
setListAdapter(adapter);
ListView lv = getListView();
}
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
//return null;
}
protected Void doInBackground(String... params) {
// If there's no zip code, there's nothing to look up. Verify size of params.
if (params.length == 0) {
return null;
}
String locationQuery = params[0];
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
String format = "json";
String units = "metric";
int numDays = 7;
String app_id = "b7940b102bb44bb3c5accd21eb44da00";
try {
// Construct the URL for the OpenWeatherMap query
// Possible parameters are avaiable at OWM's forecast API page, at
// http://openweathermap.org/API#forecast
final String FORECAST_BASE_URL =
"http://www.helloworldng.com/bip/get_traffic.php";
final String QUERY_PARAM = "q";
final String FORMAT_PARAM = "mode";
final String UNITS_PARAM = "units";
final String DAYS_PARAM = "cnt";
final String APPID = "appid";
Uri builtUri = Uri.parse(FORECAST_BASE_URL);
URL url = new URL(builtUri.toString());
Log.e("Today Traffic", url.toString());
// Create the request to OpenWeatherMap, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
forecastJsonStr = buffer.toString();
Log.e(LOG_TAG + " Manual", forecastJsonStr);
getTrafficDataFromJson(forecastJsonStr);
} catch (IOException e) {
Log.e(LOG_TAG, "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attempting
// to parse it.
// return null;
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "Error closing stream", e);
}
}
}
return null;
}
}
这是listview xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView" />
</LinearLayout>
这是第一个xml类single_drop
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Heavy Traffic"
android:id="@+id/status" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="state"
android:id="@+id/desc"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
答案 0 :(得分:1)
你应该在你的活动构造函数中调用super(context)。