ListView doesn't work in Fragment - java

List View doesn't work in Fragment.
Fragment.java
public class Fragment_Main extends Fragment{
ArrayAdapter<String> categoryAdapter;
ArrayList<String> categoryList = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View View = inflater.inflate(R.layout.fragment_main, container, false);
ListView list = (ListView) getView().findViewById(R.id.listView1);
categoryAdapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, categoryList);
list.setAdapter(categoryAdapter);
showCategoryTask task=new showCategoryTask();
task.execute();
return View;
}
class showCategoryTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL("http://garyhui86.er-webs.com/index.xml");
HttpURLConnection urlConn =
(HttpURLConnection)url.openConnection();
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
DocumentBuilder builder = DocumentBuilderFactory
.newInstance().newDocumentBuilder();
Document document = builder.parse(urlConn.getInputStream());
NodeList nodeList = document.getElementsByTagName("Info");
for (int i = 0; i < nodeList.getLength(); i++) {
NamedNodeMap attributes=nodeList.item(i).getAttributes();
String category=attributes.getNamedItem("categoryName").getNodeValue();
Log.i("ttt", category);
categoryList.add(category);
}
}
urlConn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
categoryAdapter.notifyDataSetChanged();
}
}
}
Error Log:
04-27 15:39:02.568: E/AndroidRuntime(7826): FATAL EXCEPTION: main
04-27 15:39:02.568: E/AndroidRuntime(7826): java.lang.RuntimeException: Unable to start activity ComponentInfo{hkpro.shopdada.com.summonboardinfo/hkpro.shopdada.com.summonboardinfo.MainActivity}: java.lang.NullPointerException
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.os.Looper.loop(Looper.java:137)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-27 15:39:02.568: E/AndroidRuntime(7826): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 15:39:02.568: E/AndroidRuntime(7826): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 15:39:02.568: E/AndroidRuntime(7826): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-27 15:39:02.568: E/AndroidRuntime(7826): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-27 15:39:02.568: E/AndroidRuntime(7826): at dalvik.system.NativeStart.main(Native Method)
04-27 15:39:02.568: E/AndroidRuntime(7826): Caused by: java.lang.NullPointerException
04-27 15:39:02.568: E/AndroidRuntime(7826): at hkpro.shopdada.com.summonboardinfo.Fragment_Main.onCreateView(Fragment_Main.java:39)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.app.Activity.performStart(Activity.java:5114)
04-27 15:39:02.568: E/AndroidRuntime(7826): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
04-27 15:39:02.568: E/AndroidRuntime(7826): ... 11 more

Use the inflated object to find the 'ListView'.
Do it like this:
'ListView list = (ListView) inflated_layout_object.findViewById(R.id.listView1);'
Here for you inflated_layout_object is View (which you have created).

This lines causes trouble I guess:
View View = inflater.inflate(R.layout.fragment_main, container, false);
ListView list = (ListView) getView().findViewById(R.id.listView1);
Don't use the getView() method to find Views by Id. Use the inflated layout instead:
View root = inflater.inflate(R.layout.fragment_main, container, false);
ListView list = (ListView) root.findViewById(R.id.listView1);
The reason for this is getView() only returns the Fragments View if onCreateView has returned.

Related

Fragment did not create a view after rotating device

