java需要noob帮助理解如何调试
请原谅我的混乱,我不熟悉在论坛上发布错误报告和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 安卓.view.View$1.onClick(View.java:3039)
10-21 04:53:08.712: E/AndroidRuntime(531): at 安卓.view.View.performClick(View.java:3480)
10-21 04:53:08.712: E/AndroidRuntime(531): at 安卓.view.View$PerformClick.run(View.java:13983)
10-21 04:53:08.712: E/AndroidRuntime(531): at 安卓.os.Handler.handleCallback(Handler.java:605)
10-21 04:53:08.712: E/AndroidRuntime(531): at 安卓.os.Handler.dispatchMessage(Handler.java:92)
10-21 04:53:08.712: E/AndroidRuntime(531): at 安卓.os.Looper.loop(Looper.java:137)
10-21 04:53:08.712: E/AndroidRuntime(531): at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-21 04:53:08.712: E/AndroidRuntime(531): at com.安卓.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 安卓.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: 安卓.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 安卓.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
10-21 04:53:08.712: E/AndroidRuntime(531): at 安卓.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
10-21 04:53:08.712: E/AndroidRuntime(531): at 安卓.app.Activity.startActivityForResult(Activity.java:3190)
10-21 04:53:08.712: E/AndroidRuntime(531): at 安卓.app.Activity.startActivity(Activity.java:3297)
10-21 04:53:08.712: E/AndroidRuntime(531): at com.oatmeal.recipebookapp.MainActivity.showAddRecipe(MainActivity.java:57)
主要活动。爪哇
package com.oatmeal.recipebookapp;
import java.util.ArrayList;
import java.util.HashMap;
import 安卓.app.ListActivity;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.AdapterView;
import 安卓.widget.AdapterView.OnItemClickListener;
import 安卓.widget.ListAdapter;
import 安卓.widget.ListView;
import 安卓.widget.SimpleAdapter;
import 安卓.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);
}
}
新配方。JAVA 包com。燕麦粥recipebookapp
import java.util.HashMap;
import 安卓.app.Activity;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.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);
}
}
主要活动。xml
<TableLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".MainActivity" >
<TableRow
安卓:id="@+id/tableRow1"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:background="#000000"
>
<TextView
安卓:id="@+id/recipeTitleTextView"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginLeft="@dimen/padding_5dp"
安卓:text="@string/recipe_title"
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:textColor="#FFFFFF"
安卓:layout_weight= "1" />
<Button
安卓:id="@+id/button1"
安卓:background="#444444"
安卓:onClick="showAddRecipe"
安卓:text="@string/add_button"
安卓:textSize="20sp"
安卓:textColor="#FFFFFF" />
</TableRow>
<TableRow
安卓:id="@+id/tableRow2"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content" >
<ListView
安卓:id="@安卓:id/list"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_weight= "1" >
</ListView>
</TableRow>
</TableLayout>
添加新配方。xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" >
<TableRow
安卓:id="@+id/tableRow1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:background="#000000" >
<TextView
安卓:id="@+id/addRecipeTextView"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:padding="@dimen/padding_5dp"
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:textColor="#FFFFFF"
安卓:text="@string/add_recipe" />
</TableRow>
<TableRow
安卓:id="@+id/tableRow2"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content" >
<TextView
安卓:id="@+id/recipeNameTextView"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:padding="@dimen/padding_5dp"
安卓:text="@string/recipe_name" />
<EditText
安卓:id="@+id/recipeName"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:ems="10"
安卓:inputType="textCapCharacters"
安卓:padding="@dimen/padding_5dp" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
安卓:id="@+id/tableRow3"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content" >
<TextView
安卓:id="@+id/recipeNumberTextView"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:padding="@dimen/padding_5dp"
安卓:text="@string/recipe_number" />
<EditText
安卓:id="@+id/recipeNumber"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:ems="10"
安卓:inputType="number"
安卓:padding="@dimen/padding_5dp" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
安卓:id="@+id/tableRow4"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content" >
</TableRow>
<TableRow
安卓:id="@+id/tableRow5"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content" >
<Button
安卓:id="@+id/addButton"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/save_data"
安卓:onClick="addNewRecipe"
安卓: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);
}
}
# 1 楼答案
搜索与您的应用程序相关的错误,如下所示
这表明您尚未在
AndroidManifest.xml
中声明活动NewRecipe
。这样做,你的应用程序应该可以工作。 像这样声明activity
如果需要,可以添加
intent-filters
# 2 楼答案
您是否在清单中声明了您的活动 已引发ActivityNotFoundException
# 3 楼答案
它显示错误,因为您可能忘记在清单文件中声明活动
NewRecipe