我在导航栏中创建了一个待办事项列表选项, 它打开了但是当我点击添加项目按钮时,我的应用程序崩溃了。 我扩展了片段。
请帮忙!
(这是从我的主java文件中调用的)
public class todo extends Fragment {
View myView;
private ArrayList<String> items;
private ArrayAdapter<String> itemsadapter;
private ListView lvItems;
private void readItems() {
File filesDir = this.getContext().getFilesDir();
File todoFile = new File(filesDir, "todo.txt");
try {
items = new ArrayList<String>(FileUtils.readLines(todoFile));
} catch (IOException e) {
items = new ArrayList<String>();
}
}
private void writeItems() {
File filesDir = this.getContext().getFilesDir();
File todoFile = new File(filesDir, "todo.txt");
try {
FileUtils.writeLines(todoFile, items);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.todo_layout, container, false);
lvItems=(ListView) myView.findViewById(R.id.lvItems);
items= new ArrayList<String>();
readItems();
itemsadapter= new ArrayAdapter<String>(this.getContext(),android.R.layout.simple_list_item_1,items);
lvItems.setAdapter(itemsadapter);
setupListViewListener();
return myView;
}
public void onADDItem(View v)
{
EditText etNewItem=(EditText)myView.findViewById(R.id.etNewItem);
String itemText = etNewItem.getText().toString();
Boolean a =itemText.isEmpty();
if(a==true){
Toast.makeText(getActivity().getBaseContext(),"Please Enter the item in the textbox",Toast.LENGTH_SHORT).show();
}
else {
String datetimestamp= DateFormat.getDateTimeInstance().format(new Date());
itemsadapter.add(itemText + "\n" + datetimestamp);
etNewItem.setText("");
writeItems();
}
}
private void setupListViewListener()
{
lvItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> adapter,
View item, final int pos, long id) {
AlertDialog.Builder alertbox= new AlertDialog.Builder(item.getContext());
alertbox.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
items.remove(pos);
itemsadapter.notifyDataSetChanged();
writeItems();
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
return true;
}
}
);
}}
todo的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:background="@drawable/image">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:textSize="20dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="YOUR LIST:"
android:textColor="#324567"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/btnADDItem"
android:layout_alignEnd="@+id/btnADDItem" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lvItems"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_above="@+id/btnADDItem"
android:layout_below="@+id/textView"
android:textColor="#000000"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etNewItem"
android:layout_alignTop="@+id/btnADDItem"
android:hint="enter a new item"
android:textColorHint="#000000"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/btnADDItem"
android:layout_toStartOf="@+id/btnADDItem"
android:layout_alignParentBottom="true"
android:textColor="#000000"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD ITEM"
android:id="@+id/btnADDItem"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="onADDItem"
/>
</RelativeLayout>
点击"ADD ITEM"
按钮后我的应用崩溃了。我尝试了所有选择
如果我没有扩展片段,那么连接失败,因为我使用了导航抽屉。
logcat错误:
09-17 13:58:10.835: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:10.843: E/SIMInfo(7137): info.mWapPush init error, not support
in FW.
09-17 13:58:10.866: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:10.867: E/SIMInfo(7137): SimStatusChangeReceiver, ss=null
09-17 13:58:10.883: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:10.945: E/lowmemorykiller(265): Error writing
/proc/2947/oom_score_adj; errno=22
09-17 13:58:10.961: E/SIMInfo(7137): info.mWapPush init error, not support
in FW.
09-17 13:58:14.695: E/ACRA(2821): ACRA caught a IllegalStateException for
net.cyclestreets
09-17 13:58:14.695: E/ACRA(2821): java.lang.IllegalStateException: Could not
find method onADDItem(View) in a parent or ancestor Context for
android:onClick attribute defined on view class
android.support.v7.widget.AppCompatButton with id 'btnADDItem'
09-17 13:58:14.695: E/ACRA(2821): at
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325)
09-17 13:58:14.695: E/ACRA(2821): at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
09-17 13:58:14.695: E/ACRA(2821): at android.view.View.performClick(View.java:5269)
09-17 13:58:14.695: E/ACRA(2821): at android.view.View$PerformClick.run(View.java:21556)
09-17 13:58:14.695: E/ACRA(2821): at android.os.Handler.handleCallback(Handler.java:815)
09-17 13:58:14.695: E/ACRA(2821): at android.os.Handler.dispatchMessage(Handler.java:104)
09-17 13:58:14.695: E/ACRA(2821): at android.os.Looper.loop(Looper.java:207)
09-17 13:58:14.695: E/ACRA(2821): at android.app.ActivityThread.main(ActivityThread.java:5769)
09-17 13:58:14.695: E/ACRA(2821): at java.lang.reflect.Method.invoke(Native Method)
09-17 13:58:14.695: E/ACRA(2821): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
09-17 13:58:14.695: E/ACRA(2821): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
09-17 13:58:15.175: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:15.175: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:15.210: E/lowmemorykiller(265): Error writing /proc/2821/oom_score_adj; errno=22
09-17 13:58:15.802: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:15.818: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:15.834: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:15.850: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:15.867: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:15.883: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:15.899: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:15.915: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:15.931: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:15.947: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:15.964: E/SurfaceFlinger(293): strok layer name=none
09-17 13:58:16.161: E/MultiWindowProxy(870): getServiceInstance failed!
01-01 00:00:00.000: E/(-1): Device disconnected
答案 0 :(得分:0)
这是因为Android没有关联android:onClick="onADDItem"
以下内容:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD ITEM"
android:id="@+id/btnADDItem"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="onADDItem"
/>
使用Fragment中的onADDItem()
方法:
public void onADDItem(View v) {
...
}
如果该方法位于活动中,Android只会将onClick与onADDItem()
相关联。
因此,您需要在片段中使用View.OnClickListener
。
这样的事情:
Button btnAdd = (Button) findViewById(R.id.btnADDItem);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
// Do something here.
}
});
将UI布局和代码分开以处理视图点击侦听器是一种很好的做法。因此,请勿将android:onClick
用于点击监听器。让XML布局仅用于UI,并让UI行为在代码中处理。这将节省您将来维护噩梦的时间。