你好我是android开发的新手,我做了一个小测试,但它给了我那个错误: “不幸的是,android测试已经停止了”
我在这里寻找了一些类似的主题,但没有人解决我的问题,这是我的代码:
主要活动:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
final EditText username = (EditText) findViewById(R.id.username);
final EditText password = (EditText) findViewById(R.id.password);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(checkLogin(username.getText().toString(),password.getText().toString())){
Intent i = new Intent(MainActivity.this, HelloActivity.class);
startActivity(i);
}
else{
username.setText("");
password.setText("");
}
}
});
}
private boolean checkLogin(String u,String p){
if (u == "hello" && p == "world")
return true;
else return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
主要布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.androidtest.MainActivity$PlaceholderFragment" >
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:ems="10"
android:hint="username" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/username"
android:layout_below="@+id/username"
android:ems="10"
android:hint="password"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/username"
android:layout_below="@+id/password"
android:text="Button" />
</RelativeLayout>
第二个活动
package com.androidtest;
import android.app.Activity;
import android.os.Bundle;
public class HelloActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.hello_layout);
}
}
其布局
<?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:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="193dp"
android:text="Afin akhay lpchiiwr"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
最后是Manifest文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.androidtest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.androidtest.HelloActivity"></activity>
</application>
</manifest>
感谢名单
答案 0 :(得分:1)
您发布的第一个xml块看起来就像是来自fragment_main.xml
文件。如果是这样,那么你的问题是findViewById()
正在寻找已经膨胀到片段中的活动中的视图。如果您不需要片段,则将该xml移动到activity_main.xml
并删除片段的代码。否则,将View初始化和方法移动到Fragment。
而且,正如Kedarnath指出的那样,你应该使用String.equals()
方法来比较字符串。
答案 1 :(得分:0)
首先让我告诉你,你正在以错误的方式比较String,
private boolean checkLogin(String u,String p)
{
if (u == "hello" && p == "world") // Wrong way
return true;
else return false;
}
您需要按以下方式比较字符串,
private boolean checkLogin(String u,String p)
{
if (u.equals("hello") && p.equals("world") ) // Right way
return true;
else return false;
}