简体   繁体   中英

Unfortunately, “App Name” has stopped. (Android Emulator, Eclipse)

when i try to launch my app everything work fine but when i open a class from the menu, it does not open. im new to android programming. Help me please..thank you

keep getting this message

Unfortunately, (App Name) has stopped."

Here is my java

package com.e_attendance;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ManageClass extends Activity implements OnClickListener
{

    EditText editClassno;
    Button btnAdd, btnDelete, btnModify, btnViewAll;
    SQLiteDatabase db;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.classdetail);

        editClassno = (EditText) findViewById(R.id.editClassno);
        btnAdd.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnModify.setOnClickListener(this);
        btnViewAll.setOnClickListener(this);

        db = openOrCreateDatabase("ClassDB", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS class (classno VARCHAR);");

    }

    @Override
    public void onClick(View view) {

        if (view == btnAdd) {

            if (editClassno.getText().toString().trim().length() == 0)

            {

                showMessage("Error", "Enter a Class No please");
                return;
            }

            db.execSQL("INSERT INTO class VALUES('" + editClassno.getText()
                    + "');");
            showMessage("Success", "Record added");
            clearText();
        }

        if (view == btnDelete) {
            if (editClassno.getText().toString().trim().length() == 0) {
                showMessage("Error", "Please enter Classno");
                return;
            }
            Cursor c = db.rawQuery("SELECT * FROM class WHERE classno='"
                    + editClassno.getText() + "'", null);
            if (c.moveToFirst()) {
                db.execSQL("DELETE FROM class WHERE classno='"
                        + editClassno.getText() + "'");
                showMessage("Success", "Record Deleted");
            } else {
                showMessage("Error", "Invalid Classno");
            }
            clearText();
        }

        if (view == btnModify) {
            if (editClassno.getText().toString().trim().length() == 0) {
                showMessage("Error", "Please enter Classno");
                return;
            }
            Cursor c = db.rawQuery("SELECT * FROM class WHERE classno='"
                    + editClassno.getText() + "'", null);

            if (c.moveToFirst()) {
                db.execSQL("UPDATE class SET class WHERE classno='"
                        + editClassno.getText() + "'");
                showMessage("Success", "Record Modified");

            } else {
                showMessage("Error", "Invalid Clasno");
            }
            clearText();
        }

        if (view == btnViewAll) {
            Cursor c = db.rawQuery("SELECT * FROM class", null);
            if (c.getCount() == 0) {
                showMessage("Error", "No records found");
                return;
            }
            StringBuffer buffer = new StringBuffer();
            while (c.moveToNext()) {
                buffer.append("Classno: " + c.getString(0) + "\n");
                ;
            }
            showMessage("Classes No.", buffer.toString());
        }

    }

    public void clearText() {
        editClassno.setText("");

        editClassno.requestFocus();
    }

    public void showMessage(String title, String message) {
        Builder builder = new Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }
}

my xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myLayout2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="0" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="110dp"
        android:layout_y="10dp"
        android:text="Class Section" />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_x="30dp"
        android:layout_y="200dp"
        android:text="@string/add" />

    <Button
        android:id="@+id/btnDelete"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_x="150dp"
        android:layout_y="200dp"
        android:text="@string/delete" />

    <Button
        android:id="@+id/btnModify"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_x="30dp"
        android:layout_y="250dp"
        android:text="@string/modify" />

    <Button
        android:id="@+id/btnViewAll"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_x="30dp"
        android:layout_y="300dp"
        android:text="@string/view_all" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="30dp"
        android:layout_y="102dp"
        android:text="Class_no" />

    <EditText
        android:id="@+id/editClassno"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_x="120dp"
        android:layout_y="93dp"
        android:ems="10"
        android:inputType="number" />

</AbsoluteLayout>

android manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.e_attendance"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
- 
        <activity
            android:name="com.e_attendance.HomeActivity"
            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=".SignUPActivity" />
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.e_attendance.STARTINGPOINT" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.e_attendance.MENU" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".OpenNotes"
            android:label="@string/app_name" >
        </activity>
        <activity android:name=".NewText" >
        </activity>

        <activity
            android:name=".ManageClass"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name=".ManageStudents"
            android:label="@string/app_name" >
        </activity>

    </application>

</manifest>

You haven't find IDs for your all Buttons on onCreate() method. So just do it.

  Button btnAdd, btnDelete, btnModify, btnViewAll;

  btnAdd = (Button) findViewById(R.id.btnAdd);
  btnDelete = (Button) findViewById(R.id.btnDelete);
  btnModify = (Button) findViewById(R.id.btnModify);
  btnViewAll = (Button) findViewById(R.id.btnViewAll);

Try this way,hope this will help you to solve your problem.

  @Override
  public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.classdetail);

        editClassno = (EditText) findViewById(R.id.editClassno);
        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnDelete = (Button) findViewById(R.id.btnDelete);
        btnModify = (Button) findViewById(R.id.btnModify);
        btnViewAll = (Button) findViewById(R.id.btnViewAll);
        btnAdd.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnModify.setOnClickListener(this);
        btnViewAll.setOnClickListener(this);

        db = openOrCreateDatabase("ClassDB", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS class (classno VARCHAR);");
   }

You need to create the bridge from Java to XML using the R class as follows:

 @Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.classdetail);

    editClassno = (EditText) findViewById(R.id.editClassno);
    btnAdd = (Button)findViewById(R.id.btnAdd);
    btnDelete= (Button)findViewById(R.id.btnDelete);
    btnModify= (Button)findViewById(R.id.btnModify);
    btnViewAll= (Button)findViewById(R.id.btnViewAll);
    btnAdd.setOnClickListener(this);
    btnDelete.setOnClickListener(this);
    btnModify.setOnClickListener(this);
    btnViewAll.setOnClickListener(this);

    db = openOrCreateDatabase("ClassDB", Context.MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS class (classno VARCHAR);");

Hope this works fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM