0

嗨,我是 Android 开发的新手。我创建了这个活动,似乎每次我点击这个活动中的一个按钮。我不断收到 Activity LogCat 错误的无法执行方法。

这是我的活动 Java 文件

public class Add_Edit_Cows extends Activity  {

    Button cradd, credit, crdelete, crview;
EditText cowids, sire, dam, months, dates, years;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add__edit__cows);
    cradd = (Button) findViewById(R.id.addcow);
    cowids = (EditText) findViewById(R.id.cowid);
    sire = (EditText) findViewById(R.id.sire);
    dam = (EditText) findViewById(R.id.dam);
    months = (EditText) findViewById(R.id.DOBM);
    dates = (EditText) findViewById(R.id.DOBd);
    years = (EditText) findViewById(R.id.DOBy);


    credit = (Button) findViewById(R.id.editcow);
    crdelete = (Button) findViewById(R.id.removecow);
    crview = (Button) findViewById(R.id.viewcows);
}
public void addcow (View view) {
    Cowsdatabase cowsdb = new Cowsdatabase(this, null, null, 1);

    int Cown = Integer.parseInt(cowids.getText().toString());
    String Sire = sire.getText().toString();
    String Dam = dam.getText().toString();
    int Month = Integer.parseInt(months.getText().toString());
    int Date = Integer.parseInt(dates.getText().toString());
    int Year = Integer.parseInt(years.getText().toString());

    cow Cow = new cow(Integer.parseInt(cowids.getText().toString()), Sire, Dam, Month, Date, Year);
    cowsdb.addcow(Cow);
    cowids.setText("");
    sire.setText("");
    dam.setText("");
    months.setText("");
    dates.setText("");
    years.setText("");
}

public void editcow(View view) {
    Cowsdatabase cowsdb = new Cowsdatabase(this, null, null, 1);

    cow Cow =
            cowsdb.findcow(Integer.parseInt(cowids.getText().toString()));
    if (Cow != null) {
        sire.setText(String.valueOf(Cow.getSire()));
        dam.setText(String.valueOf(Cow.getDam()));
        months.setText(String.valueOf(Cow.getMonth()));
        dates.setText(String.valueOf(Cow.getDate()));
        years.setText(String.valueOf(Cow.getYear()));
    } else {
        cowids.setText("No Match Found");
    }
    }
public void dcow (View view) {
    Cowsdatabase cowsdb = new Cowsdatabase(this, null, null, 1);
    boolean result = cowsdb.deleteCow(Integer.parseInt(cowids.getText().toString()));
    if (result)
    {
        cowids.setText("Record Deleted");
        sire.setText("");
        dam.setText("");
        months.setText("");
        dates.setText("");
        years.setText("");
    }
    else
        cowids.setText("No Match Found");
}
public void viewcow (View view){
startActivity(new Intent("Ag.Access.Viewcows"));
}
}

LogCat 错误消息:

10-15 20:02:51.735: E/AndroidRuntime(7738): FATAL EXCEPTION: main
10-15 20:02:51.735: E/AndroidRuntime(7738): java.lang.IllegalStateException: Could not execute method of the activity
10-15 20:02:51.735: E/AndroidRuntime(7738):     at android.view.View$1.onClick(View.java:3591)
10-15 20:02:51.735: E/AndroidRuntime(7738):     at android.view.View.performClick(View.java:4084)
10-15 20:02:51.735: E/AndroidRuntime(7738):     at android.view.View$PerformClick.run(View.java:16966)
10-15 20:02:51.735: E/AndroidRuntime(7738):     at android.os.Handler.handleCallback(Handler.java:615)
10-15 20:02:51.735: E/AndroidRuntime(7738):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 20:02:51.735: E/AndroidRuntime(7738):     at android.os.Looper.loop(Looper.java:137)
10-15 20:02:51.735: E/AndroidRuntime(7738):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-15 20:02:51.735: E/AndroidRuntime(7738):     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 20:02:51.735: E/AndroidRuntime(7738):     at java.lang.reflect.Method.invoke(Method.java:511)
10-15 20:02:51.735: E/AndroidRuntime(7738):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-15 20:02:51.735: E/AndroidRuntime(7738):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-15 20:02:51.735: E/AndroidRuntime(7738):     at dalvik.system.NativeStart.main(Native Method)
10-15 20:02:51.735: E/AndroidRuntime(7738): Caused by: java.lang.reflect.InvocationTargetException
10-15 20:02:51.735: E/AndroidRuntime(7738):     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 20:02:51.735: E/AndroidRuntime(7738):     at java.lang.reflect.Method.invoke(Method.java:511)
10-15 20:02:51.735: E/AndroidRuntime(7738):     at android.view.View$1.onClick(View.java:3586)
10-15 20:02:51.735: E/AndroidRuntime(7738):     ... 11 more
10-15 20:02:51.735: E/AndroidRuntime(7738): Caused by: java.lang.NullPointerException
10-15 20:02:51.735: E/AndroidRuntime(7738):     at ag.access.cowsdb.Cowsdatabase.addcow(Cowsdatabase.java:64)
10-15 20:02:51.735: E/AndroidRuntime(7738):     at ag.access.Add_Edit_Cows.addcow(Add_Edit_Cows.java:48)
10-15 20:02:51.735: E/AndroidRuntime(7738):     ... 14 more