I am trying to make a 2 fragments screen in the tablet view but it's crashing after rotating the device.
This is layout-sw600dp/
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:id="#+id/main_activity"
android:orientation="horizontal"
tools:context="com.morxander.popularmovies.MainActivity">
<fragment
android:id="#+id/fragment_main"
android:name="com.morxander.popularmovies.MainActivity$PlaceholderFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
tools:layout="#layout/fragment_main" />
<FrameLayout
android:id="#+id/movie_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
And this is the related part of the code :
private static boolean mTwoPane;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.movie_detail_container) != null) {
mTwoPane = true;
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_main, new PlaceholderFragment())
.commit();
}else{
mTwoPane = false;
}
}
}
.....
.....
.....
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
setHasOptionsMenu(true);
// Method to set the views controls
initComponents(rootView);
return rootView;
And when I rotate the device the app crashes and gives me the following error :
08-10 21:22:34.842: E/AndroidRuntime(4338): FATAL EXCEPTION: main
08-10 21:22:34.842: E/AndroidRuntime(4338): Process: com.morxander.popularmovies, PID: 4338
08-10 21:22:34.842: E/AndroidRuntime(4338): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.morxander.popularmovies/com.morxander.popularmovies.MainActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3912)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.ActivityThread.access$900(ActivityThread.java:144)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.os.Handler.dispatchMessage(Handler.java:102)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.os.Looper.loop(Looper.java:135)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.ActivityThread.main(ActivityThread.java:5221)
08-10 21:22:34.842: E/AndroidRuntime(4338): at java.lang.reflect.Method.invoke(Native Method)
08-10 21:22:34.842: E/AndroidRuntime(4338): at java.lang.reflect.Method.invoke(Method.java:372)
08-10 21:22:34.842: E/AndroidRuntime(4338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
08-10 21:22:34.842: E/AndroidRuntime(4338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
08-10 21:22:34.842: E/AndroidRuntime(4338): Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
08-10 21:22:34.842: E/AndroidRuntime(4338): at com.morxander.popularmovies.MainActivity.onCreate(MainActivity.java:38)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.Activity.performCreate(Activity.java:5937)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
08-10 21:22:34.842: E/AndroidRuntime(4338): ... 11 more
08-10 21:22:34.842: E/AndroidRuntime(4338): Caused by: java.lang.IllegalStateException: Fragment com.morxander.popularmovies.MainActivity$PlaceholderFragment did not create a view.
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2199)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:543)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
08-10 21:22:34.842: E/AndroidRuntime(4338): ... 21 more
}
I tried many solutions but nothing is working for me.
Try to check the layout passed in the statement
View rootView = inflater.inflate(R.layout.fragment_main, container, false);

coding android Accelerometer in class

I'm writing an app that show accelerometer readings, I was able yesterday to write all the accelerometer code in the main activity and it works fine on emulator and my device.
But today I was trying to make a function that contains all the accelerometer code and return the three integers X,Y,Z in integer array, so I make a code that I think it`s right but every time I run the project the emulator gives an error "Unfortunately app-name has stopped" .
So I wish any help , please .
LogCat:
04-27 20:39:27.098: W/dalvikvm(812): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
04-27 20:39:27.118: E/AndroidRuntime(812): FATAL EXCEPTION: main
04-27 20:39:27.118: E/AndroidRuntime(812): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.accelerometer_sensor/com.example.accelerometer_sensor.MainActivity}: java.lang.NullPointerException
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.os.Looper.loop(Looper.java:137)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-27 20:39:27.118: E/AndroidRuntime(812): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 20:39:27.118: E/AndroidRuntime(812): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 20:39:27.118: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-27 20:39:27.118: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-27 20:39:27.118: E/AndroidRuntime(812): at dalvik.system.NativeStart.main(Native Method)
04-27 20:39:27.118: E/AndroidRuntime(812): Caused by: java.lang.NullPointerException
04-27 20:39:27.118: E/AndroidRuntime(812): at com.example.accelerometer_sensor.AccelerometerClass.AccelerometerInit(AccelerometerClass.java:20)
04-27 20:39:27.118: E/AndroidRuntime(812): at com.example.accelerometer_sensor.MainActivity.onCreate(MainActivity.java:19)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.Activity.performCreate(Activity.java:5104)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-27 20:39:27.118: E/AndroidRuntime(812): ... 11 more
04-27 20:39:30.119: I/Process(812): Sending signal. PID: 812 SIG: 9
here`s my main activity code:
package com.example.accelerometer_sensor;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
int [] AccVal = new int[3];
AccelerometerClass acc = new AccelerometerClass();
TextView acceleration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
acc = new AccelerometerClass();
AccVal = acc.AccelerometerInit();
acceleration = (TextView)findViewById(R.id.acceleration);
acceleration.setText("X: "+AccVal[0]+
"Y: "+AccVal[1]+
"Z: "+AccVal[2]);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and this is my AccelerometerClass Code ::
package com.example.accelerometer_sensor;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class AccelerometerClass implements SensorEventListener
{
private static Context context;
int [] Vals = new int[3];
public int [] AccelerometerInit()
{
Sensor accelerometer;
SensorManager sm;
sm = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
return Vals;
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
Vals[0]=(int)event.values[0];
Vals[1]=(int)event.values[1];
Vals[2]=(int)event.values[2];
}
}
Thanks .
Your context in AccelerometerClass is never set!
Possible solution:
public class AccelerometerClass implements SensorEventListener {
int[] Vals = new int[3];
public int[] AccelerometerInit(Context context) {
SensorManager sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
return Vals;
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
#Override
public void onSensorChanged(SensorEvent event) {
Vals[0] = (int) event.values[0];
Vals[1] = (int) event.values[1];
Vals[2] = (int) event.values[2];
}
}
in your Activity use
AccelerometerClass ac = new AccelerometerClass();
ac.AccelerometerInit(this);

Force close after clear memory and resume app

When I test my app, after close app and clean memory(in my case, CleanMaster app) and then reopen it again, it force close. I try search for cause but can't find solvable solution.
It work all fine if not clean memory. I wonder if anything need to be saved and restore after these incident. I'm quite newby for Android.
here is log
06-29 11:59:19.479 22283-22283/com.thaifasttel D/WebView﹕ onTrimMemory: 20
06-29 11:59:33.985 22524-22524/com.thaifasttel D/jdwp﹕ sendBufferedRequest : len=0x39
06-29 11:59:33.993 22524-22524/com.thaifasttel W/asset﹕ AssetManager-->addDefaultAssets CIP path not exsit!
06-29 11:59:34.050 22524-22524/com.thaifasttel D/dalvikvm﹕ newInstance failed: no <init>()
06-29 11:59:34.052 22524-22524/com.thaifasttel D/AndroidRuntime﹕ Shutting down VM
06-29 11:59:34.052 22524-22524/com.thaifasttel W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40d6f9a8)
06-29 11:59:34.060 22524-22524/com.thaifasttel E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thaifasttel/com.thaifasttel.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.thaifasttel.MainActivity$PlaceholderFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
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:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.thaifasttel.MainActivity$PlaceholderFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:413)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1801)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:213)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:97)
at com.thaifasttel.MainActivity.onCreate(MainActivity.java:73)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            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:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.InstantiationException: can't instantiate class com.thaifasttel.MainActivity$PlaceholderFragment; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
            at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
            at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1801)
            at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:213)
            at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:97)
            at com.thaifasttel.MainActivity.onCreate(MainActivity.java:73)
            at android.app.Activity.performCreate(Activity.java:5122)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            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:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
