activity 在虚拟设备中 运行 时强制停止
activity forcestop when run in virtual device
帮帮我
这段代码有什么问题
我知道问题出在创建方法上
当设置为提交按钮的点击侦听器时
当我想要 运行 虚拟设备中的应用程序时,我会强制停止
我该如何解决
public class Addmoney extends AppCompatActivity {
AutoCompleteTextView textv;
Context context = this;
private boolean cansend;
private RadioButton radioCreditor;
private RadioButton radioDebtor;
private Button Commit;
private int as = 0;//1=creditor;2=debtor
@Override
protected void onCreate(Bundle savedInstanceState) {
radioCreditor = (RadioButton) findViewById(R.id.radiocreditor);
radioDebtor = (RadioButton) findViewById(R.id.radiocreditor);
Commit = (Button) findViewById(R.id.btnCommit);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addmoney);
Button btn = (Button) findViewById(R.id.btcheckid);
textv = (AutoCompleteTextView) findViewById(R.id.textuser);
btn.setOnClickListener(new View.OnClickListener() {
Editable str = textv.getText();
@Override
public void onClick(View v) {
showprogresscheckid(true);
checkID check = new checkID(str.toString(), context);
check.execute();
}
});
Commit.setOnClickListener(new View.OnClickListener() {
Editable str = textv.getText();
@Override
public void onClick(View v) {
checkID check = new checkID(str.toString(), context);
check.execute();
}
});
}
void showprogresscheckid(final boolean turn) {
ProgressBar pr = (ProgressBar) findViewById(R.id.progressBarcheckid);
Button bt = (Button) findViewById(R.id.btcheckid);
bt.setVisibility(turn ? View.GONE : View.VISIBLE);
pr.setVisibility(turn ? View.VISIBLE : View.GONE);
return;
}
class checkID extends AsyncTask<Void, Void, Integer> {
String user;
String mLink = "http://test.kholaseketab.ir/checkid.php";
Context contex;
@Override
protected void onPostExecute(Integer integer) {
if (integer == 1) {
Toast.makeText(context, "ok", Toast.LENGTH_LONG).show();
}
if (integer == 0) {
Toast.makeText(context, "not", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "e4", Toast.LENGTH_LONG).show();
}
showprogresscheckid(false);
super.onPostExecute(integer);
}
public checkID(String User, Context cnt) {
super();
contex = cnt;
user = User;
}
@Override
protected Integer doInBackground(Void... params) {
try {
String data = URLEncoder.encode("user", "UTF8") + "=" + URLEncoder.encode(user, "UTF8");
URL mylink = new URL(mLink);
URLConnection connect = mylink.openConnection();
connect.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(connect.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
StringBuilder sb = new StringBuilder();
String res = reader.readLine();
if (res.trim().equals("ok")) {
return 1;
} else if (res.trim().equals("not")) {
return 0;
} else {
return -1;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
}
}
和我的布局 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:background="@mipmap/light"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/test"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:layout_gravity="center">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:layout_gravity="center_vertical"
android:gravity="right">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check user"
android:id="@+id/btcheckid"
android:gravity="center"
android:layout_gravity="center_vertical|right" />
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/progressBarcheckid"
android:layout_gravity="center_vertical|right"
android:visibility="gone" />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/view"
android:layout_weight="1"
android:gravity="center">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textuser"
android:text="mohsen"
android:maxLines="1"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Az Creditor"
android:id="@+id/radiocreditor"
android:layout_gravity="center_horizontal"
android:keepScreenOn="false"
android:enabled="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Az Debtor"
android:id="@+id/radiodebtor"
android:layout_gravity="center_horizontal" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Cost :"
android:id="@+id/textView2"
android:layout_gravity="center"
android:gravity="center" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText"
android:layout_gravity="left|center"
android:gravity="fill_horizontal|left"
android:enabled="true" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Details :"
android:id="@+id/textView3"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText2"
android:layout_gravity="center"
android:layout_weight="1" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Commit"
android:id="@+id/btnCommit"
android:layout_gravity="center_horizontal" />
Logcat:
02-03 18:06:41.163 10733-10733/com.example.mohsen158.test1 E/GMPM:
GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services. Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.
02-03 18:06:41.167 10733-10733/com.example.mohsen158.test1 E/GMPM: Scheduler not set. Not logging error/warn.
02-03 18:06:41.187 10733-10747/com.example.mohsen158.test1 E/GMPM: Uploading is not possible. App measurement disabled
02-03 18:06:41.563 10733-10733/com.example.mohsen158.test1 E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache
02-03 18:06:41.563 10733-10733/com.example.mohsen158.test1 E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384
02-03 18:06:41.567 10733-10733/com.example.mohsen158.test1 E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-03 18:06:41.567 10733-10733/com.example.mohsen158.test1 E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384
02-03 18:06:44.223 10733-10733/com.example.mohsen158.test1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mohsen158.test1, PID: 10733
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mohsen158.test1/com.example.mohsen158.test1.Addmoney}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access0(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:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.mohsen158.test1.Addmoney.onCreate(Addmoney.java:62)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access0(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:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
将这些代码放在 setContentView()
之后
radioCreditor = (RadioButton) findViewById(R.id.radiocreditor);
radioDebtor = (RadioButton) findViewById(R.id.radiocreditor);
Commit = (Button) findViewById(R.id.btnCommit);
在设置 activity 视图之前,您不能使用 findViewById
,它不会找到任何 view/controller。
你错过了setContentView.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_XML);
.....
以下 3 行移动
radioCreditor = (RadioButton) findViewById(R.id.radiocreditor);
radioDebtor = (RadioButton) findViewById(R.id.radiocreditor);
Commit = (Button) findViewById(R.id.btnCommit);
在
之后
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addmoney);
这两行应该在 onCreate(Bundle savedInstanceState) 方法的顶部,因为如果不膨胀 xml 你将要查找单选按钮,这是错误的步骤,所以在 onCreate()[=11= 中作为起始行移动]
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addmoney);
帮帮我 这段代码有什么问题 我知道问题出在创建方法上 当设置为提交按钮的点击侦听器时 当我想要 运行 虚拟设备中的应用程序时,我会强制停止 我该如何解决
public class Addmoney extends AppCompatActivity {
AutoCompleteTextView textv;
Context context = this;
private boolean cansend;
private RadioButton radioCreditor;
private RadioButton radioDebtor;
private Button Commit;
private int as = 0;//1=creditor;2=debtor
@Override
protected void onCreate(Bundle savedInstanceState) {
radioCreditor = (RadioButton) findViewById(R.id.radiocreditor);
radioDebtor = (RadioButton) findViewById(R.id.radiocreditor);
Commit = (Button) findViewById(R.id.btnCommit);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addmoney);
Button btn = (Button) findViewById(R.id.btcheckid);
textv = (AutoCompleteTextView) findViewById(R.id.textuser);
btn.setOnClickListener(new View.OnClickListener() {
Editable str = textv.getText();
@Override
public void onClick(View v) {
showprogresscheckid(true);
checkID check = new checkID(str.toString(), context);
check.execute();
}
});
Commit.setOnClickListener(new View.OnClickListener() {
Editable str = textv.getText();
@Override
public void onClick(View v) {
checkID check = new checkID(str.toString(), context);
check.execute();
}
});
}
void showprogresscheckid(final boolean turn) {
ProgressBar pr = (ProgressBar) findViewById(R.id.progressBarcheckid);
Button bt = (Button) findViewById(R.id.btcheckid);
bt.setVisibility(turn ? View.GONE : View.VISIBLE);
pr.setVisibility(turn ? View.VISIBLE : View.GONE);
return;
}
class checkID extends AsyncTask<Void, Void, Integer> {
String user;
String mLink = "http://test.kholaseketab.ir/checkid.php";
Context contex;
@Override
protected void onPostExecute(Integer integer) {
if (integer == 1) {
Toast.makeText(context, "ok", Toast.LENGTH_LONG).show();
}
if (integer == 0) {
Toast.makeText(context, "not", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "e4", Toast.LENGTH_LONG).show();
}
showprogresscheckid(false);
super.onPostExecute(integer);
}
public checkID(String User, Context cnt) {
super();
contex = cnt;
user = User;
}
@Override
protected Integer doInBackground(Void... params) {
try {
String data = URLEncoder.encode("user", "UTF8") + "=" + URLEncoder.encode(user, "UTF8");
URL mylink = new URL(mLink);
URLConnection connect = mylink.openConnection();
connect.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(connect.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
StringBuilder sb = new StringBuilder();
String res = reader.readLine();
if (res.trim().equals("ok")) {
return 1;
} else if (res.trim().equals("not")) {
return 0;
} else {
return -1;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
}
}
和我的布局 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:background="@mipmap/light"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/test"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:layout_gravity="center">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:layout_gravity="center_vertical"
android:gravity="right">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check user"
android:id="@+id/btcheckid"
android:gravity="center"
android:layout_gravity="center_vertical|right" />
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/progressBarcheckid"
android:layout_gravity="center_vertical|right"
android:visibility="gone" />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/view"
android:layout_weight="1"
android:gravity="center">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textuser"
android:text="mohsen"
android:maxLines="1"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Az Creditor"
android:id="@+id/radiocreditor"
android:layout_gravity="center_horizontal"
android:keepScreenOn="false"
android:enabled="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Az Debtor"
android:id="@+id/radiodebtor"
android:layout_gravity="center_horizontal" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Cost :"
android:id="@+id/textView2"
android:layout_gravity="center"
android:gravity="center" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText"
android:layout_gravity="left|center"
android:gravity="fill_horizontal|left"
android:enabled="true" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Details :"
android:id="@+id/textView3"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText2"
android:layout_gravity="center"
android:layout_weight="1" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Commit"
android:id="@+id/btnCommit"
android:layout_gravity="center_horizontal" />
Logcat:
02-03 18:06:41.163 10733-10733/com.example.mohsen158.test1 E/GMPM:
GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services. Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.
02-03 18:06:41.167 10733-10733/com.example.mohsen158.test1 E/GMPM: Scheduler not set. Not logging error/warn.
02-03 18:06:41.187 10733-10747/com.example.mohsen158.test1 E/GMPM: Uploading is not possible. App measurement disabled
02-03 18:06:41.563 10733-10733/com.example.mohsen158.test1 E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache
02-03 18:06:41.563 10733-10733/com.example.mohsen158.test1 E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384
02-03 18:06:41.567 10733-10733/com.example.mohsen158.test1 E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-03 18:06:41.567 10733-10733/com.example.mohsen158.test1 E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384
02-03 18:06:44.223 10733-10733/com.example.mohsen158.test1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mohsen158.test1, PID: 10733
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mohsen158.test1/com.example.mohsen158.test1.Addmoney}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access0(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:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.mohsen158.test1.Addmoney.onCreate(Addmoney.java:62)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access0(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:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
将这些代码放在 setContentView()
radioCreditor = (RadioButton) findViewById(R.id.radiocreditor);
radioDebtor = (RadioButton) findViewById(R.id.radiocreditor);
Commit = (Button) findViewById(R.id.btnCommit);
在设置 activity 视图之前,您不能使用 findViewById
,它不会找到任何 view/controller。
你错过了setContentView.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_XML);
.....
以下 3 行移动
radioCreditor = (RadioButton) findViewById(R.id.radiocreditor);
radioDebtor = (RadioButton) findViewById(R.id.radiocreditor);
Commit = (Button) findViewById(R.id.btnCommit);
在
之后 super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addmoney);
这两行应该在 onCreate(Bundle savedInstanceState) 方法的顶部,因为如果不膨胀 xml 你将要查找单选按钮,这是错误的步骤,所以在 onCreate()[=11= 中作为起始行移动]
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addmoney);