无法在android

时间:2017-04-06 10:32:37

标签: android exception

我在这里创建应用程序是这个应用程序的链接onlineContact这个应用程序运行正常,当我从Android工作室通过连接移动与它安装。但是,当我从Play商店安装它时,它会崩溃我调试Play商店应用程序并找到一些意外的错误,因为当我从android studio直接运行应用程序时没有这样的错误。

日志

 java.lang.IllegalStateException: Could not find method createAccount(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btnCreateAccount'

 D/AccessibilityManager: setStateLocked: wasEnabled = false, mIsEnabled = false, wasTouchExplorationEnabled = false, mIsTouchExplorationEnabled = false, wasHighTextContrastEnabled = false, mIsHighTextContrastEnabled = false
                                                     java.lang.Throwable: setStateLocked

第二个不是错误,但它显示应用程序崩溃时。这些错误来自 LoginActivity ,这里是代码 的 LoginActivity

 public class LoginActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
}

protected void login(View view){
    EditText etxtPhoneNumber = (EditText) findViewById(R.id.etxtPhoneNumber);
    EditText etxtPassword = (EditText) findViewById(R.id.etxtPassword);
    String phoneNumber = etxtPhoneNumber.getText().toString();
    String password = etxtPassword.getText().toString();
    String action = "login";

    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    backgroundWorker.execute(action, phoneNumber, password);
}


protected void forgetPassActivity(View view){
    startActivity(new Intent(this, ForgetPassword.class));
}

protected void createAccount(View view){
    Intent loginIntent = new Intent(this, CreateAccount.class);
    startActivity(loginIntent);
}

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}

}

MainActivity

 public class MainActivity extends AppCompatActivity {

public static String SHARED_PREF_FILENAME = "com.azeem.onlinecontact.MYSHAREDPREF";
SharedPreferences sharedPref;
InterstitialAd interstitialAd;

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Placing Ads in Application

    MobileAds.initialize(this, "ca-app-pub-XXXX");
    AdView adView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId("ca-app-pub-XXXXX");
    interstitialAd.loadAd(adRequest);
    interstitialAd.setAdListener(new AdListener(){
        @Override
        public void onAdLoaded() {
            if(interstitialAd.isLoaded()){
                interstitialAd.show();
            }
        }
    });

       sharedPref = getSharedPreferences(SHARED_PREF_FILENAME, MODE_PRIVATE);
        Boolean login = sharedPref.getBoolean("login", false);
        if (login) {
               // Send user to login Activity
            Intent mainActivity = new Intent(this, LoginActivity.class);
            startActivity(mainActivity);
       }
       // Rest of the code.

activity_login.xml

   <?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">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:ems="10"
    android:layout_below="@+id/etxtPhoneNumber"
    android:layout_alignLeft="@+id/etxtPhoneNumber"
    android:layout_alignStart="@+id/etxtPhoneNumber"
    android:layout_marginTop="39dp"
    android:id="@+id/etxtPassword"
    android:hint="@string/enter_password" />

<Button
    android:text="@string/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/etxtPassword"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="31dp"
    android:id="@+id/btnLogin"
    android:onClick="login"
    style="@style/Widget.AppCompat.Button.Colored" />

<Button
    android:text="@string/create_account"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="45dp"
    android:id="@+id/btnCreateAccount"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="createAccount"
    style="@style/Widget.AppCompat.Button.Colored" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:ems="10"
    android:layout_marginTop="147dp"
    android:id="@+id/etxtPhoneNumber"
    android:hint="@string/your_phone_number"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:text="@string/online_contact"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
    android:layout_marginRight="38dp"
    android:layout_marginEnd="38dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="54dp" />

<TextView
    android:text="@string/foget_password_click_here"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btnCreateAccount"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="24dp"
    android:id="@+id/tvForgetPassword"
    android:onClick="forgetPassActivity" />
    </RelativeLayout>    

我检查了一些stackoverflow答案,当你没有声明onClick函数或没有传递view等时出现这种错误。我认为还有其他一些错误。 记住当我从Android工作室运行应用程序时,相同的代码工作正常

1 个答案:

答案 0 :(得分:0)

LoginActivity

中替换以下3种方法
public void login(View view){
    EditText etxtPhoneNumber = (EditText) findViewById(R.id.etxtPhoneNumber);
    EditText etxtPassword = (EditText) findViewById(R.id.etxtPassword);
    String phoneNumber = etxtPhoneNumber.getText().toString();
    String password = etxtPassword.getText().toString();
    String action = "login";

    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    backgroundWorker.execute(action, phoneNumber, password);
}

public void forgetPassActivity(View view){
    startActivity(new Intent(this, ForgetPassword.class));
}

public void createAccount(View view){
    Intent loginIntent = new Intent(this, CreateAccount.class);
    startActivity(loginIntent);
}