MainActivity.java
public class MainActivity extends ActionBarActivity {
#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();
return id == R.id.action_settings || super.onOptionsItemSelected(item);
}
public class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e("My App", "onCreateView");
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
WebView myWebView;
RelativeLayout splash_view;
TextView splash_text;
SharedPreferences Usettings;
String lang="th";
int isloadwa=0;
AssetManager assetPath = getAssets();
String waAssetPath ="web";
String wabase=getApplicationContext().getFilesDir().getPath();
String waIntPath = "web";
String waIntPath2 = wabase+"/"+waIntPath;
String baseupdatefolder="http://www.test.com/test/webapp";
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("My App", "onActivityCreated");
myWebView = (WebView) getView().findViewById(R.id.webview);
splash_view = (RelativeLayout) getView().findViewById(R.id.splashview);
splash_text = (TextView) getView().findViewById(R.id.splash_status);
inituserPref();
copydir();
}
#Override
public void onStart(){
super.onStart();
Log.e("My App", "onstart");
}
#Override
public void onResume(){
super.onResume();
Log.e("My App", "onResume");
if(isloadwa==1) {
if (isConnected()) {
Log.e("My App", "check update in app");
splash_text.setText(a_check_update);
checkupdate();
}
}
}
// do a lot of method after this

implements OnScrollListener inside listFragment

