需要noob帮助理解如何调试

时间:2013-10-21 04:59:03

标签: java android android-activity android-asynctask onclick

原谅我的烂摊子我是新来的,在论坛和java编辑上发布错误报告。

目前我点击添加按钮,它只是一遍又一遍地崩溃。

LogCat

    10-21 04:52:30.000: D/gralloc_goldfish(531): Emulator without GPU emulation detected.
    10-21 04:53:08.681: D/AndroidRuntime(531): Shutting down VM
    10-21 04:53:08.681: W/dalvikvm(531): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
    10-21 04:53:08.712: E/AndroidRuntime(531): FATAL EXCEPTION: main
    10-21 04:53:08.712: E/AndroidRuntime(531): java.lang.IllegalStateException: Could not execute method of the activity
    10-21 04:53:08.712: E/AndroidRuntime(531):  at android.view.View$1.onClick(View.java:3039)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at android.view.View.performClick(View.java:3480)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at android.view.View$PerformClick.run(View.java:13983)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at android.os.Handler.handleCallback(Handler.java:605)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at android.os.Handler.dispatchMessage(Handler.java:92)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at android.os.Looper.loop(Looper.java:137)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at android.app.ActivityThread.main(ActivityThread.java:4340)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at java.lang.reflect.Method.invokeNative(Native Method)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at java.lang.reflect.Method.invoke(Method.java:511)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at dalvik.system.NativeStart.main(Native Method)
    10-21 04:53:08.712: E/AndroidRuntime(531): Caused by: java.lang.reflect.InvocationTargetException
    10-21 04:53:08.712: E/AndroidRuntime(531):  at java.lang.reflect.Method.invokeNative(Native Method)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at java.lang.reflect.Method.invoke(Method.java:511)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at android.view.View$1.onClick(View.java:3034)
    10-21 04:53:08.712: E/AndroidRuntime(531):  ... 11 more
    10-21 04:53:08.712: E/AndroidRuntime(531): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.oatmeal.recipebookapp/com.oatmeal.recipebookapp.NewRecipe}; have you declared this activity in your AndroidManifest.xml?
    10-21 04:53:08.712: E/AndroidRuntime(531):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at android.app.Activity.startActivityForResult(Activity.java:3190)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at android.app.Activity.startActivity(Activity.java:3297)
    10-21 04:53:08.712: E/AndroidRuntime(531):  at com.oatmeal.recipebookapp.MainActivity.showAddRecipe(MainActivity.java:57)

MainActivity.Java

package com.oatmeal.recipebookapp;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;



public class MainActivity extends ListActivity {

    Intent intent;
    TextView recipeId;

    DBTools dbTools = new DBTools(this);

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ArrayList<HashMap<String, String>> recipeList = dbTools.getAllRecipes();

        if(recipeList.size() != 0){
            ListView listView = getListView();
            listView.setOnItemClickListener(new OnItemClickListener() {


                public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 

                recipeId = (TextView) view.findViewById(R.id.recipeId);

                String recipeIdValue = recipeId.getText().toString();
                Intent theIntent = new Intent(getApplication(), EditRecipe.class);
                theIntent.putExtra("recipeId", recipeIdValue);
                startActivity(theIntent);
                }
                });
            ListAdapter adapter = new SimpleAdapter( MainActivity.this,recipeList,R.layout.recipe_entry, new String [] {"recipeId", "recipeName", "recipeNumber"}, new int[] {R.id.recipeId, R.id.recipeNumber, R.id.recipeName});

            setListAdapter(adapter);
            }

        }
    public void showAddRecipe(View view){
        Intent theIntent = new Intent(getApplication(), NewRecipe.class);
        startActivity(theIntent);
    }
    }

