致命错误java.lang.IllegalStateException:无法执行android:onClick的方法

时间:2018-08-27 05:33:58

标签: java android json xml

我遇到以下错误

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.standardcoach.ticketing, PID: 12763
                                                                         java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                             at android.view.View$DeclaredOnClickListener.onClick(View.java:4505)
                                                                             at android.view.View.performClick(View.java:5265)
                                                                             at android.view.View$PerformClick.run(View.java:21534)
                                                                             at android.os.Handler.handleCallback(Handler.java:815)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                             at android.os.Looper.loop(Looper.java:207)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5765)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
                                                                          Caused by: java.lang.reflect.InvocationTargetException
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at android.view.View$DeclaredOnClickListener.onClick(View.java:4500)
                                                                             at android.view.View.performClick(View.java:5265) 
                                                                             at android.view.View$PerformClick.run(View.java:21534) 
                                                                             at android.os.Handler.handleCallback(Handler.java:815) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:104) 
                                                                             at android.os.Looper.loop(Looper.java:207) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5765) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
                                                                          Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.standardcoach.ticketing/com.standardcoach.ticketing.ParcelActivity}; have you declared this activity in your AndroidManifest.xml?
                                                                             at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
                                                                             at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
                                                                             at android.app.Activity.startActivityForResult(Activity.java:3968)
                                                                             at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
                                                                             at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
                                                                             at android.app.Activity.startActivityForResult(Activity.java:3920)
                                                                             at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
                                                                             at android.app.Activity.startActivity(Activity.java:4259)
                                                                             at android.app.Activity.startActivity(Activity.java:4227)
                                                                             at com.standardcoach.ticketing.MainActivity.parcels(MainActivity.java:189)
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at android.view.View$DeclaredOnClickListener.onClick(View.java:4500) 
                                                                             at android.view.View.performClick(View.java:5265) 
                                                                             at android.view.View$PerformClick.run(View.java:21534) 
                                                                             at android.os.Handler.handleCallback(Handler.java:815) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:104) 
                                                                             at android.os.Looper.loop(Looper.java:207) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5765) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 

通过mainActivity执行我的课程时,我遇到上述错误,错误提示第189行是

startActivity(new Intent(getApplicationContext(), ParcelActivity.class));

下面是我要点击的按钮的主要活动,即main_activity.xml

<android.support.v7.widget.AppCompatButton
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="24dp"
        android:padding="12dp"
        android:onClick="parcels"
        android:background="@color/white"
        android:textSize="24dip"
        android:text="PARCELS(MIZIGO)"/>

我在mainActivity类上的课程是

public void parcels(View view) {
if (LoginActivity.scanTicket.equalsIgnoreCase("Yes")) {
  startActivity(new Intent(getApplicationContext(), ParcelActivity.class));
} else {
  dialogMessage("parcel Activity Access Denied");
}}

以下是mainActivity中的包裹类要调用的类

parcelActivity.java是

公共类ParcelActivity         扩展AppCompatActivity {     私有静态字符串POST_URL;

EditText parcelNameBox;
EditText senderNameBox;
EditText senderPhoneBox;
EditText receiverNameBox;
EditText receiverPhoneBox;
EditText parcelFromBox;
EditText parcelToBox;
EditText parcelAmountBox;
EditText shipDateBox;
EditText parcelBusNumberBox;

String parcelName;
String senderName;
String senderPhone;
String receiverName;
String receiverPhone;
String parcelFrom;
String parcelTo;
String parcelAmountPaid;
String shipDate;
String parcelBusNumber;

private ProgressDialog progress;

private String capitalize(String paramString)
{
    StringBuffer localStringBuffer = new StringBuffer();
    Matcher localMatcher = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(paramString);
    while (localMatcher.find()) {
        localMatcher.appendReplacement(localStringBuffer, localMatcher.group(1).toUpperCase() + localMatcher.group(2).toLowerCase());
    }
    return localMatcher.appendTail(localStringBuffer).toString();
}

void dialogMessage(String paramString)
{
    AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
    localBuilder.setMessage(paramString);
    localBuilder.setCancelable(false);
    localBuilder.setNegativeButton("Okay", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
        {
            paramAnonymousDialogInterface.cancel();
        }
    });
    localBuilder.create().show();
}

public void onBackPressed()
{
    super.onBackPressed();
    startActivity(new Intent(getApplicationContext(), ChooseSeatActivity.class));
}

protected void onCreate(Bundle paramBundle)
{
    super.onCreate(paramBundle);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setTitle("RECORD PARCEL");
    getSupportActionBar().setLogo(R.mipmap.ic_launcher);
    getSupportActionBar().setDisplayUseLogoEnabled(true);
    setContentView(R.layout.activity_record_parcel);
    POST_URL = LoginActivity.base_url + "Webservice/parcel";
    this.parcelNameBox = ((EditText)findViewById(R.id.parcelNameBox));
    this.senderNameBox = ((EditText)findViewById(R.id.senderNameBox));
    this.senderPhoneBox = ((EditText)findViewById(R.id.senderPhoneBox));
    this.receiverNameBox = ((EditText)findViewById(R.id.receiverNameBox));
    this.receiverPhoneBox = ((EditText)findViewById(R.id.receiverPhoneBox));
    this.parcelFromBox = ((EditText)findViewById(R.id.parcelFromBox));
    this.parcelToBox = ((EditText)findViewById(R.id.parcelToBox));
    this.parcelAmountBox = ((EditText)findViewById(R.id.parcelAmountBox));
    this.shipDateBox = ((EditText)findViewById(R.id.shipDateBox));
    this.parcelBusNumberBox = ((EditText)findViewById(R.id.parcelBusNumberBox));


}

public void registerParcel(View paramView)
{
    this.parcelName = this.parcelNameBox.getText().toString();
    this.parcelName = capitalize(this.parcelName);
    this.senderName = this.senderNameBox.getText().toString();
    this.senderName = capitalize(this.senderName);
    this.senderPhone = this.senderPhoneBox.getText().toString();
    this.receiverName = this.receiverNameBox.getText().toString();
    this.receiverName = capitalize(this.receiverName);
    this.receiverPhone = this.receiverPhoneBox.getText().toString();
    this.parcelFrom = this.parcelFromBox.getText().toString();
    this.parcelFrom = capitalize(this.parcelFrom);
    this.parcelTo = this.parcelToBox.getText().toString();
    this.parcelTo = capitalize(this.parcelTo);
    this.parcelAmountPaid = this.parcelAmountBox.getText().toString();
    this.shipDate = this.shipDateBox.getText().toString();
    this.parcelBusNumber = this.parcelBusNumberBox.getText().toString();
    Constants.parcelName = this.parcelName;
    Constants.senderName = this.senderName;
    Constants.senderPhone = this.senderPhone;
    Constants.receiverName = this.receiverName;
    Constants.receiverPhone = this.receiverPhone;
    Constants.parcelFrom = this.parcelFrom;
    Constants.parcelTo = this.parcelTo;
    Constants.parcelAmountPaid = this.parcelAmountPaid;
    Constants.shipDate = this.shipDate;
    Constants.parcelBusNumber = this.parcelBusNumber;

    if ((this.parcelName.length() > 0) && (this.senderName.length() > 0) && (this.senderPhone.length() > 0) && (this.receiverName.length() > 0) && (this.receiverPhone.length() > 0) && (this.parcelFrom.length() > 0) && (this.parcelTo.length() > 0) && (this.parcelAmountPaid.length() > 0)&& (this.shipDate.length() > 0) && (this.parcelBusNumber.length() > 0))
    {
        if (this.senderPhone.length() == 12 && this.receiverPhone.length() == 12)
        {


            new PostClass(this).execute(parcelName,senderName,senderPhone,receiverName,receiverPhone,parcelFrom,parcelTo,parcelAmountPaid,shipDate,parcelBusNumber);
            return;
        }
        dialogMessage("Weka Namba ya Simu Sahihi");
        return;
    }
    dialogMessage("Hakikisha Umejaza Taarifa Zote za Mzigo");
}

private class PostClass extends AsyncTask<String, Void, String> {
    private final Context context;
    String result = "";

    public PostClass(Context c) {
        this.context = c;
    }

    protected void onPreExecute() {
        ParcelActivity.this.progress = new ProgressDialog(this.context);
        ParcelActivity.this.progress.setCancelable(false);
        ParcelActivity.this.progress.setMessage("Please Wait.....");
        ParcelActivity.this.progress.show();
    }

    protected String doInBackground(String... params) {
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(ParcelActivity.POST_URL).openConnection();
            String urlParameters = "parcelName=" + ParcelActivity.this.parcelName +
                    "&&senderName=" + ParcelActivity.this.senderName +
                    "&&senderPhone=" + ParcelActivity.this.senderPhone +
                    "&&receiverName=" + ParcelActivity.this.receiverName +
                    "&&receiverPhone=" + ParcelActivity.this.receiverPhone +
                    "&&parcelFrom=" + ParcelActivity.this.parcelFrom +
                    "&&parcelTo=" + ParcelActivity.this.parcelTo +
                    "&&parcelAmount=" + ParcelActivity.this.parcelAmountPaid +
                    "&&shipDate=" + ParcelActivity.this.shipDate +
                    "&&agentName=" + LoginActivity.agentName +
                    "&&agentPhone=" + LoginActivity.agentPhone +
                    "&&parcelBusNumber=" + ParcelActivity.this.parcelBusNumber;
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
            dStream.writeBytes(urlParameters);
            dStream.flush();
            dStream.close();
            StringBuilder output = new StringBuilder("");
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = "";
            StringBuilder responseOutput = new StringBuilder();
            while (true) {
                line = br.readLine();
                if (line == null) {
                    break;
                }
                responseOutput.append(line);
            }
            br.close();
            output.append(responseOutput.toString());
            this.result = responseOutput.toString();
        } catch (MalformedURLException e) {
            this.result = e.getMessage();
        } catch (IOException e2) {
            this.result = e2.getMessage();
        }
        return this.result;
    }

    protected void onPostExecute(String result) {
        ParcelActivity.this.progress.dismiss();
        System.out.println(result);
        try {
            JSONObject jsonObj = new JSONObject(result);
            String parcelNumber = jsonObj.getString("parcelNumber");
            Constants.parcelTicket = parcelNumber;
      //    Constants.agentCode = LoginActivity.agentCode;


            if (LoginActivity.agentGroup.equalsIgnoreCase("Agent") || LoginActivity.agentGroup.equalsIgnoreCase("All")) {


                ParcelActivity.this.startActivity(new Intent(ParcelActivity.this.getApplicationContext(), PrintParcelActivity.class));
            } else {
                ParcelActivity.this.dialogMessage("Try Again...");
            }

        } catch (JSONException e) {
            ParcelActivity.this.dialogMessage("Jaribu Tena..Kuna Tatizo");
            Log.d("ERROR: ", e.getLocalizedMessage());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

错误提示

  

原因:android.content.ActivityNotFoundException:无法找到   显式活动课   {com.standardcoach.ticketing / com.standardcoach.ticketing.ParcelActivity};   您是否在AndroidManifest.xml中声明了此活动?

将活动添加到manifest.xml文件中。

  <activity
            android:name="com.standardcoach.ticketing.ParcelActivity"/>