I implemented OnScrollListener interface in ListFragment, and i want to change text when last element of list is visible but it doesn't work. I didn't find example of similar problem(OnScrollListener inside ListFragment). My example:
public class MyListFragment1 extends ListFragment implements OnScrollListener {
public View view;
public class MyListAdapter extends ArrayAdapter<String> {
Context myContext;
List<String> lista;
public MyListAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
myContext = context;
lista = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
LayoutInflater inflater =
(LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.row, parent, false);
TextView label=(TextView)row.findViewById(R.id.month);
label.setText(lista.get(position));
ImageView icon=(ImageView)row.findViewById(R.id.icon);
//Customize your icon here
icon.setImageResource(R.drawable.ic_launcher);
return row;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
ListAdapter myListAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
month);
setListAdapter(myListAdapter);
*/
List<String> lista = new ArrayList<String>();
lista.add("January");
lista.add("February");
lista.add("March");
lista.add("April");
lista.add("May");
lista.add("June");
lista.add("July");
lista.add("August");
lista.add("September");
lista.add("October");
lista.add("November");
lista.add("December");
MyListAdapter myListAdapter =
new MyListAdapter(getActivity(), R.layout.row, lista);
setListAdapter(myListAdapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.listfragment1, container, false);
changeText("jestem tutaj");
return view;
}
private void changeText(String txt){
TextView nowy = (TextView)view.findViewById(R.id.article);
nowy.setText(txt);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(
getActivity(),
getListView().getItemAtPosition(position).toString(),
Toast.LENGTH_LONG).show();
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (++firstVisibleItem + visibleItemCount > totalItemCount) {
changeText("something");
}
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
I don't have any error, it just doesn't work ;)
P.S implementation of onScroll method i found in StackOverflow.
EDIT:/ adding logcat:
10-14 01:11:34.944: E/AndroidRuntime(817): FATAL EXCEPTION: main
10-14 01:11:34.944: E/AndroidRuntime(817): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exercise.AndroidListFragment/com.exercise.AndroidListFragment.AndroidListFragmentActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.os.Looper.loop(Looper.java:137)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-14 01:11:34.944: E/AndroidRuntime(817): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 01:11:34.944: E/AndroidRuntime(817): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-14 01:11:34.944: E/AndroidRuntime(817): at dalvik.system.NativeStart.main(Native Method)
10-14 01:11:34.944: E/AndroidRuntime(817): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Activity.setContentView(Activity.java:1881)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.exercise.AndroidListFragment.AndroidListFragmentActivity.onCreate(AndroidListFragmentActivity.java:11)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Activity.performCreate(Activity.java:5104)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-14 01:11:34.944: E/AndroidRuntime(817): ... 11 more
10-14 01:11:34.944: E/AndroidRuntime(817): Caused by: java.lang.IllegalStateException: Content view not yet created
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ListFragment.ensureList(ListFragment.java:386)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.ListFragment.getListView(ListFragment.java:280)
10-14 01:11:34.944: E/AndroidRuntime(817): at com.exercise.AndroidListFragment.MyListFragment1.onCreateView(MyListFragment1.java:93)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Fragment.performCreateView(Fragment.java:1695)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.app.Activity.onCreateView(Activity.java:4717)
10-14 01:11:34.944: E/AndroidRuntime(817): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
10-14 01:11:34.944: E/AndroidRuntime(817): ... 21 more
EDIT 2: I have by tried findViewById but it doesn't work too....
You have implemented the OnScrollListener, but you havent attached it to your ListView.
getListView().setOnScrollListener(this);
add this to your onCreateView Method.
You have this error because you are trying to access your Listview when this one is not created yet.
Caused by: java.lang.IllegalStateException: Content view not yet
created
Your Listview is actually created by the last line of your onCreateView method, at the line which usually look something like this:
return inflater.inflate(R.layout.fragment_list, container, false);
The solution is to access your list from the onActivityCreated method, which is executed just after the onCreateView method.
#Override
public void onActivityCreated (Bundle savedInstanceState){
// Always call the superclass so it can save the view hierarchy state
super.onCreate(savedInstanceState);
getListView().setOnScrollListener(this);
}

Android App crashes right on start