我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ag.access"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name`"`enter code here`
    android:theme="@style/AppTheme" >
    <activity
        android:name="ag.access.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="ag.access.Farming"
        android:label="@string/title_activity_farming"
        android:parentActivityName="ag.access.MainActivity" >
        <intent-filter>
            <action android:name="ag.access.Farming" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="ag.access.MainActivity" />
    </activity>
    <activity
        android:name="ag.access.Dairy"
        android:label="@string/title_activity_dairy"
        android:parentActivityName="ag.access.MainActivity" >
        <intent-filter>
            <action android:name="ag.access.Dairy" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="ag.access.MainActivity" />
    </activity>
    <activity
        android:name="ag.access.Daily_log"
        android:label="@string/title_activity_daily_log"
        android:parentActivityName="ag.access.Dairy" >
        <intent-filter>
            <action android:name="ag.access.Daily_log" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="ag.access.Dairy" />
    </activity>
    <activity
        android:name="ag.access.Viewlogs"
        android:label="@string/title_activity_viewlogs"
        android:parentActivityName="ag.access.Daily_log" >
        <intent-filter>
            <action android:name="ag.access.Viewlogs" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="ag.access.Daily_log" />
    </activity>
    <activity
        android:name="ag.access.Add_Edit_Cows"
        android:label="@string/title_activity_add__edit__cows"
        android:parentActivityName="ag.access.Dairy" >
        <intent-filter>
            <action android:name="ag.access.Add_Edit_Cows" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="ag.access.Dairy" />
     </activity>
     <activity
        android:name="ag.access.Viewcows"
        android:label="@string/title_activity_viewcows"
        android:parentActivityName="ag.access.Add_Edit_Cows" >
        <intent-filter>
            <action android:name="ag.access.Viewcows" /
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="ag.access.Add_Edit_Cows" />
    </activity>
 </application>
 </manifest>

我的 Add_Edit_Cows 活动

<ScrollView
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".Add_Edit_Cows" >
    <TextView
    android:id="@+id/textView1"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
    android:text="Add Edit Delete Cows"
    android:textAppearance="?android:attr/textAppearanceLarge" />
    <EditText
    android:id="@+id/cowid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:hint="@string/cowid" />     
    <EditText
    android:id="@+id/sire"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:hint="@string/sire"/>
    <EditText
    android:id="@+id/dam"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:hint="@string/dam"/>
    <EditText 
    android:id="@+id/DOBM"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:hint="@string/DOBM"/>
    <EditText 
    android:id="@+id/DOBd"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:hint="@string/DOBD"/>
    <EditText 
    android:id="@+id/DOBy"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:hint="@string/DOBY"/>
    <Button
    android:id="@+id/addcow"
    android:layout_width="261dp"
    android:layout_height="wrap_content"
    android:onClick="addcow"
    android:text="@string/baddcow" />
    <Button
    android:id="@+id/editcow"
    android:layout_width="261dp"
    android:layout_height="wrap_content"
    android:onClick="editcow"
    android:text="@string/editcow" />  
    <Button
    android:id="@+id/removecow"
    android:layout_width="261dp"
    android:layout_height="wrap_content"
    android:onClick="dcow"
    android:text="@string/deletecow" />  
    <Button
    android:id="@+id/viewcows"
    android:layout_width="261dp"
    android:layout_height="wrap_content"
    android:onClick="viewcow"
    android:text="@string/viewc" />  

 </LinearLayout>
 </ScrollView>

