2015-08-16 59 views
-1

我是新的android和我只是嘗試使用此代碼並顯示數據庫中的保存數據在listView中。但是當我按下按鈕說不幸已經停止。請幫幫我。「無法找到明確的活動」錯誤

MainActivity類別

package com.example.employeedetailsnew; 

import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.*; 

public class MainActivity extends Activity{ 
private EditText nme = null; 
private EditText num = null; 
private EmployeeDatabase empObj; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    nme = (EditText) findViewById(R.id.name); 
    num = (EditText) findViewById(R.id.ageemp); 
    Button btnShowData = (Button) findViewById(R.id.button2); 
    btnShowData.setOnClickListener(new Button.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      onSaveClick(); 
     } 
    }); 
} 
public void onButtonClicked(View view) { 
    if (nme.length()!=0 && num.length()!=0){ 
    Calendar c = Calendar.getInstance(); 
    SimpleDateFormat sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a"); 
    String strDate = sdf.format(c.getTime()); 
    String varName = nme.getText().toString(); 
    String varAge = num.getText().toString(); 
    empObj = new EmployeeDatabase(getApplicationContext()); 
    empObj.insert(varName,varAge,strDate); 
    AlertDialog alertDialog = null; 
    alertDialog = new AlertDialog.Builder(MainActivity.this).create(); 
    alertDialog.setTitle(getResources().getString(R.string.Message)); 
       alertDialog.setMessage(getResources().getString(R.string.You_have_been_Registered)); 
    alertDialog.setCancelable(true); 
    alertDialog.show(); 
    nme.setText(""); 
    num.setText(""); 
    }else{ 
     Toast toast = Toast.makeText(getApplicationContext(), getResources().getString(R.string.Empty_Item), 
       Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 
public void onSaveClick(){ 

    Intent intent = new Intent(this, ShowData.class); 
    startActivity(intent); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

} 

詳細類

package com.example.employeedetailsnew; 

public class DetailClass { 
private String name; 
private String age; 
private String time; 

public String getName() { 
    return name; 
} 

public String getAge() { 
    return age; 
} 

public String getTime() { 
    return time; 
} 
} 

MyAdaptor類

package com.example.employeedetailsnew; 

import android.content.Context; 
import android.database.Cursor; 
import android.support.v4.widget.CursorAdapter; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import java.util.ArrayList; 

public class MyAdapter extends CursorAdapter { 
public MyAdapter(Context context, Cursor c) { 
    super(context, c); 
} 

@Override 
public int getCount() { 
    return 0; 
} 

@Override 
public Object getItem(int position) { 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

@Override 
public View getView(int index, View view, ViewGroup parent) { 
    if (view == null) 
    { 
     LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
     view = inflater.inflate(R.layout.shows_view, parent, false); 
    } 

    ArrayList times = null; 
    DetailClass empDtl = (DetailClass) times.get(index); 

    TextView nameTextView = (TextView) view.findViewById(R.id.name1); 
    nameTextView.setText(empDtl.getName()); 


    TextView ageTextView = (TextView) view.findViewById(R.id.age2); 
    ageTextView.setText(empDtl.getAge()); 


    TextView timeTextView = (TextView) view.findViewById(R.id.time3); 
    timeTextView.setText((CharSequence) empDtl.getTime()); 
    return view; 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    TextView nameTextView = (TextView) view.findViewById(R.id.name1); 
    nameTextView.setText(cursor.getString(cursor.getColumnIndex("name"))); 

    TextView ageTextView = (TextView) view.findViewById(R.id.name1); 
    ageTextView.setText(cursor.getString(cursor.getColumnIndex("age"))); 

    TextView timeTextView = (TextView) view.findViewById(R.id.name1); 
    timeTextView.setText(cursor.getString(cursor.getColumnIndex("time"))); 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    LayoutInflater inflater = LayoutInflater.from (parent.getContext()); 
    View view = inflater.inflate(R.layout.shows_list, parent, false); 
    return view; 
} 

} 

MYDATABASE類

package com.example.employeedetailsnew; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

class EmployeeDatabase extends SQLiteOpenHelper{ 

EmployeeDatabase (Context context) { 
    super(context, "empdb.db", null,3); 
} 

@Override 
public void onCreate(SQLiteDatabase database) 
{ 
    database.execSQL("create table employee" + "(name TEXT , age TEXT, time TEXT)"); 
} 
@Override 
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) 
{ 
    database.execSQL("DROP TABLE IF EXISTS EMPLOYEEDETAILNEW"); 
    onCreate(database); 
} 

public Cursor getDetails() 
{ 
     SQLiteDatabase db = getReadableDatabase(); 
     return db.rawQuery("select rowid _id,name, age,time from employeedetailnew", null); 

} 
public void insert(String name, String age, String time) 
{ 
    long rowId = 0; 
    try{ 
     SQLiteDatabase db = getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put("name", name); 
     contentValues.put("age", age); 
     contentValues.put("time", time); 
     rowId = db.insert("employeedetailnew", null, contentValues); 


    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
    finally{ 
     System.out.println("The rowId is "+rowId); 
     System.out.println("Name is "+name); 
     System.out.println("Age is "+age); 
     System.out.println("Time is "+time); 
    } 
} 
public boolean deleteTitle(String name) 
{ 
    SQLiteDatabase db = getWritableDatabase(); 
    return db.delete("employeedetailnew", name + "=" + name, null) > 0; 
} 
} 

ShowData類

package com.example.employeedetailsnew; 


import android.app.ListActivity; 
import android.database.Cursor; 
import android.os.Bundle; 

public class ShowData extends ListActivity 


{ 
private EmployeeDatabase databaseHelper; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.shows_list); 


    EmployeeDatabase empClick = new EmployeeDatabase(getApplicationContext()); 
    Cursor cursor = empClick.getDetails(); 
    if (cursor.moveToFirst()) 
    { 
     do 
     { 
      String name = cursor.getString(1); 
      String notes = cursor.getString(2); 
      String time = cursor.getString(3); 
     } while (cursor.moveToNext()); 
    } 
    if (!cursor.isClosed()) 
    { 
     cursor.close(); 
    } 

} 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 

android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.employeedetailsnew.MainActivity$PlaceholderFragment" > 

<EditText 
    android:id="@+id/name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/empname" 
    android:layout_below="@+id/empname" 
    android:layout_marginTop="19dp" 
    android:ems="10" 
    android:inputType="textPersonName" > 

    <requestFocus /> 
</EditText> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/sub" 
    android:layout_below="@+id/sub" 
    android:layout_marginTop="24dp" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_below="@+id/textView1" /> 

<TextView 
    android:id="@+id/empname" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="46dp" 
    android:layout_marginTop="27dp" 

    android:text="@string/name" /> 

<TextView 
    android:id="@+id/age" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/name" 
    android:layout_below="@+id/name" 
    android:layout_marginTop="16dp" 

    android:text="@string/age" /> 

<EditText 
    android:id="@+id/ageemp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/age" 
    android:layout_below="@+id/age" 
    android:ems="10" 
    android:inputType="numberSigned" /> 

<Button 
    android:id="@+id/sub" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/button3" 
    android:layout_centerVertical="true" 
    android:onClick="onButtonClicked" 
    android:text="@string/sub" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:text="@string/show_data" /> 


</RelativeLayout> 

shows_list.xml

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

<ListView 
    android:id="@+id/list_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 

</LinearLayout> 

shows_view.xml

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

<TextView 
    android:id="@+id/name1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

<TextView 
    android:id="@+id/age2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

<TextView 
    android:id="@+id/time3" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

</LinearLayout> 

,這就是我的logcat的

08-16 11:52:08.427 28242-28242/com.example.employeedetailsnew  E/AndroidRuntime﹕ FATAL EXCEPTION: main 
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.employeedetailsnew/com.example.employeedetailsnew.ShowData}; have you declared this activity in your AndroidManifest.xml? 
     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628) 
     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424) 
     at android.app.Activity.startActivityForResult(Activity.java:3390) 
     at android.app.Activity.startActivityForResult(Activity.java:3351) 
     at android.app.Activity.startActivity(Activity.java:3587) 
     at android.app.Activity.startActivity(Activity.java:3555) 
     at com.example.employeedetailsnew.MainActivity.onSaveClick(MainActivity.java:60) 
     at com.example.employeedetailsnew.MainActivity$1.onClick(MainActivity.java:30) 
     at android.view.View.performClick(View.java:4421) 
     at android.view.View$PerformClick.run(View.java:17903) 
     at android.os.Handler.handleCallback(Handler.java:730) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:213) 
     at android.app.ActivityThread.main(ActivityThread.java:5225) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:525) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 
     at dalvik.system.NativeStart.main(Native Method) 
+1

添加ShowData活動請剝離下來,以再現你的問題的代碼mimimal量... https://stackoverflow.com/help/how-to-ask https://stackoverflow.com/help/mcve –

+0

你爲什麼要擴展'CursorAdapter'?只需使用'SimpleCursorAdapter'來代替,也可以將你的Activity添加到清單中:'' – pskink

+0

@pskink我這樣做但仍然說不幸已經停止 – Setare

回答

2

manifest.xml

<activity 
android:name=".ShowDatata"/> 
+0

是的,我忘了做這個工作,謝謝我這樣做,刷新我的logcat – Setare

+1

@Setare這是一個問答網站。在得到答案後不要改變你的問題。 –

相關問題