[英]Error when i extend AppCompatActivity in Android studio
Hello my code makes a simple http request and returns a json. 您好,我的代码提出了一个简单的http请求,并返回了json。 My Code works fine but when i try to extend AppCompatActivity (because i want to show the json on device screen) it gaves me multiple errors my code is below
我的代码工作正常,但是当我尝试扩展AppCompatActivity时(因为我想在设备屏幕上显示json),它给了我多个错误,我的代码如下
package com.vogella.java.library.okhttp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import org.json.JSONException;
import org.json.JSONObject;
public class TestMain extends AppCompatActivity {
OkHttpClient client = new OkHttpClient();
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
String doPostRequest(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static void main(String[] args) throws IOException, JSONException {
String username = "test";
String password = "123";
TestMain example = new TestMain();
String json = "";
String postResponse = example.doPostRequest("http://86.145.89.53:8080/authentication_service-dev/authenticate?username=" + username + "&password=" + password, json);
int counter = postResponse.length();
if (counter == 14) {
System.out.println("Your username or password is invalid!!");
}
if (counter > 1000) {
System.out.println("HTTP Status 401 – Unauthhorized");
}
if (counter < 1000 && counter > 14) {
System.out.println("Welcome!!");
}
}}
the errors are : 错误是:
Exception in thread "main" java.lang.RuntimeException: Stub!
at android.content.Context.<init>(Context.java:20)
at android.content.ContextWrapper.<init>(ContextWrapper.java:21)
at android.view.ContextThemeWrapper.<init>(ContextThemeWrapper.java:21)
at android.app.Activity.<init>(Activity.java:22)
at android.support.v4.app.SupportActivity.<init>(SupportActivity.java:31)
at android.support.v4.app.BaseFragmentActivityGingerbread.<init>(BaseFragmentActivityGingerbread.java:37)
at android.support.v4.app.BaseFragmentActivityHoneycomb.<init>(BaseFragmentActivityHoneycomb.java:29)
at android.support.v4.app.BaseFragmentActivityJB.<init>(BaseFragmentActivityJB.java:30)
at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:79)
at android.support.v7.app.AppCompatActivity.<init>(AppCompatActivity.java:61)
at com.vogella.java.library.okhttp.TestMain.<init>(TestMain.java:18)
at com.vogella.java.library.okhttp.TestMain.main(TestMain.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
You don't use public static void main
in Android dev. 您不要在Android开发人员中使用
public static void main
。 In the activity, you use onCreate: 在活动中,使用onCreate:
@Override
public void onCreate(Bundle sis){
super.onCreate(sis);
//Move the contents of "public static void main" here
}
And delete public static void main
when you have moved the code. 移动代码后,请删除
public static void main
。
Android activities must have onCreate()
method, when you extend any class with AppCompactAcitvity
it must implement some methods Android活动必须具有
onCreate()
方法,当您使用AppCompactAcitvity
扩展任何类时,它必须实现一些方法
Change your code, remove main method, add onCreate()
and do stuff there. 更改您的代码,删除main方法,
add onCreate()
并在那里做一些事情。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_acremote);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.