NewRecipe.java     包com.oatmeal.recipebookapp;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;




    public class NewRecipe extends Activity{



        EditText recipeName;
        EditText recipeNumber;

        DBTools dbTools = new DBTools(this);

        @Override
        public void onCreate(Bundle savedInstanceState) {



            super.onCreate(savedInstanceState);



            setContentView(R.layout.add_new_recipe);


            recipeName = (EditText) findViewById(R.id.recipeName);
            recipeNumber = (EditText) findViewById(R.id.recipeNumber);


        }
        public void addNewRecipe(View view) {



            HashMap<String, String> queryValuesMap =  new  HashMap<String, String>();



            queryValuesMap.put("recipeName", recipeName.getText().toString());
            queryValuesMap.put("recipeNumber", recipeNumber.getText().toString());



            dbTools.insertRecipe(queryValuesMap);



            this.callMainActivity(view);
        }
       public void callMainActivity(View view) {
            Intent theIntent = new Intent(getApplication(), MainActivity.class);
            startActivity(theIntent);
        }  
    }

activity_main.xml中

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         android:background="#000000"
         >

        <TextView
            android:id="@+id/recipeTitleTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/padding_5dp"
            android:text="@string/recipe_title"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#FFFFFF"
            android:layout_weight= "1" />

        <Button
            android:id="@+id/button1"
            android:background="#444444"
            android:onClick="showAddRecipe"
            android:text="@string/add_button"
            android:textSize="20sp"
            android:textColor="#FFFFFF" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight= "1" >
        </ListView>

    </TableRow>

</TableLayout>

add_new_recipe.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >



    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000000" >

            <TextView
                android:id="@+id/addRecipeTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/padding_5dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#FFFFFF"
                android:text="@string/add_recipe" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/recipeNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="@dimen/padding_5dp"
            android:text="@string/recipe_name" />

        <EditText
            android:id="@+id/recipeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textCapCharacters"
            android:padding="@dimen/padding_5dp" >

            <requestFocus />
        </EditText>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/recipeNumberTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="@dimen/padding_5dp"
            android:text="@string/recipe_number" />

        <EditText
            android:id="@+id/recipeNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number"
            android:padding="@dimen/padding_5dp" >

            <requestFocus />
        </EditText>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    </TableRow>

    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/addButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save_data"
            android:onClick="addNewRecipe"
            android:layout_weight="1" />

    </TableRow>

</TableLayout>

清单

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ArrayList<HashMap<String, String>> recipeList = dbTools.getAllRecipes();

        if(recipeList.size() != 0){
            ListView listView = getListView();
            listView.setOnItemClickListener(new OnItemClickListener() {


                public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 

                recipeId = (TextView) view.findViewById(R.id.recipeId);

                String recipeIdValue = recipeId.getText().toString();
                Intent theIntent = new Intent(getApplication(), EditRecipe.class);
                theIntent.putExtra("recipeId", recipeIdValue);
                startActivity(theIntent);
                }
                });
            ListAdapter adapter = new SimpleAdapter( MainActivity.this,recipeList,R.layout.recipe_entry, new String [] {"recipeId", "recipeName", "recipeNumber"}, new int[] {R.id.recipeId, R.id.recipeNumber, R.id.recipeName});

            setListAdapter(adapter);
            }

        }
    public void showAddRecipe(View view){
        Intent theIntent = new Intent(getApplication(), NewRecipe.class);
        startActivity(theIntent);
    }

    }

3 个答案:

答案 0 :(得分:0)

显示错误,因为您可能忘记在清单文件中声明活动NewRecipe

 <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >    
  <activity
            android:name=".NewRecipe"/>
   </application>

答案 1 :(得分:0)

像这样搜索与您的应用相关的错误

10-21 04:53:08.712: E/AndroidRuntime(531): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.oatmeal.recipebookapp/com.oatmeal.recipebookapp.NewRecipe}; have you declared this activity in your AndroidManifest.xml?

这表明您尚未在NewRecipe中宣布您的活动AndroidManifest.xml。这样做,你的应用程序应该工作。 像这样声明activity

<activity
        android:name=".NewRecipe"/>

如果需要,您可以添加intent-filters

答案 2 :(得分:-1)

你有没有在舱单中宣布你的活动? ActivityNotFoundException 已被抛出......