我试图在SQL中为3个EditTexts创建列。我认为所有的代码都很好,但每次我通过onClick运行SAVE时都会崩溃。我不知道logcat中的错误是什么意思。如果有人可以提供帮助,那将是最精彩的。我花了很长时间试图自己解决这个问题(超过一个小时)。
logcat的:
08-31 02:51:40.241: W/dalvikvm(867): threadid=1: thread exiting with uncaught exception (group=0x41465700)
08-31 02:51:40.311: E/AndroidRuntime(867): FATAL EXCEPTION: main
08-31 02:51:40.311: E/AndroidRuntime(867): java.lang.IllegalStateException: Could not execute method of the activity
08-31 02:51:40.311: E/AndroidRuntime(867): at android.view.View$1.onClick(View.java:3633)
08-31 02:51:40.311: E/AndroidRuntime(867): at android.view.View.performClick(View.java:4240)
08-31 02:51:40.311: E/AndroidRuntime(867): at android.view.View$PerformClick.run(View.java:17721)
08-31 02:51:40.311: E/AndroidRuntime(867): at android.os.Handler.handleCallback(Handler.java:730)
08-31 02:51:40.311: E/AndroidRuntime(867): at android.os.Handler.dispatchMessage(Handler.java:92)
08-31 02:51:40.311: E/AndroidRuntime(867): at android.os.Looper.loop(Looper.java:137)
08-31 02:51:40.311: E/AndroidRuntime(867): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-31 02:51:40.311: E/AndroidRuntime(867): at java.lang.reflect.Method.invokeNative(Native Method)
08-31 02:51:40.311: E/AndroidRuntime(867): at java.lang.reflect.Method.invoke(Method.java:525)
08-31 02:51:40.311: E/AndroidRuntime(867): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-31 02:51:40.311: E/AndroidRuntime(867): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-31 02:51:40.311: E/AndroidRuntime(867): at dalvik.system.NativeStart.main(Native Method)
08-31 02:51:40.311: E/AndroidRuntime(867): Caused by: java.lang.reflect.InvocationTargetException
08-31 02:51:40.311: E/AndroidRuntime(867): at java.lang.reflect.Method.invokeNative(Native Method)
08-31 02:51:40.311: E/AndroidRuntime(867): at java.lang.reflect.Method.invoke(Method.java:525)
08-31 02:51:40.311: E/AndroidRuntime(867): at android.view.View$1.onClick(View.java:3628)
08-31 02:51:40.311: E/AndroidRuntime(867): ... 11 more
08-31 02:51:40.311: E/AndroidRuntime(867): Caused by: java.lang.NullPointerException
08-31 02:51:40.311: E/AndroidRuntime(867): at com.mikitz.rogsimple.DamageTracker.save(DamageTracker.java:41)
08-31 02:51:40.311: E/AndroidRuntime(867): ... 14 more
MainActivity.java:
package com.mikitz.rogsimple;
import java.util.Random;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
Random rndNumbers = new Random();
int d20 = rndNumbers.nextInt(20) +1;
Button roll;
TextView display;
ROGDatabaseAdapter ROGHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ROGHelper=new ROGDatabaseAdapter(this);
roll = (Button) findViewById(R.id.d20);
display = (TextView) findViewById(R.id.display);
roll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
d20 = rndNumbers.nextInt(20) +1;
display.setText(" " + d20);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void damagetrackerpage (View view)
{
Intent intent=new Intent (this, DamageTracker.class);
startActivity(intent);
}
public void charactersheetpage (View view)
{
Intent intent=new Intent (this, CharacterProfile.class);
startActivity(intent);
}
@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;
}
}
}
ROGDatabaseAdapter.java:
package com.mikitz.rogsimple;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class ROGDatabaseAdapter {
ROGHelper helper;
public ROGDatabaseAdapter(Context context)
{
helper=new ROGHelper(context);
}
public long insertData(String armorhealthchest, String healthchest, String abvchest)
{
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(ROGHelper.AHChest, armorhealthchest);
contentValues.put(ROGHelper.HChest, healthchest);
contentValues.put(ROGHelper.ABVChest, abvchest);
long id=db.insert(ROGHelper.TABLE_NAME, null, contentValues);
return id;
}
static class ROGHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="rogdatabase";
private static final String TABLE_NAME="ARMORSTATS";
private static final int DATABASE_VERSION=10;
private static final String UID="_id";
private static final String AHChest="Armor Health";
private static final String HChest="Health";
private static final String ABVChest="Absorb Value";
private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+HChest+" VARCHAR(255), "+ABVChest+" VARCHAR(255), "+AHChest+" VARCHAR(255));";
private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME;
private Context context;
public ROGHelper (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
Message.message(context, "constructor called");
}
@Override
public void onCreate(SQLiteDatabase db) {
// CREATE TABLE ARMORSTATS (_id INTEGER PRIMARY KEY AUTOINCREMENT, Name VARCHAR(255));
try {
db.execSQL(CREATE_TABLE);
Message.message(context, "onCreate called");
} catch (android.database.SQLException e) {
// TODO Auto-generated catch block
Message.message(context, ""+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVerison, int newVerison) {
try {
Message.message(context, "onUpgrade called");
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (android.database.SQLException e) {
// TODO Auto-generated catch block
Message.message(context, ""+e);
}
}
}
}
DamageTracker.java:
package com.mikitz.rogsimple;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class DamageTracker extends Activity {
EditText armorhealthchest, healthchest, abvchest;
// EditText headdi, armorhealthhead, healthheaad, abvhead;
// EditText rightarmdi, armorhealthrightarm, healthrightarm, abvrightarm;
// EditText leftarmdi, armorhealthleftarm, healthleftarm, abvleftarm;
// EditText rightlegdi, armorhealthrightleg, healthrightleg, abvrightleg;
// EditText leftlegdi, armorhealthleftleg, healthleftleg, abvleftleg;
ROGDatabaseAdapter rogHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.damage_profile);
armorhealthchest=(EditText) findViewById(R.id.armorhealthchest);
healthchest=(EditText) findViewById(R.id.healthchest);
abvchest=(EditText) findViewById(R.id.abvchest);
// headdi=(EditText) findViewById(R.id.headdi);
// rightarmdi=(EditText) findViewById(R.id.rightarmdi);
// leftarmdi=(EditText) findViewById(R.id.leftarmdi);
// rightlegdi=(EditText) findViewById(R.id.rightlegdi);
// leftlegdi=(EditText) findViewById(R.id.leftlegdi);
}
public void save(View view)
{
String armorhealthcheststr=armorhealthchest.getText().toString();
String healthcheststr=healthchest.getText().toString();
String abvcheststr=abvchest.getText().toString();
long id=rogHelper.insertData(armorhealthcheststr, healthcheststr, abvcheststr);
if(id<0)
{
Message.message(this, "Unsuccesful");
}
else
{
Message.message(this, "Successfully inserted a row");
// Intent intent=new Intent (this, MainActivity.class);
// startActivity(intent);
}
}
}
答案 0 :(得分:1)
rogHelper
中的DamageTracker.java
似乎在此行为空:
long id=rogHelper.insertData(armorhealthcheststr, healthcheststr, abvcheststr);
您需要先将其初始化,最好是onCreate
。