i want to use clear() in my CostomArrayAdapter, but i get always this error:
09-08 18:15:01.960: E/AndroidRuntime(27192): FATAL EXCEPTION: main
09-08 18:15:01.960: E/AndroidRuntime(27192): java.lang.UnsupportedOperationException
09-08 18:15:01.960: E/AndroidRuntime(27192): at java.util.AbstractList.remove(AbstractList.java:638)
09-08 18:15:01.960: E/AndroidRuntime(27192): at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
09-08 18:15:01.960: E/AndroidRuntime(27192): at java.util.AbstractList.removeRange(AbstractList.java:658)
09-08 18:15:01.960: E/AndroidRuntime(27192): at java.util.AbstractList.clear(AbstractList.java:466)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.widget.ArrayAdapter.clear(ArrayAdapter.java:258)
09-08 18:15:01.960: E/AndroidRuntime(27192): at de.fterhorst.pictoriusvertretungsplan.SimpleArrayadapterDay.dayempty(SimpleArrayadapterDay.java:235)
09-08 18:15:01.960: E/AndroidRuntime(27192): at de.fterhorst.pictoriusvertretungsplan.SimpleArrayadapterDay.getView(SimpleArrayadapterDay.java:177)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.widget.AbsListView.obtainView(AbsListView.java:2177)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.widget.ListView.makeAndAddView(ListView.java:1840)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.widget.ListView.fillDown(ListView.java:675)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.widget.ListView.fillFromTop(ListView.java:736)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.widget.ListView.layoutChildren(ListView.java:1655)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.widget.AbsListView.onLayout(AbsListView.java:2012)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.View.layout(View.java:14289)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.ViewGroup.layout(ViewGroup.java:4559)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.View.layout(View.java:14289)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.ViewGroup.layout(ViewGroup.java:4559)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.View.layout(View.java:14289)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.ViewGroup.layout(ViewGroup.java:4559)
09-08 18:15:01.960: E/AndroidRuntime(27192): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.View.layout(View.java:14289)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.ViewGroup.layout(ViewGroup.java:4559)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.View.layout(View.java:14289)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.ViewGroup.layout(ViewGroup.java:4559)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.Choreographer.doFrame(Choreographer.java:532)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.os.Handler.handleCallback(Handler.java:730)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.os.Handler.dispatchMessage(Handler.java:92)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.os.Looper.loop(Looper.java:137)
09-08 18:15:01.960: E/AndroidRuntime(27192): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-08 18:15:01.960: E/AndroidRuntime(27192): at java.lang.reflect.Method.invokeNative(Native Method)
09-08 18:15:01.960: E/AndroidRuntime(27192): at java.lang.reflect.Method.invoke(Method.java:525)
09-08 18:15:01.960: E/AndroidRuntime(27192): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-08 18:15:01.960: E/AndroidRuntime(27192): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-08 18:15:01.960: E/AndroidRuntime(27192): at dalvik.system.NativeStart.main(Native Method)
when i make it so:
this.clear();
this.notifyDataSetChanged();
on my ArrayAdapter:
public class CostomArrayAdapter extends ArrayAdapter<String>
can i use clear() on a arrayadapter,
i hope someone can help me.
The error you are getting isn't from calling clear() on the ArrayAdapter. You are using a List implementation (possibly the default) which doesn't implement a clear() method and therefore the parent class', ie. AbstractList implementation is used, which throws UnsupportedOperationException.
If you pass an array to the constructor
public ArrayAdapter(Context context, int resource, T[] objects) {
init(context, resource, 0, Arrays.asList(objects));
}
it calls Arrays.asList() which returns a List which you cannot add() to, remove() from or clear().
Use this constructor
public ArrayAdapter(Context context, int resource, List<T> objects) {
init(context, resource, 0, objects);
}
by passing a LinkedList or ArrayList containing your objects.
If an array is used to initialize and ArrayAdaptor, the array is converted into an AbstractList. AbstractList does not support remove() or clear().
You can use a List which exposes a clear.remove, like an ArrayList, instead of an array to initialize.
Edit: To make it clear as suggested, it is not really converted to an AbstractList. Its converted to a special ArrayList which extends AbstractList and has no clear/remove methods.
in Kotlin, the result of
toList(), also does NOT respond to clear() function.
You should use toMutableList() when setting your ArrayAdaptor out of another collection or a literal.
In addition to what Sotirios Delimanolis and rocketboy said, I guess you should fire clear operation on reference of your custom adatper, say for example,
ArrayAdapter<String> adapter = (ArrayAdapter<String>) Listview.getAdapter();
if(adapter!= null)
{
adapter.clear();
adapter.notifyDataSetChanged();
}
Related
I'm getting a NullPointerException while trying to start an Activity which contains a ListView .
In the getView method of the adapter class, the exception happens when the setText function of a textView is being called .
The code bellow is my adapter class:
public class QuestionsListAdapter extends ArrayAdapter<Question> {
Context context;
List<Question> questions;
public QuestionsListAdapter(Context context, List<Question> questions){
super(context, R.layout.list_item_question, questions);
this.context = context;
this.questions = questions;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.list_item_question, null);
Question question = questions.get(position);
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
Log.i(TableCreator.LOG_TAG, question.toString()); //this works fine and the string is not null .
tv.setText(question.toString()+""); //NULL POINTER EXCEPTION .
return view;
}
}
As you see, I've logged the string in the logcat and it works just fine, but the next line makes the mistake .
And this is the logcat output:
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist I/Operation Checklist﹕ |-Question-> id:1 summary:mySummary comment:myComment solution:mySolution ownerList:dummyOwner
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist D/AndroidRuntime﹕ Shutting down VM
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0f5f648)
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at org.kabiri.operationchecklist.adapter.QuestionsListAdapter.getView(QuestionsListAdapter.java:43)
at android.widget.AbsListView.obtainView(AbsListView.java:2177)
at android.widget.ListView.makeAndAddView(ListView.java:1840)
at android.widget.ListView.fillDown(ListView.java:675)
at android.widget.ListView.fillFromTop(ListView.java:736)
at android.widget.ListView.layoutChildren(ListView.java:1655)
at android.widget.AbsListView.onLayout(AbsListView.java:2012)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
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:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
The logcat shows that the error happens on this line of my adapter class:
tv.setText(question.toString()+"");
I really appreciate your help .
You already know where the problem is!
tv.setText(question.toString()+"");
is causing the NPE that means the TextView tv is null. And that means that the line
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
is not able to find the TextView. Check the question_list_item_string id and make sure it matches the id in your list_item_question.xml file
I am currently looking through the Android Developer tutorials and am on the most basic of tutorials which is passing an intent from one activity to another and displaying the result.
Eclipse shows the app has having no errors and everything works fine (not functional) till I insert these lines of code:
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
I'm looking at the LogCat and did manage to resolve one error where i'd simply named something incorrectly but now i'm stumped again. Here is my current LogCat output:
10-09 18:09:03.882: D/Send Message Button(9652): Pressed
10-09 18:09:03.917: E/FragmentManager(9652): No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.917: E/FragmentManager(9652): Activity state:
10-09 18:09:03.917: D/FragmentManager(9652): Local FragmentActivity 422d1598 State:
10-09 18:09:03.922: D/FragmentManager(9652): mCreated=truemResumed=false mStopped=false mReallyStopped=false
10-09 18:09:03.922: D/FragmentManager(9652): mLoadersStarted=false
10-09 18:09:03.922: D/FragmentManager(9652): Active Fragments in 422d1808:
10-09 18:09:03.922: D/FragmentManager(9652): #0: PlaceholderFragment{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.922: D/FragmentManager(9652): mFragmentId=#7f05003c mContainerId=#7f05003c mTag=null
10-09 18:09:03.922: D/FragmentManager(9652): mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
10-09 18:09:03.927: D/FragmentManager(9652): mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
10-09 18:09:03.927: D/FragmentManager(9652): mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
10-09 18:09:03.927: D/FragmentManager(9652): mRetainInstance=false mRetaining=false mUserVisibleHint=true
10-09 18:09:03.927: D/FragmentManager(9652): mFragmentManager=FragmentManager{422d1808 in DisplayMessageActivity{422d1598}}
10-09 18:09:03.927: D/FragmentManager(9652): mActivity=com.example.myfirstapp.DisplayMessageActivity#422d1598
10-09 18:09:03.927: D/FragmentManager(9652): Added Fragments:
10-09 18:09:03.927: D/FragmentManager(9652): #0: PlaceholderFragment{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.927: D/FragmentManager(9652): FragmentManager misc state:
10-09 18:09:03.927: D/FragmentManager(9652): mActivity=com.example.myfirstapp.DisplayMessageActivity#422d1598
10-09 18:09:03.927: D/FragmentManager(9652): mContainer=android.support.v4.app.FragmentActivity$2#422d1880
10-09 18:09:03.927: D/FragmentManager(9652): mCurState=2 mStateSaved=false mDestroyed=false
10-09 18:09:03.927: D/FragmentManager(9652): View Hierarchy:
10-09 18:09:03.927: D/FragmentManager(9652): com.android.internal.policy.impl.PhoneWindow$DecorView{422d31a8 V.E..... ... 0,0-0,0}
10-09 18:09:03.932: D/FragmentManager(9652): com.android.internal.widget.ActionBarOverlayLayout{422d37e8 V.ED.... ... 0,0-0,0 #1020313 android:id/action_bar_overlay_layout}
10-09 18:09:03.932: D/FragmentManager(9652): android.widget.FrameLayout{422d43f8 V.E..... ... 0,0-0,0 #1020002 android:id/content}
10-09 18:09:03.932: D/FragmentManager(9652): android.widget.TextView{422e3b30 V.ED.... ... 0,0-0,0}
10-09 18:09:03.932: D/FragmentManager(9652): com.android.internal.widget.ActionBarContainer{422d47f8 V.ED.... ... 0,0-0,0 #1020314 android:id/action_bar_container}
10-09 18:09:03.932: D/FragmentManager(9652): com.android.internal.widget.ActionBarView{422d4d00 V.E..... ... 0,0-0,0 #1020315 android:id/action_bar}
10-09 18:09:03.932: D/FragmentManager(9652): android.widget.LinearLayout{422d5270 VFE...C. ... 0,0-0,0}
10-09 18:09:03.932: D/FragmentManager(9652): com.android.internal.widget.ActionBarView$HomeView{422d6350 V.E..... ... 0,0-0,0}
10-09 18:09:03.937: D/FragmentManager(9652): android.widget.ImageView{422d66e8 V.ED.... ... 0,0-0,0 #102025a android:id/up}
10-09 18:09:03.937: D/FragmentManager(9652): android.widget.ImageView{422d6a48 V.ED.... ... 0,0-0,0 #102002c android:id/home}
10-09 18:09:03.937: D/FragmentManager(9652): android.widget.LinearLayout{422d7c40 G.E..... ... 0,0-0,0}
10-09 18:09:03.937: D/FragmentManager(9652): android.widget.TextView{422d7f58 V.ED.... ... 0,0-0,0 #1020265 android:id/action_bar_title}
10-09 18:09:03.937: D/FragmentManager(9652): android.widget.TextView{422d8be0 G.ED.... ... 0,0-0,0 #1020266 android:id/action_bar_subtitle}
10-09 18:09:03.937: D/FragmentManager(9652): com.android.internal.widget.ActionBarContextView{422d9200 G.E..... ... 0,0-0,0 #1020316 android:id/action_context_bar}
10-09 18:09:03.937: D/AndroidRuntime(9652): Shutting down VM
10-09 18:09:03.937: W/dalvikvm(9652): threadid=1: thread exiting with uncaught exception (group=0x41f55ba8)
10-09 18:09:03.942: E/AndroidRuntime(9652): FATAL EXCEPTION: main
10-09 18:09:03.942: E/AndroidRuntime(9652): Process: com.example.myfirstapp, PID: 9652
10-09 18:09:03.942: E/AndroidRuntime(9652): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.os.Handler.dispatchMessage(Handler.java:102)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.os.Looper.loop(Looper.java:136)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.ActivityThread.main(ActivityThread.java:5001)
10-09 18:09:03.942: E/AndroidRuntime(9652): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 18:09:03.942: E/AndroidRuntime(9652): at java.lang.reflect.Method.invoke(Method.java:515)
10-09 18:09:03.942: E/AndroidRuntime(9652): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-09 18:09:03.942: E/AndroidRuntime(9652): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-09 18:09:03.942: E/AndroidRuntime(9652): at dalvik.system.NativeStart.main(Native Method)
10-09 18:09:03.942: E/AndroidRuntime(9652): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:934)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.Activity.performStart(Activity.java:5241)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
10-09 18:09:03.942: E/AndroidRuntime(9652): ... 11 more
MainActivity.java
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
public void sendMessage(View view) {
Log.d("Send Message Button", "Pressed");
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
DisplayMessageActivity.java
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
return rootView;
}
}
}
Any help is appreciated. Thanks
EDIT: Ran a clean and this is logcat now:
10-09 18:24:43.317: W/dalvikvm(9771): threadid=1: thread exiting with uncaught exception (group=0x41f55ba8)
10-09 18:24:43.322: E/AndroidRuntime(9771): FATAL EXCEPTION: main
10-09 18:24:43.322: E/AndroidRuntime(9771): Process: com.example.myfirstapp, PID: 9771
10-09 18:24:43.322: E/AndroidRuntime(9771): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{4228bce8 #0 id=0x7f05003c}
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.os.Handler.dispatchMessage(Handler.java:102)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.os.Looper.loop(Looper.java:136)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.ActivityThread.main(ActivityThread.java:5001)
10-09 18:24:43.322: E/AndroidRuntime(9771): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 18:24:43.322: E/AndroidRuntime(9771): at java.lang.reflect.Method.invoke(Method.java:515)
10-09 18:24:43.322: E/AndroidRuntime(9771): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-09 18:24:43.322: E/AndroidRuntime(9771): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-09 18:24:43.322: E/AndroidRuntime(9771): at dalvik.system.NativeStart.main(Native Method)
10-09 18:24:43.322: E/AndroidRuntime(9771): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{4228bce8 #0 id=0x7f05003c}
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:934)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.Activity.performStart(Activity.java:5241)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
10-09 18:24:43.322: E/AndroidRuntime(9771): ... 11 more
You have replace the view R.layout.activity_display_message with text view. The layout R.layout.activity_display_message contains the container to put fragment into it.
The IllegalArgument is passed in your setContentView that is textView in your case so the container Id in you mainActivity is missing and your fragment is still attached to your activity thats why the exception occured.
Though you have not provided xml. Check your xml file and look for container Because it says No view found for container. Try adding or replacing it with frame layout
10-09 18:09:03.942: E/AndroidRuntime(9652): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{422e3888 #0 id=0x7f05003c}
In your setContentView you have to set a layout you defined in /res/layout folder. There you can create your textView...
For example setContentView(R.layout.myFirstLayout);
My app keeps crashing when i select item from list. I have tried literally everything that is available from previously asked questions regarding this issue. I know the issue is within something to do with the intent and context. I just can't seem to figure out why this isn't working. Some please help
package com.example.f4f;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class RecipesAdapter extends BaseAdapter implements OnClickListener{
private Context context;
private MainActivity activity;
private LayoutInflater layoutInflater;
private ArrayList<Recipes> recipes;
public RecipesAdapter(Context context){
this.context = context;
}
public RecipesAdapter (MainActivity a, LayoutInflater l, ArrayList<Recipes> data)
{
this.activity = a;
this.layoutInflater = l;
this.recipes = data;
}
#Override
public int getCount(){
return this.recipes.size();
}
#Override
public boolean areAllItemsEnabled ()
{
return true;
}
#Override
public Object getItem(int arg0){
return null;
}
#Override
public long getItemId(int pos) {
return pos;
}
#Override
public View getView(int pos, View convertView, ViewGroup parent) {
MyViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate (R.layout.list_items, parent, false);
View myView = (View) convertView.findViewById(R.id.listitem);
if(pos % 2 == 0){
myView.setBackgroundColor(Color.parseColor("#d3d3d3"));
}
holder = new MyViewHolder();
holder.recipeName = (TextView) convertView.findViewById(R.id.textView1);
holder.rating = (TextView) convertView.findViewById(R.id.textView2);
//holder.storyDesp = (TextView) convertView.findViewById(R.id.textView3);
convertView.setTag(holder);
}
else {
holder = (MyViewHolder) convertView.getTag();
}
convertView.setOnClickListener(this);
Recipes recipe = recipes.get(pos);
holder.recipe = recipe;
holder.recipeName.setText(recipe.getrecipeName());
holder.rating.setText(recipe.getRating());
//holder.storyDesp.setText(news.getDesp());
return convertView;
}
#Override
public void onClick(View v) {
MyViewHolder holder = (MyViewHolder) v.getTag();
if (v instanceof View) {
//String selectedrecipe = holder.recipe.getID();
//String selectedrecipeurl = "http://www.yummly.com/recipe/external/"+selectedrecipe;
//Uri uri = Uri.parse("http://www.yummly.com/recipe/external/"+selectedrecipe);
Intent intent = new Intent(this.context, SelectedRecipeWebView.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
public static class MyViewHolder {
public TextView recipeName, rating;
public Recipes recipe;
}
}
09-08 01:10:05.933: D/dalvikvm(3346): GC_FOR_ALLOC freed 21K, 3% free 3931K/4028K, paused 2ms, total 2ms
09-08 01:10:05.933: D/dalvikvm(3346): GC_FOR_ALLOC freed 5K, 3% free 4147K/4252K, paused 2ms, total 2ms
09-08 01:10:05.937: I/dalvikvm-heap(3346): Grow heap (frag case) to 6.537MB for 2536932-byte allocation
09-08 01:10:05.941: D/dalvikvm(3346): GC_FOR_ALLOC freed <1K, 2% free 6624K/6732K, paused 3ms, total 3ms
09-08 01:10:06.001: W/EGL_genymotion(3346): eglSurfaceAttrib not implemented
09-08 01:10:07.153: D/AndroidRuntime(3346): Shutting down VM
09-08 01:10:07.153: W/dalvikvm(3346): threadid=1: thread exiting with uncaught exception (group=0xa4d28b20)
09-08 01:10:07.153: E/AndroidRuntime(3346): FATAL EXCEPTION: main
09-08 01:10:07.153: E/AndroidRuntime(3346): Process: com.example.f4f, PID: 3346
09-08 01:10:07.153: E/AndroidRuntime(3346): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.f4f/com.example.f4f.SelectedRecipeWebView}; have you declared this activity in your AndroidManifest.xml?
09-08 01:10:07.153: E/AndroidRuntime(3346): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
09-08 01:10:07.153: E/AndroidRuntime(3346): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
09-08 01:10:07.153: E/AndroidRuntime(3346): at android.app.Activity.startActivityForResult(Activity.java:3424)
09-08 01:10:07.153: E/AndroidRuntime(3346): at android.app.Activity.startActivityForResult(Activity.java:3385)
09-08 01:10:07.153: E/AndroidRuntime(3346): at android.app.Activity.startActivity(Activity.java:3627)
09-08 01:10:07.153: E/AndroidRuntime(3346): at android.app.Activity.startActivity(Activity.java:3595)
09-08 01:10:07.153: E/AndroidRuntime(3346): at com.example.f4f.RecipesAdapter.onClick(RecipesAdapter.java:99)
09-08 01:10:07.153: E/AndroidRuntime(3346): at android.view.View.performClick(View.java:4438)
09-08 01:10:07.153: E/AndroidRuntime(3346): at android.view.View$PerformClick.run(View.java:18422)
09-08 01:10:07.153: E/AndroidRuntime(3346): at android.os.Handler.handleCallback(Handler.java:733)
09-08 01:10:07.153: E/AndroidRuntime(3346): at android.os.Handler.dispatchMessage(Handler.java:95)
09-08 01:10:07.153: E/AndroidRuntime(3346): at android.os.Looper.loop(Looper.java:136)
09-08 01:10:07.153: E/AndroidRuntime(3346): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-08 01:10:07.153: E/AndroidRuntime(3346): at java.lang.reflect.Method.invokeNative(Native Method)
09-08 01:10:07.153: E/AndroidRuntime(3346): at java.lang.reflect.Method.invoke(Method.java:515)
09-08 01:10:07.153: E/AndroidRuntime(3346): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-08 01:10:07.153: E/AndroidRuntime(3346): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-08 01:10:07.153: E/AndroidRuntime(3346): at dalvik.system.NativeStart.main(Native Method)
09-08 01:10:09.529: D/dalvikvm(3388): Late-enabling CheckJNI
09-08 01:10:09.585: D/dalvikvm(3388): GC_FOR_ALLOC freed 37K, 3% free 3582K/3692K, paused 2ms, total 2ms
09-08 01:10:09.621: D/libEGL(3388): loaded /system/lib/egl/libEGL_genymotion.so
09-08 01:10:09.621: D/(3388): HostConnection::get() New Host Connection established 0xb8e43568, tid 3388
09-08 01:10:09.629: D/libEGL(3388): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
09-08 01:10:09.629: D/libEGL(3388): loaded /system/lib/egl/libGLESv2_genymotion.so
09-08 01:10:09.653: W/EGL_genymotion(3388): eglSurfaceAttrib not implemented
09-08 01:10:09.653: E/OpenGLRenderer(3388): Getting MAX_TEXTURE_SIZE from GradienCache
09-08 01:10:09.653: E/OpenGLRenderer(3388): MAX_TEXTURE_SIZE: 16384
09-08 01:10:09.661: E/OpenGLRenderer(3388): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
09-08 01:10:09.661: E/OpenGLRenderer(3388): MAX_TEXTURE_SIZE: 16384
09-08 01:10:09.661: D/OpenGLRenderer(3388): Enabling debug mode 0
You have initialized your context only within the RecipesAdapter(Context context). If you are not using this constructor while creating the instance of RecipesAdapter, your context has a null value when it is referenced. You need to initialize this value.
From the looks of this class, it looks like you used the other constructor. I am inferring this because your layoutInflater is set and apparently you do not have a problem with that.
EDIT: To pass an instance of the current context, you can replace the activity field in the constructor as RecipesAdapter(Context context, LayoutInflater l, ArrayList data). I am saying this since this is the normal manner in which this is done (also because you do not seem to be using your MainActivity field anywhere).
As I have seen on previously asked questions, inside the custom adapter class (say, MyAdapter extends ArrayAdapter) they always use an inflated xml list-item layout. What I am hoping to do is create everything entirely using Java and no XML...
// for example
String[] wordlist = new String[] {a, b, c};
LinearLayout list_item_layout = new LinearLayout(this);
list_item_layout.setId(5000);
TextView listText = new TextView(this);
listText.setId(5001);
listLayout.addView(listText);
ListView list = new ListView(this);
// ** QUESTION ** do I declare a programmatic .setAdapter() like this?
// ** TAKE NOTE ** I passed 'wordlist' here..
list.setAdapter(new MyAdapter(this, list_item_layout.getId(), listText.getId(), wordlist));
and then for MyAdapter...
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
super(context, resource, textViewResourceId, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView; //is this the list_item_layout that I passed??
TextView tv = (TextView) v.findViewById(5001);
// ** QUESTION ** do I pass 'wordlist' again here?
tv.setText( wordlist[position] );
return v;
}
}
What happens when I run this on my device is I get the following errors...
10-08 23:11:19.775: E/AndroidRuntime(18276): FATAL EXCEPTION: main
10-08 23:11:19.775: E/AndroidRuntime(18276): android.content.res.Resources$NotFoundException: String resource ID #0x0
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.content.res.Resources.getText(Resources.java:222)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.widget.TextView.setText(TextView.java:3011)
10-08 23:11:19.775: E/AndroidRuntime(18276): at com.turista.client.TuristaClientMain.onClick(TuristaClientMain.java:113)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.view.View.performClick(View.java:2538)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.view.View$PerformClick.run(View.java:9152)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Handler.handleCallback(Handler.java:587)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Handler.dispatchMessage(Handler.java:92)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Looper.loop(Looper.java:123)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.app.ActivityThread.main(ActivityThread.java:3691)
10-08 23:11:19.775: E/AndroidRuntime(18276): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 23:11:19.775: E/AndroidRuntime(18276): at java.lang.reflect.Method.invoke(Method.java:507)
10-08 23:11:19.775: E/AndroidRuntime(18276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-08 23:11:19.775: E/AndroidRuntime(18276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-08 23:11:19.775: E/AndroidRuntime(18276): at dalvik.system.NativeStart.main(Native Method)
Can anyone explain how to do this programmatically?
* EDITED * October 9, 2012
Ok, as I am still stuck in this issue I think I've made some improvements but still get error messages. The improved code is as follows..
//wordlist is a global variable
String[] wordlist = new String[] {a, b, c};
// ..
// .. inside onCreate...
ListView list = new ListView(this);
list.setAdapter(new MyAdapter(this, R.layout.listitem, R.id.mLargeTextView, wordlist));
// since the ArrayAdapter class needs XML parameters to inflate, I created a dummy layout
now inside MyAdapter class..
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
super(context, resource, textViewResourceId, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout listLayout = new LinearLayout(Main.this);
listLayout.setLayoutParams(new LayoutParams(wrapContent, wrapContent));
listLayout.setId(5000);
TextView listText = new TextView(Main.this);
listText.setId(5001);
listLayout.addView(listText);
listText.setText(wordlist[position]);
return listLayout;
}
}
As you can see, I think I have to override getView in order to display my custom view so this is what I did. Unfortunately I think I misunderstood it. Here are the errors..
10-09 09:24:10.095: E/AndroidRuntime(2517): FATAL EXCEPTION: main
10-09 09:24:10.095: E/AndroidRuntime(2517): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.measureScrapChild(ListView.java:1183)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.measureHeightOfChildren(ListView.java:1266)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.onMeasure(ListView.java:1175)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewRoot.performTraversals(ViewRoot.java:847)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.os.Looper.loop(Looper.java:123)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.app.ActivityThread.main(ActivityThread.java:3691)
10-09 09:24:10.095: E/AndroidRuntime(2517): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 09:24:10.095: E/AndroidRuntime(2517): at java.lang.reflect.Method.invoke(Method.java:507)
10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-09 09:24:10.095: E/AndroidRuntime(2517): at dalvik.system.NativeStart.main(Native Method)
ListView extends AbsListView which in turn extends AdapterView which extends view group. So all the child of ListView would be added to ViewGroup. So to set the layout param, you can
use ViewGroup.LayoutParam while setting layout param for listLayout.
Or try AbsListView.LayoutParams for setting layout param for listLayout
Please try it and let me know if it worked.
Following code is working fine..
public class MainActivity1 extends Activity {
String[] wordlist = new String[] { "a", "b", "c" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView list = new ListView(this);
list.setAdapter(new MyAdapter(this, wordlist));
setContentView(list);
}
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, String[] strings) {
super(context, -1, -1, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout listLayout = new LinearLayout(MainActivity1.this);
listLayout.setLayoutParams(new AbsListView.LayoutParams(
AbsListView.LayoutParams.WRAP_CONTENT,
AbsListView.LayoutParams.WRAP_CONTENT));
listLayout.setId(5000);
TextView listText = new TextView(MainActivity1.this);
listText.setId(5001);
listLayout.addView(listText);
listText.setText(super.getItem(position));
return listLayout;
}
}
}
The problem was that by default Eclipse imports ViewGroup.LayoutParams which are incompatible with the AbsListView.LayoutParams which need to be used for setting layout param for view returned from the getView() method.
Please check and let me know how it went..
To add to the above answer, setting id with a constant number is not a good idea.
Replace, listLayout.setId(5000); with,listLayout.setId(View.generateViewId());
In case you are targeting API 16 and below, refer this.
I am trying to create my first android+html 5 application..
My Activity code is :
import android.os.Bundle;
import android.app.Activity;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = new WebView(this);
String summary = "<html><body>HTML 5 Test</body></html>";
webView = (WebView)findViewById(R.id.webView);
webView.loadData( summary, "text/html; character=UTF-8", null);
setContentView(webView);
}
}
When I run above code, It gives Null Pointer Exception...
09-08 09:27:21.393: D/AndroidRuntime(562): Shutting down VM
09-08 09:27:21.393: W/dalvikvm(562): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
09-08 09:27:21.530: E/AndroidRuntime(562): FATAL EXCEPTION: main
09-08 09:27:21.530: E/AndroidRuntime(562): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rightquery.test/com.rightquery.test.MainActivity}: java.lang.NullPointerException
09-08 09:27:21.530: E/AndroidRuntime(562): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-08 09:27:21.530: E/AndroidRuntime(562): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-08 09:27:21.530: E/AndroidRuntime(562): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-08 09:27:21.530: E/AndroidRuntime(562): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-08 09:27:21.530: E/AndroidRuntime(562): at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 09:27:21.530: E/AndroidRuntime(562): at android.os.Looper.loop(Looper.java:137)
09-08 09:27:21.530: E/AndroidRuntime(562): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-08 09:27:21.530: E/AndroidRuntime(562): at java.lang.reflect.Method.invokeNative(Native Method)
09-08 09:27:21.530: E/AndroidRuntime(562): at java.lang.reflect.Method.invoke(Method.java:511)
09-08 09:27:21.530: E/AndroidRuntime(562): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-08 09:27:21.530: E/AndroidRuntime(562): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-08 09:27:21.530: E/AndroidRuntime(562): at dalvik.system.NativeStart.main(Native Method)
09-08 09:27:21.530: E/AndroidRuntime(562): Caused by: java.lang.NullPointerException
09-08 09:27:21.530: E/AndroidRuntime(562): at com.rightquery.test.MainActivity.onCreate(MainActivity.java:19)
09-08 09:27:21.530: E/AndroidRuntime(562): at android.app.Activity.performCreate(Activity.java:4465)
09-08 09:27:21.530: E/AndroidRuntime(562): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-08 09:27:21.530: E/AndroidRuntime(562): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-08 09:27:21.530: E/AndroidRuntime(562): ... 11 more
EDIT: Layout
<RelativeLayout 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" >
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp" />
</RelativeLayout>
What is wrong ?
The Solution:
import android.os.Bundle;
import android.app.Activity;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = new WebView(this);
setContentView(R.layout.main);
String summary = "<html><body>HTML 5 Test</body></html>";
webView = new WebView(this);
webView.loadData( summary, "text/html; character=UTF-8", null);
setContentView(webView);
}
}
You need to do setContentView(R.layout.youView); before calling
webView = (WebView)findViewById(R.id.webView);
you call like this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.XMLname);
.....
}
in your onCreate do changes as
setContentView(R.layout.main);
webView = (WebView)findViewById(R.id.webView);