I have an app that's supposed to get some string from a server depending on the webpage, and it crashes every time I run it. I don't understand how to even debug it, and I've tried numerous changes. It started crashing ever since I messed with the GUI, and added the ability to switch views if that provides any sort of help.
Here is the code:
public class MainActivity extends Activity implements TextToSpeech.OnInitListener, OnClickListener{
private WebView mWebview;
EditText addressBar;
String currentWebpage = "http://www.aljazeera.com/news/americas/2013/07/20137113200544375.html";
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.linearview);
View main = (View) findViewById(R.layout.activity_main);
View readOut = (View) findViewById(R.layout.read_out);
#Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("Showing Activity_Main...");
viewGroup.addView(View.inflate(this, R.layout.activity_main, null));
super.onCreate(savedInstanceState);
//setContentView(R.menu.main);
addressBar = (EditText)findViewById(R.id.addressBar);
addressBar.setText(currentWebpage);
mWebview = (WebView)findViewById(R.id.webview);
mWebview.getSettings().setJavaScriptEnabled(true); // enables javascript
mWebview.setWebViewClient(new WebViewClient());
System.out.println("Loading Webpage...");
mWebview.loadUrl(currentWebpage);
}
public void speakOut(String text) {
TextToSpeech tts = new TextToSpeech(this, this);
System.out.println("Speaking");
if(tts.isLanguageAvailable(Locale.ENGLISH) != -1){
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
public int speakFull(String text){
switchToRead();
String[] sentences = text.split("\n|\\.(?!\\d)|(?<!\\d)\\."); // Regex that splits the body of text into the sentences of that body which are stored in a String array.
for(int i = 0; i < sentences.length; i++){
speakOut(sentences[i]);
if(i == sentences.length - 1){
return 1;
}
}
return 0;
}
public String getText(String webPage) throws ParseException, IOException{
HttpResponse response = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://someserver.net:8080/" + webPage));
response = client.execute(request);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String responseBody = "No text found on webpage.";
int responseCode = response.getStatusLine().getStatusCode();
switch(responseCode) {
case 200:
HttpEntity entity = response.getEntity();
if(entity != null) {
responseBody = EntityUtils.toString(entity);
}
}
System.out.println("Returning Response..");
System.out.println(responseBody);
return responseBody;
}
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
}
private class tts extends AsyncTask<String, Void, String>{//Async http request to get text
#Override
protected String doInBackground(String... arg0) {
try {
System.out.println("Running seperate thread for TTS.");
int complete = 0;
while(complete == 0){
System.out.println("Speaking full..");
complete = speakFull(getText(mWebview.getUrl()));
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result){
}
}
public void clickPlay(View v){
new tts().execute("");
}
public void clickGo(View v){
if(addressBar.getText() != null){
currentWebpage = addressBar.getText().toString();
System.out.println("Current webpage changed to: " + currentWebpage);
mWebview.loadUrl(currentWebpage);
}
}
public void clickPause(View v){
System.out.println("Clicked pause.");
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
public void switchToRead(){// Switches to the reading view which displays the text that the tts engine reads off.
System.out.println("Swtiching view to Read.");
viewGroup.removeView(main);
viewGroup.addView(View.inflate(this, R.layout.read_out, null));
}
public void switchToMain(){
System.out.println("Switching view to Main.");
viewGroup.removeView(readOut);
viewGroup.addView(View.inflate(this, R.layout.activity_main, null));
}
}
Also here are the numerous errors I encounter when launching my app:
08-01 14:53:10.210: E/Trace(812): error opening trace file: No such file or directory (2)
08-01 14:53:10.600: D/AndroidRuntime(812): Shutting down VM
08-01 14:53:10.631: W/dalvikvm(812): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-01 14:53:10.660: E/AndroidRuntime(812): FATAL EXCEPTION: main
08-01 14:53:10.660: E/AndroidRuntime(812): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.webview/com.example.webview.MainActivity}: java.lang.NullPointerException
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.os.Looper.loop(Looper.java:137)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 14:53:10.660: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-01 14:53:10.660: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-01 14:53:10.660: E/AndroidRuntime(812): at dalvik.system.NativeStart.main(Native Method)
08-01 14:53:10.660: E/AndroidRuntime(812): Caused by: java.lang.NullPointerException
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.Activity.findViewById(Activity.java:1839)
08-01 14:53:10.660: E/AndroidRuntime(812): at com.example.webview.MainActivity.<init>(MainActivity.java:39)
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.Class.newInstanceImpl(Native Method)
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.Class.newInstance(Class.java:1319)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
08-01 14:53:10.660: E/AndroidRuntime(812): ... 11 more
08-01 14:53:27.439: D/AndroidRuntime(860): Shutting down VM
08-01 14:53:27.439: W/dalvikvm(860): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-01 14:53:27.449: E/AndroidRuntime(860): FATAL EXCEPTION: main
08-01 14:53:27.449: E/AndroidRuntime(860): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.webview/com.example.webview.MainActivity}: java.lang.NullPointerException
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.os.Looper.loop(Looper.java:137)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 14:53:27.449: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-01 14:53:27.449: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-01 14:53:27.449: E/AndroidRuntime(860): at dalvik.system.NativeStart.main(Native Method)
08-01 14:53:27.449: E/AndroidRuntime(860): Caused by: java.lang.NullPointerException
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.Activity.findViewById(Activity.java:1839)
08-01 14:53:27.449: E/AndroidRuntime(860): at com.example.webview.MainActivity.<init>(MainActivity.java:39)
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.Class.newInstanceImpl(Native Method)
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.Class.newInstance(Class.java:1319)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
08-01 14:53:27.449: E/AndroidRuntime(860): ... 11 more
Move your view initialization inside onCreate after setContentView
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.linearview);
...// rest of the code

Categories