I am still having a problem with just adding a button to the screen and getting it to execute my code. Right now, I will be happy with select the button and getting the "log,d (msg);" to work. The problem may be due to all my code in the fragment and/or combination of buttons and a list that I scroll on the same screen. It is probably a very basic problem.
My application first starts with a scrolling list created by a cursor from SQLite table. So it is an open ended list. Select an item and I get a detail screen to allow add, update or delete. That all works.
Now I am adding 3 buttons above the list.
I am a newbie, and the book I started with said to create a fragment, so the screen with the list is coded that way. (The detail screen is rewritten to just the activity.)
I have tried lots of things, but roughly three ways below:
In my onCreate method:
"btn_Add.setOnClickListener(new View.OnClickListener()"
As suggested in a recent - reply - This executes fragment onCreate , then crashes with these messages:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.dummies.android.taskreminder/com.dummies.android.
taskreminder. EventListActivity}:Binary XML file line #34: Error inflating class fragment
As a separate method after the onCreate:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
This causes a loop - repeating the onCreate several times then crashes with this:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.dummies.android.taskreminder/com.dummies.android.
taskreminder.EventListActivity}: android.view.InflateException:
Binary XML file line #8: Error inflating class <unknown>
3 I added this to the XML file in button definition:
android:onClick="MyButton"
and
public void MyButton(View v)
{
Log.d(". . . " + v);
}
This allowed the application to load, but when I selected the button, it crashed when I selected the button with the following message:
java.lang.IllegalStateException: Could not find a method MyButton(View) in the activity
This is pieces from the fragment: eventListFragment.java
public class EventListFragment extends ListFragment
implements LoaderCallbacks<Cursor>
{
. . . .
List strRecord = new ArrayList(3);
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
. . . .
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
)
//------------------------------------
// THIS IS EXAMPLE # 1
// java.lang.RuntimeException: Unable to start activity
// ComponentInfo{com.dummies.android.taskreminder/com.dummies.android.taskreminder.EventListActivity}:
// android.view.InflateException: Binary XML file line #34: Error inflating class fragment
btn_Add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// you may want to perform your clicks here.
Log.d("EventLst","S create view btnAddEvent:" + v);
}
});
//-------------------------------------
} //----> END: public void onCreate
//-------------------------------------
// THIS IS EXAMPLE #2:
// it give me this error on initial load:
// java.lang.RuntimeException: Unable to start activity
// ComponentInfo{com.dummies.android.taskreminder/com.dummies.android.taskreminder.EventListActivity}:
// android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.event_list, container, false);
Button button = (Button) view.findViewById(R.id.btn_Add);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Activity activity = getActivity();
if (activity != null)
{
Log.d("EventLst","S create view btnAddEvent:" + v);
Toast.makeText(activity, "GOT button", Toast.LENGTH_LONG).show();
}
}
} );
return view;
}
//---------------------------------------
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
{
. . .
while (cursor.isAfterLast() == false)
{
strRecord.add(iRecNo, strDate + ": " + strC[2]);
}
lstAdapter = new ArrayAdapter<String>(getActivity(),
R.layout.event_row, R.id.text1, strRecord);
setListAdapter(lstAdapter);
This is the main layout: event_list.XML Note: android:onClick="MyButton" only in try #3 above
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:name="com.dummies.android.taskreminder.EventListFragment" >
<Button android:id="@+id/btn_Add"
android:onClick="MyButton"
android:layout_width="50dp"
android:layout_height="30dp"
android:textSize="12dp"
android:padding="1dip"
android:text="@string/add" />
<Button android:id="@+id/btn_Exit"
android:onClick="onClick"
android:layout_width="50dp"
android:layout_height="30dp"
android:textSize="12dp"
android:padding="1dip"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/btn_Add"
android:text="@string/exit" />
<Button android:id="@+id/btn_Other"
android:layout_width="50dp"
android:layout_height="30dp"
android:textSize="12dp"
android:padding="1dip"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/btn_Exit"
android:text="@string/other" />
<fragment
android:name="com.dummies.android.taskreminder.EventListFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_Add" />
</RelativeLayout>
This is the row to create the scrow list: event_row.XML
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="12dp"
android:padding="2dip" />
Luksprog requested this information in his reply please post or at least make available to him/her.
First I created this XML file, event_list_activity.xml, guessed a little on the filename:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Also added to strings:
<string name="container">EventContainer</string>
Ran my test and this is the complete log: (First three records from my code):
04-28 16:09:24.504: D/EventLst(258): X onCrt: x01strI:eventname
04-28 16:09:24.544: D/EventLst(258): X onCrt: x02 START
04-28 16:09:24.584: D/EventLst(258): X onCrt: x03 START
04-28 16:09:24.624: D/AndroidRuntime(258): Shutting down VM
04-28 16:09:24.646: W/dalvikvm(258): threadid=3: thread exiting with uncaught exception (group=0x4001b1d8)
04-28 16:09:24.646: E/AndroidRuntime(258): Uncaught handler: thread main exiting due to uncaught exception
04-28 16:09:24.646: W/dalvikvm(258): threadid=3: thread exiting with uncaught exception (group=0x4001b1d8)
04-28 16:09:24.646: E/AndroidRuntime(258): Uncaught handler: thread main exiting due to uncaught exception
04-28 16:09:25.065: E/AndroidRuntime(258): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.dummies.android.taskreminder/com.dummies.android.taskreminder.EventListActivity}:
android.view.InflateException: Binary XML file line #33: Error inflating class fragment
04-28 16:09:25.065: E/AndroidRuntime(258): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-28 16:09:25.065: E/AndroidRuntime(258): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-28 16:09:25.065: E/AndroidRuntime(258): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-28 16:09:25.065: E/AndroidRuntime(258): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-28 16:09:25.065: E/AndroidRuntime(258): at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 16:09:25.065: E/AndroidRuntime(258): at android.os.Looper.loop(Looper.java:123)
04-28 16:09:25.065: E/AndroidRuntime(258): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-28 16:09:25.065: E/AndroidRuntime(258): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 16:09:25.065: E/AndroidRuntime(258): at java.lang.reflect.Method.invoke(Method.java:521)
04-28 16:09:25.065: E/AndroidRuntime(258): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-28 16:09:25.065: E/AndroidRuntime(258): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-28 16:09:25.065: E/AndroidRuntime(258): at dalvik.system.NativeStart.main(Native Method)
04-28 16:09:25.065: E/AndroidRuntime(258): Caused by: android.view.InflateException: Binary XML file line #33:
Error inflating class fragment
04-28 16:09:25.065: E/AndroidRuntime(258): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582)
04-28 16:09:25.065: E/AndroidRuntime(258): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
04-28 16:09:25.065: E/AndroidRuntime(258): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
04-28 16:09:25.065: E/AndroidRuntime(258): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-28 16:09:25.065: E/AndroidRuntime(258): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-28 16:09:25.065: E/AndroidRuntime(258): at
com.android.internal.policy.impl.WimmWindow.setContentView(WimmWindow.java:181)
04-28 16:09:25.065: E/AndroidRuntime(258): at android.app.Activity.setContentView(Activity.java:1622)
04-28 16:09:25.065: E/AndroidRuntime(258): at
com.dummies.android.taskreminder.EventListActivity.onCreate(EventListActivity.java:22)
04-28 16:09:25.065: E/AndroidRuntime(258): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-28 16:09:25.065: E/AndroidRuntime(258): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-28 16:09:25.065: E/AndroidRuntime(258): ... 11 more
04-28 16:09:25.065: E/AndroidRuntime(258): Caused by: java.lang.NullPointerException
04-28 16:09:25.065: E/AndroidRuntime(258): at
com.dummies.android.taskreminder.EventListFragment.onCreate(EventListFragment.java:98)
04-28 16:09:25.065: E/AndroidRuntime(258): at
android.support.v4.app.Fragment.performCreate(Fragment.java:1437)
04-28 16:09:25.065: E/AndroidRuntime(258): at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
04-28 16:09:25.065: E/AndroidRuntime(258): at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
04-28 16:09:25.065: E/AndroidRuntime(258): at
android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168)
04-28 16:09:25.065: E/AndroidRuntime(258): at
android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
04-28 16:09:25.065: E/AndroidRuntime(258): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558)
Next I added your statements below and get errors below is from the start of the fragment to the end of on create Note: I also had to add: import android.support.v4.app.FragmentTransaction;
package com.dummies.android.taskreminder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
//* below insures that you are using the ...v4 for version android 1.6
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.MenuItem;
import android.content.ContentValues;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Button;
import android.widget.Toast;
import android.util.Log;
public class EventListFragment extends ListFragment
implements
LoaderCallbacks<Cursor>
{
private SimpleCursorAdapter mAdapter;
List strRecId = new ArrayList();
private ListAdapter lstAdapter;
private Button btn_Add;
private Button myButton;
List strRecord = new ArrayList(3);
List strFields = new ArrayList();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//---------------------------------------------------------------
// assuming you use FragmentActivity
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, new EvetListFragment());
ft.commit();
// errors above:
// For: getSupportFragmentManager()
// "The method getSupportFragmentManager() is undefined for the type EventListFragment"
// For: EvetListFragment());
// "EvetListFragment cannot be resolved to a type"
//----------------------------------------------------------------
// BELOW READS FROM DATABASE AND DISPLAYS EVENTNAME
// THEN ALLOWS UPDATE OF THEM ABD INSERT FROM EXISTING
// Log.d("EventLst","X onCrt: x01 START" );
String[] strItems = new String[]
{
EventProvider.COLUMN_EVENTNAME
};
Log.d("EventLst","X onCrt: x01strI:" + strItems[0]);
int[] iCnt = new int[]
{
R.id.text1
};
mAdapter = new SimpleCursorAdapter(getActivity(),R.layout.event_row,
null, strItems, iCnt, 0);
Log.d("EventLst","X onCrt: x02 START" );
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
Log.d("EventLst","X onCrt: x03 START" );
//---------------------------------------------------------------------------------
// I thought this set my OnClickListener. Without this code, the application runs
// but the buttons don't do anything
btn_Add.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Log.d("EventLst","S onclkBtn2" + v);
}
} );
//-------------------------------------------------------------------------------
} //----> END: public void onCreate
Many thanks for your help.