牛数据库.java

 package ag.access.cowsdb;

 import ag.access.cowsdb.provider.Cows_Provider;
 import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteDatabase.CursorFactory;
 import android.database.sqlite.SQLiteOpenHelper;

 public class Cowsdatabase extends SQLiteOpenHelper {

private ContentResolver cowcr;

public Cowsdatabase(Context context, String name, CursorFactory factory,
        int version) {
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
}

public static final String Key_id = "_id";
public static final String Key_Cow = "Cowid";
public static final String Key_Sire = "Sire";
public static final String Key_Dam = "Dam";
public static final String Key_DOBM = "Month";
public static final String Key_DOBD = "Date";
public static final String Key_DOBY = "Year";

private static final String DATABASE_NAME = "CowsDB";
public static final String DATABASE_TABLE = "Cows";

private static final int DATABASE_VERSION = 1;

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String Create_Cows_Table = "CREATE TABLE" + 
    DATABASE_TABLE + "(" +
    Key_id + "INTEGER PRIMARY KEY AUTOINCREMENT," +
    Key_Cow + "INTEGER NOT NULL" +
    Key_Sire + "TEXT NOT NULL," +
    Key_Dam  + "TEXT NOT NULL," +
    Key_DOBM + "INTEGER NOT NULL," +
    Key_DOBD + "INTEGER NOT NULL," +
    Key_DOBY + "INTEGER NOT NULL," + ")";
    db.execSQL(Create_Cows_Table);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
}
    public void addcow(cow Cow) {
ContentValues values = new ContentValues();
values.put(Key_Cow, Cow.getCowid());
values.put(Key_Sire, Cow.getSire());
values.put(Key_Dam, Cow.getDam());
values.put(Key_DOBM, Cow.getMonth());
values.put(Key_DOBD, Cow.getDate());
values.put(Key_DOBY, Cow.getYear());

cowcr.insert(Cows_Provider.Content_Uri, values);
 }
 public cow findcow(int cowid) {
String[] projection = {Key_id, Key_Cow, Key_Sire, Key_Dam, Key_DOBM, Key_DOBD, Key_DOBY};

String selection = "cowid = \"" + cowid + "\"";
Cursor cursor = cowcr.query(Cows_Provider.Content_Uri, projection, selection, null, null);

cow cow = new cow();

if (cursor.moveToFirst()) {
    cursor.moveToFirst();
    cow.Setid(Integer.parseInt(cursor.getString(0)));
    cow.SetCowid(Integer.parseInt(cursor.getString(1)));
    cow.setSire(cursor.getString(2));
    cow.setDam(cursor.getString(3));
    cow.setMonth(Integer.parseInt(cursor.getString(4)));
    cow.setDate(Integer.parseInt(cursor.getString(5)));
    cow.setYear(Integer.parseInt(cursor.getString(6)));
    cursor.close();
} else {
    cow = null;
}
return cow;
 }
public boolean deleteCow (int cowid) {
    boolean result = false;

    String selection = "cowid = \"" + cowid + "\"";

    int rowsDeleted = cowcr.delete(Cows_Provider.Content_Uri, selection, null);

    if (rowsDeleted > 0)
        result = true;

    return result;
}
 }

对于 cradd 按钮,我希望它将来自 edittexts 的值添​​加到我的数据库中,然后由我的内容提供者接收对于 credit 按钮,我希望能够编辑数据库中的奶牛。对于 crdelete 按钮,我希望能够删除该数据库条目。对于 Crview,我希望它能够进行另一个活动,以便能够查看内容提供者。

就像我之前所说的那样,所有按钮都出现“无法执行活动的方法”LOGCAT 错误。

您的帮助将不胜感激。

谢谢

4

2 回答 2

1

您忘记为视图对象注册 onlick 事件。setOnClickListener(这个)

于 2013-10-16T03:28:47.647 回答
1

错误来自Cowsdatabase.java第 64 行。

我想它在这个街区的某个地方:

int Cown = Integer.parseInt(cowids.getText().toString());
String Sire = sire.getText().toString();
String Dam = dam.getText().toString();
int Month = Integer.parseInt(months.getText().toString());
int Date = Integer.parseInt(dates.getText().toString());
int Year = Integer.parseInt(years.getText().toString());

因为它是NullPointerException,我们可以假设其中一个variable.getText()执行返回null。如果这是真的,以下toString()将触发异常。

使用调试器会使这一点非常清楚。

于 2013-10-16T03:30:37.683 回答