我跟随Volley
这是我的代码但由于某种原因最后一行queue.add(stringRequest);与未知类错误。
public class MainActivity extends ActionBarActivity {
final TextView mTextView = (TextView)findViewById(R.id.text);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
queue.add(stringRequest)
}
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.google.com";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mTextView.setText("Response is: " + response.substring(0, 500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didnt work!");
}
});
}
你知道为什么会出错吗?
以下是错误
Process: za.co.stillie.networktest, PID: 11113
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{za.co.stillie.networktest/za.co.stillie.networktest.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1884)
at za.co.stillie.networktest.MainActivity.<init>(MainActivity.java:25)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
上面的错误是我在代码
中尝试运行我的应用程序时遇到的错误注意:我将queue.add(stringRequest)移动到onCreate方法
答案 0 :(得分:2)
似乎queue.add(stringRequest)
在MainActivity中,但在任何函数中都没有。
只需将queue.add(stringRequest)
放入onCreate或将被调用的任何其他函数中。
Unknown Class
是因为Java将'queue.add(stringRequest)'视为变量,它不知道它属于哪个类。
您没有错误的堆栈跟踪,因为Java不允许您运行此代码。
您提供的网站没有列出所有代码,只是简短的。
我没有足够的评论声誉...... 那么你是否在mannifest中声明了这个活动,以及网络权限?
我猜你应该在setContentView之后使用findViewById
。
final TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView)findViewById(R.id.text);
queue.add(stringRequest);
}
答案 1 :(得分:2)
在设置之前,您无法启动文本视图或任何视图 内容查看...
好的..我发现了错误...你首先将stringrequest添加到队列然后启动它...我已经更新了答案..检查它。
public class MainActivity extends ActionBarActivity {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.google.com";
//dont mean to do that...
//final TextView mTextView = (TextView)findViewById(R.id.text);
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView)findViewById(R.id.text);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mTextView.setText("Response is: " + response.substring(0, 500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didnt work!");
}
});
queue.add(stringRequest);
}
}