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);
Related
I am attempting to integrate Mixpanel into a simple application using the instructions here. However I am getting an error that is causing my App to crash.
I have no syntax errors in my code (see below).
Android Activity:
package com.example.testmixpanel;
import org.json.JSONException;
import org.json.JSONObject;
import com.mixpanel.android.mpmetrics.MixpanelAPI;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
public static final String MIXPANEL_TOKEN = "xxx"; //intentionally not shown
// Initialize the library with your
// Mixpanel project token, MIXPANEL_TOKEN, and a reference
// to your application context.
MixpanelAPI mixpanel =
MixpanelAPI.getInstance(this, MIXPANEL_TOKEN);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JSONObject props = new JSONObject();
mixpanel.track("Plan Selected", props);
}
#Override
protected void onDestroy() {
mixpanel.flush();
super.onDestroy();
}
}
However, when I try to run the application I get this error:
0-09 16:36:31.575: E/AndroidRuntime(13192): FATAL EXCEPTION: main
10-09 16:36:31.575: E/AndroidRuntime(13192): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testmixpanel/com.example.testmixpanel.MainActivity}: java.lang.NullPointerException
What could be the cause of this issue?
Full stack trace (for reference):
10-09 16:36:31.575: E/AndroidRuntime(13192): FATAL EXCEPTION: main
10-09 16:36:31.575: E/AndroidRuntime(13192): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testmixpanel/com.example.testmixpanel.MainActivity}: java.lang.NullPointerException
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.access$700(ActivityThread.java:159)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.os.Looper.loop(Looper.java:176)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.main(ActivityThread.java:5419)
10-09 16:36:31.575: E/AndroidRuntime(13192): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 16:36:31.575: E/AndroidRuntime(13192): at java.lang.reflect.Method.invoke(Method.java:525)
10-09 16:36:31.575: E/AndroidRuntime(13192): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
10-09 16:36:31.575: E/AndroidRuntime(13192): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
10-09 16:36:31.575: E/AndroidRuntime(13192): at dalvik.system.NativeStart.main(Native Method)
10-09 16:36:31.575: E/AndroidRuntime(13192): Caused by: java.lang.NullPointerException
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
10-09 16:36:31.575: E/AndroidRuntime(13192): at com.mixpanel.android.mpmetrics.MixpanelAPI.getInstance(MixpanelAPI.java:175)
10-09 16:36:31.575: E/AndroidRuntime(13192): at com.example.testmixpanel.MainActivity.<init>(MainActivity.java:22)
10-09 16:36:31.575: E/AndroidRuntime(13192): at java.lang.Class.newInstanceImpl(Native Method)
10-09 16:36:31.575: E/AndroidRuntime(13192): at java.lang.Class.newInstance(Class.java:1130)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
10-09 16:36:31.575: E/AndroidRuntime(13192): ... 11 more
EDIT:
public class MainActivity extends Activity {
public static final String MIXPANEL_TOKEN = "xxxxx";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MixpanelAPI mixpanel =
MixpanelAPI.getInstance(getApplicationContext(), MIXPANEL_TOKEN);
setContentView(R.layout.activity_main);
JSONObject props = new JSONObject();
mixpanel.track("Plan Selected", props);
}
#Override
protected void onDestroy() {
mixpanel.flush(); //how to call this as mixpanel is not a class variable?
super.onDestroy();
}
}
If you want to use the mixpanel on onDestroy you should instantiate it like this:
public class MainActivity extends Activity {
public static final String MIXPANEL_TOKEN = "xxxxx";
MixpanelAPI mixpanel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mixpanel =
MixpanelAPI.getInstance(getApplicationContext(), MIXPANEL_TOKEN);
setContentView(R.layout.activity_main);
JSONObject props = new JSONObject();
mixpanel.track("Plan Selected", props);
}
(...)
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).
I have the following code which is called by the onClick event of buttons contained within a ListView.
public void viewExerciseHistory(View view) {
// History button click handler
LinearLayout parentRow = (LinearLayout)view.getParent();
TextView exerciseNameTextView = (TextView)parentRow.getChildAt(0);
String exerciseName = (String)exerciseNameTextView.getText();
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.exercise_history_dialog);
dialog.setTitle(exerciseName);
TextView headerTextView = (TextView)view.findViewById(R.id.exercise_history_dialog_name_textview);
//headerTextView.setText("History");
Button closeDialogButton = (Button)view.findViewById(R.id.exercise_history_dialog_close_button);
//closeDialogButton.setOnClickListener(new OnClickListener() {
// #Override
// public void onClick(View v) {
// dialog.dismiss();
// }
//});
dialog.show();
}
The ListView row is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="#+id/exercise_history_dialog_name_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="monospace"
android:textSize="15sp" />
<Button android:id="#+id/exercise_history_dialog_close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:typeface="monospace"
android:textSize="12sp"
android:textColor="#color/button_blue_text"
android:text="#string/button_text_close"
style="?android:attr/borderlessButtonStyle" />
</RelativeLayout>
The above code works fine and displays the dialog, but if uncomment out the headerTextView.setText("History") or the closeDialog.setOnClickListener it crashes with a NullPointerException.
Can anyone explain what I'm doing wrong?
EDIT LogCat:
10-14 21:36:19.755: W/IInputConnectionWrapper(26167): getTextBeforeCursor on inactive InputConnection
10-14 21:36:19.755: W/IInputConnectionWrapper(26167): getTextAfterCursor on inactive InputConnection
10-14 21:36:19.915: W/IInputConnectionWrapper(26167): getTextBeforeCursor on inactive InputConnection
10-14 21:36:19.945: W/IInputConnectionWrapper(26167): getTextAfterCursor on inactive InputConnection
10-14 21:36:20.435: D/AndroidRuntime(26167): Shutting down VM
10-14 21:36:20.435: W/dalvikvm(26167): threadid=1: thread exiting with uncaught exception (group=0x40e27930)
10-14 21:36:20.445: E/AndroidRuntime(26167): FATAL EXCEPTION: main
10-14 21:36:20.445: E/AndroidRuntime(26167): java.lang.IllegalStateException: Could not execute method of the activity
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$1.onClick(View.java:3599)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View.performClick(View.java:4204)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$PerformClick.run(View.java:17355)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Handler.handleCallback(Handler.java:725)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Handler.dispatchMessage(Handler.java:92)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Looper.loop(Looper.java:137)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-14 21:36:20.445: E/AndroidRuntime(26167): at dalvik.system.NativeStart.main(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): Caused by: java.lang.reflect.InvocationTargetException
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$1.onClick(View.java:3594)
10-14 21:36:20.445: E/AndroidRuntime(26167): ... 11 more
10-14 21:36:20.445: E/AndroidRuntime(26167): Caused by: java.lang.NullPointerException
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.example.workoutlog.ManageWorkouts.viewExerciseHistory(ManageWorkouts.java:269)
10-14 21:36:20.445: E/AndroidRuntime(26167): ... 14 more
It's crashing because this line:
TextView headerTextView = (TextView) findViewById(R.id.exercise_history_dialog_name_textview);
is actually returning null.
The findViewById method is looking for the view on the main activity's content view. Instead you want to use the findViewById method on the view object. Calling findViewById on the view object will allow you to get the fields you are after.
dialog.setContentView(R.layout.exercise_history_dialog);
TextView headerTextView = (TextView) dialog.findViewById(R.id.exercise_history_dialog_name_textview);
headerTextView.setText("History");
Try changing these
TextView headerTextView = (TextView)findViewById(R.id.exercise_history_dialog_name_textview);
Button closeDialogButton = (Button)findViewById(R.id.exercise_history_dialog_close_button);
to
TextView headerTextView = (TextView)dialog.findViewById(R.id.exercise_history_dialog_name_textview);
Button closeDialogButton = (Button)dialog.findViewById(R.id.exercise_history_dialog_close_button);
You have to tell which view you want to findViewById from
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();
}