Twitter search method using Twitter4J - java

I am doing an application where u write a word, a new activity opens up then all the tweets SHOULD show up, but i'm getting an error.
I am calling the method printTweets inside the main of the new activity:
And don't worry all my credentials work, i have tried the code on a java application ( not android) and it worked fine and I was getting results.
This is my code:
public void printTweets(String q) {
LinearLayout layout = new LinearLayout(this);
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("xxxxx")
.setOAuthConsumerSecret("xxxxxx")
.setOAuthAccessToken("xxxxxx")
.setOAuthAccessTokenSecret("xxxxx");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
Query query = new Query(q);
QueryResult result = null;
do{
try {
result = twitter.search(query);
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
TextView tv=new TextView(getApplicationContext());
tv.setText("#" + tweet.getUser().getScreenName() + ":" + tweet.getText());
layout.addView(tv);
}
} while ((query = result.nextQuery()) != null);
}
This is my error:
11-15 01:07:30.890: E/AndroidRuntime(12926): FATAL EXCEPTION: main
11-15 01:07:30.890: E/AndroidRuntime(12926): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sentimentmining/com.example.sentimentmining.DisplayResults}: android.os.NetworkOnMainThreadException
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.ActivityThread.access$600(ActivityThread.java:128)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.os.Looper.loop(Looper.java:137)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.ActivityThread.main(ActivityThread.java:4514)
11-15 01:07:30.890: E/AndroidRuntime(12926): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 01:07:30.890: E/AndroidRuntime(12926): at java.lang.reflect.Method.invoke(Method.java:511)
11-15 01:07:30.890: E/AndroidRuntime(12926): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-15 01:07:30.890: E/AndroidRuntime(12926): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-15 01:07:30.890: E/AndroidRuntime(12926): at dalvik.system.NativeStart.main(Native Method)
11-15 01:07:30.890: E/AndroidRuntime(12926): Caused by: android.os.NetworkOnMainThreadException
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
11-15 01:07:30.890: E/AndroidRuntime(12926): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
11-15 01:07:30.890: E/AndroidRuntime(12926): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
11-15 01:07:30.890: E/AndroidRuntime(12926): at java.net.InetAddress.getAllByName(InetAddress.java:220)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpConnection.(HttpConnection.java:71)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpConnection.(HttpConnection.java:50)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:133)
11-15 01:07:30.890: E/AndroidRuntime(12926): at twitter4j.internal.http.HttpResponseImpl.(HttpResponseImpl.java:34)
11-15 01:07:30.890: E/AndroidRuntime(12926): at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:141)
11-15 01:07:30.890: E/AndroidRuntime(12926): at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
11-15 01:07:30.890: E/AndroidRuntime(12926): at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:81)
11-15 01:07:30.890: E/AndroidRuntime(12926): at twitter4j.TwitterImpl.get(TwitterImpl.java:1929)
11-15 01:07:30.890: E/AndroidRuntime(12926): at twitter4j.TwitterImpl.search(TwitterImpl.java:306)
11-15 01:07:30.890: E/AndroidRuntime(12926): at com.example.sentimentmining.DisplayResults.printTweets(DisplayResults.java:83)
11-15 01:07:30.890: E/AndroidRuntime(12926): at com.example.sentimentmining.DisplayResults.onCreate(DisplayResults.java:34)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.Activity.performCreate(Activity.java:4465)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
11-15 01:07:30.890: E/AndroidRuntime(12926): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
11-15 01:07:30.890: E/AndroidRuntime(12926): ... 11 more
If you need anything more please tell me and I will give it to you

NetworkOnMainThreadException
set this permission in manifest
<uses-permission android:name="android.permission.INTERNET" />
Use asyntask
The reason why you are getting this exception is because you are attempting to perform an expensive operation on the UI thread, which can significantly slow your app down and cause it to force close. You should wrap your code in an AsyncTask (or Thread) instead.
or
use this before doing network operations
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}

Related

android app unfortunately has stopped

I had tired to build an android application and I face a problem.
I post my java code, xml and error.
Can someone teach me why it happened and how to solve it? Thanks!
This is what the problem that I face...
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: FATAL EXCEPTION: main
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: Process: com.onemap.activities, PID: 21798
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.onemap.activities/com.onemap.activities.OneMap}: android.view.InflateException: Binary XML file line #7: Error inflating class android.widget.TextView
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:156)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.os.Looper.loop(Looper.java:157)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5872)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.widget.TextView
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.view.LayoutInflater.createView(LayoutInflater.java:620)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at com.onemap.fragments.MainMenu.onCreateView(MainMenu.java:88)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.support.v4.app.Fragment.performCreateView(Fragment.java:1965)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1047)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1237)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1339)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2295)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:375)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.app.Activity.setContentView(Activity.java:1997)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at com.onemap.activities.OneMap.onCreate(OneMap.java:58)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5312)
10-27 16:35:22.488 21798-21798/com.onemap.activities E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
Here is my java code and xml
OneMap.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.onemap);
AppUtilities.init(this);
pomPref = getSharedPreferences(POM_PREF, MODE_PRIVATE);
pomEditor = pomPref.edit();
favDB = new FavouritesDBHandler(AppUtilities.getContext());
this.mMapView = (MapView) findViewById(R.id.mMapView);
this.mMapView.setOnStatusChangedListener(OneMapStatusListener.getInstance());
this.scrollView = (SlidingMenu) findViewById(R.id.mScrollView);
View mainContent = this.scrollView.getMainContent();
OnClickListener clickListener = getClickListener();
OnLongClickListener longClickListener = getLongClickListener();
mainContent.findViewById(R.id.mScreenToggle).setOnClickListener(clickListener);
JewelContainer jewelContainer = (JewelContainer) mainContent.findViewById(R.id.jewel_container);
jewelContainer.setOnClickListener(clickListener);
jewelContainer.setOnLongClickListener(longClickListener);
this.mMapView.setOnSingleTapListener(getSingleTapListener());
this.mMapView.setOnLongPressListener(getLongPressListener());
this.mMapView.setCalloutClickListener(getClickListener());
this.mMapView.getCallout().setMaxWidth(550);
}
MainMenu.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.menu_main, container, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.arrowUp = (ImageView) view.findViewById(R.id.iv_arrow_up);
viewHolder.arrowDown = (ImageView) view.findViewById(R.id.iv_arrow_down);
ListView menuList = (ListView) view.findViewById(R.id.menu_list);
menuList.setOnItemClickListener(this);
menuList.setOnScrollListener(this);
menuList.setTag(viewHolder);
MenuItem[] arrayMenuItem = new MenuItem[5];
arrayMenuItem[0] = new MenuItem(0, "Services", R.mipmap.ic_menu_title_service);
arrayMenuItem[1] = new MenuItem(1, getString(R.string.get_directions), R.mipmap.ic_directions);
arrayMenuItem[2] = new MenuItem(2, getString(R.string.amenities), R.mipmap.ic_amenities);
arrayMenuItem[3] = new MenuItem(3, "Others", R.mipmap.ic_menu_title_other);
arrayMenuItem[4] = new MenuItem(4, getString(R.string.about), R.mipmap.ic_about);
menuList.setAdapter(new MenuAdapter(getActivity(), arrayMenuItem));
return view;
}
onemap.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.onemap.widgets.MapView
xmlns:callout="http://schemas.android.com/apk/res/com.onemap.activities"
android:id="#id/mMapView"
calloutStyle="#xml/callout_style"
initExtent="29454.233386372267 39831.55546813806 30038.01821247406 40758.313879574656"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
callout:layout="#layout/callout" />
<ProgressBar
android:id="#id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
<com.onemap.widgets.SlidingMenu
xmlns:menu="http://schemas.android.com/apk/res/com.onemap.activities"
android:id="#id/mScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
menu:main_content="#id/mainContainer"
menu:menu_content="#id/menuContainer"
menu:menu_handle="#id/handle"
menu:menu_marginRight="#dimen/menu_margin_r">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment
android:id="#id/menuContainer"
class="com.onemap.fragments.MainMenu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:tag="MainMenu"
tools:layout="#layout/menu_main" />
<include
android:id="#id/mainContainer"
layout="#layout/widgets" />
</LinearLayout>
</com.onemap.widgets.SlidingMenu>
</RelativeLayout>
menu_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#id/btnSearch"
style="#android:style/TextAppearance.Widget.EditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/menu_item_margin"
android:background="#drawable/selector_edittext_search"
android:clickable="true"
android:drawableLeft="#mipmap/ic_search"
android:drawablePadding="#dimen/menu_item_icon_padding"
android:gravity="center_vertical"
android:text="#string/search"
android:textColor="#color/selector_edittext_search_text" />
<LinearLayout
android:id="#id/listFooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="#dimen/menu_item_margin"
android:gravity="right">
<ImageView
android:id="#id/iv_arrow_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:paddingRight="#dimen/menu_item_icon_padding"
android:src="#mipmap/ic_arrow_up_disabled" />
<ImageView
android:id="#id/iv_arrow_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:paddingLeft="#dimen/menu_item_icon_padding"
android:paddingRight="#dimen/menu_item_icon_padding"
android:src="#mipmap/ic_arrow_down_enabled" />
</LinearLayout>
<ListView
android:id="#id/menu_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/listFooter"
android:layout_below="#id/btnSearch"
android:divider="#null"
android:dividerHeight="0.0dip"
android:fadingEdgeLength="0.0dip" />
</RelativeLayout>
set #+id instead of #id.
android:id="#+id/btnSearch"
for more take it as a reference

NumberFormatException invalid int ""

I am trying to take the input values from EditText and I want to save it to sqlite Database. I dont know how to use the logcat [also please explain how can I read the errors from the LogCat].
MainActivity.java:
package com.example.database;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity{
EditText first,last,age,classc;
Button add,view;
String FirstName;
String LastName,Class;
Integer Age;
sqLit myDB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDB = new sqLit(this);
Link();
xmlToVar();
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Integer flag=myDB.insertValue(FirstName, LastName, Class, Age);
if (flag==1)
{
Context context=getApplicationContext();
Toast toast = Toast.makeText(context, "Record Added" , Toast.LENGTH_LONG);
toast.show();
}
else
{
Context context=getApplicationContext();
Toast toast = Toast.makeText(context, "Error Occured" , Toast.LENGTH_LONG);
toast.show();
}
}
});
}
public void Link()
{
first=(EditText) findViewById(R.id.editFirst);
last=(EditText) findViewById(R.id.editLast);
classc=(EditText) findViewById(R.id.editClass);
age=(EditText) findViewById(R.id.editAge);
add=(Button) findViewById(R.id.Add);
view=(Button) findViewById(R.id.ViewAll);
}
public void xmlToVar()
{
FirstName = first.getText().toString();
LastName = last.getText().toString();
Class = classc.getText().toString();
Age = Integer.parseInt(age.getText().toString());
}
}
sqLit.java:
package com.example.database;
import org.w3c.dom.Text;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class sqLit extends SQLiteOpenHelper
{
public static final String DB_NAME="student12.db";
public static final String TABLE_NAME="class1";
public static final String COL_1="ROLL NO";
public static final String COL_2="First Name";
public static final String COL_3="Last Name";
public static final String COL_4="Class";
public static final String COL_5="Age";
public sqLit(Context context) {
super(context, DB_NAME, null, 1);
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("Create table " + TABLE_NAME + "(" + COL_1 + "INTEGER AUTOINCREMENT PRIMARY KEY," + COL_2 + "TEXT," + COL_3 + "TEXT," + COL_4 + "TEXT," + COL_5 + "INTEGER)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("Drop Table If Exist" + TABLE_NAME );
onCreate(db);
}
public Integer insertValue(String FirstName, String LastName, String Class, Integer Age)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentV = new ContentValues();
contentV.put(COL_2, FirstName);
contentV.put(COL_3, LastName);
contentV.put(COL_4, Class);
contentV.put(COL_5, Age);
long isInserted = db.insert(TABLE_NAME, null, contentV);
if (isInserted == -1){
return 0;
}
else
{
return 1;
}
}
}
Also, please explain how to use LogCat as I don't understand how to read it.
LOGCAT:
06-11 18:44:39.650: E/Trace(29536): error opening trace file: No such file or directory (2)
06-11 18:44:39.660: D/AndroidRuntime(29536): Shutting down VM
06-11 18:44:39.660: W/dalvikvm(29536): threadid=1: thread exiting with uncaught exception (group=0xb3f2b288)
06-11 18:44:39.660: E/AndroidRuntime(29536): FATAL EXCEPTION: main
06-11 18:44:39.660: E/AndroidRuntime(29536): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.os.Looper.loop(Looper.java:137)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 18:44:39.660: E/AndroidRuntime(29536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 18:44:39.660: E/AndroidRuntime(29536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 18:44:39.660: E/AndroidRuntime(29536): at dalvik.system.NativeStart.main(Native Method)
06-11 18:44:39.660: E/AndroidRuntime(29536): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.Integer.invalidInt(Integer.java:138)
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.Integer.parseInt(Integer.java:359)
06-11 18:44:39.660: E/AndroidRuntime(29536): at java.lang.Integer.parseInt(Integer.java:332)
06-11 18:44:39.660: E/AndroidRuntime(29536): at com.example.database.MainActivity.xmlToVar(MainActivity.java:72)
06-11 18:44:39.660: E/AndroidRuntime(29536): at com.example.database.MainActivity.onCreate(MainActivity.java:34)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.Activity.performCreate(Activity.java:5008)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-11 18:44:39.660: E/AndroidRuntime(29536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-11 18:44:39.660: E/AndroidRuntime(29536): ... 11 more
06-11 18:46:03.652: D/dalvikvm(30256): Not late-enabling CheckJNI (already on)
06-11 18:46:03.682: E/Trace(30256): error opening trace file: No such file or directory (2)
06-11 18:46:03.712: D/AndroidRuntime(30256): Shutting down VM
06-11 18:46:03.712: W/dalvikvm(30256): threadid=1: thread exiting with uncaught exception (group=0xb3f2b288)
06-11 18:46:03.712: E/AndroidRuntime(30256): FATAL EXCEPTION: main
06-11 18:46:03.712: E/AndroidRuntime(30256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.os.Looper.loop(Looper.java:137)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 18:46:03.712: E/AndroidRuntime(30256): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 18:46:03.712: E/AndroidRuntime(30256): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 18:46:03.712: E/AndroidRuntime(30256): at dalvik.system.NativeStart.main(Native Method)
06-11 18:46:03.712: E/AndroidRuntime(30256): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.Integer.invalidInt(Integer.java:138)
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.Integer.parseInt(Integer.java:359)
06-11 18:46:03.712: E/AndroidRuntime(30256): at java.lang.Integer.parseInt(Integer.java:332)
06-11 18:46:03.712: E/AndroidRuntime(30256): at com.example.database.MainActivity.xmlToVar(MainActivity.java:73)
06-11 18:46:03.712: E/AndroidRuntime(30256): at com.example.database.MainActivity.onCreate(MainActivity.java:35)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.Activity.performCreate(Activity.java:5008)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-11 18:46:03.712: E/AndroidRuntime(30256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-11 18:46:03.712: E/AndroidRuntime(30256): ... 11 more
06-11 18:53:33.378: E/Trace(2383): error opening trace file: No such file or directory (2)
06-11 18:53:33.408: D/AndroidRuntime(2383): Shutting down VM
06-11 18:53:33.408: W/dalvikvm(2383): threadid=1: thread exiting with uncaught exception (group=0xb3f2b288)
06-11 18:53:33.408: E/AndroidRuntime(2383): FATAL EXCEPTION: main
06-11 18:53:33.408: E/AndroidRuntime(2383): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.os.Looper.loop(Looper.java:137)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 18:53:33.408: E/AndroidRuntime(2383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 18:53:33.408: E/AndroidRuntime(2383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 18:53:33.408: E/AndroidRuntime(2383): at dalvik.system.NativeStart.main(Native Method)
06-11 18:53:33.408: E/AndroidRuntime(2383): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.Integer.invalidInt(Integer.java:138)
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.Integer.parseInt(Integer.java:359)
06-11 18:53:33.408: E/AndroidRuntime(2383): at java.lang.Integer.parseInt(Integer.java:332)
06-11 18:53:33.408: E/AndroidRuntime(2383): at com.example.database.MainActivity.xmlToVar(MainActivity.java:73)
06-11 18:53:33.408: E/AndroidRuntime(2383): at com.example.database.MainActivity.onCreate(MainActivity.java:35)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.Activity.performCreate(Activity.java:5008)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-11 18:53:33.408: E/AndroidRuntime(2383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-11 18:53:33.408: E/AndroidRuntime(2383): ... 11 more
Caused by: java.lang.NumberFormatException: Invalid int: ""
(after FATAL EXCEPTION:) tells us you are trying to parse an int but the value you are trying to parse is an empty String.
this line (the first that references your class) tells us which line it was
at com.example.database.MainActivity.xmlToVar(MainActivity.java:72)
this is happening because you are calling it in onCreate() before the EditText has any value entered.
From the line above, we can see that the error happens in MainActivity in the method xmlToVar at line 72 which should be
Age = Integer.parseInt(age.getText().toString());
To solve this, you should call that method in an onClick or some event listener. You also should surround it in a try/catch or use some other validation checking.
You should read the following questions and their answers for more details:
What is a stack trace, and how can I use it to debug my application errors?
Unfortunately MyApp has stopped. How can I solve this?
java.lang.NumberFormatException: Invalid int: "" in android
basically, read the error it gives you then look for the first line which mentions your class. Sometimes you need to read deeper but for basic errors that is usually enough.
Also Please Explain how to use LogCat as I am 3 days Old Android Programmer. I never used Eclipse.
Note that the stacktrace isn't Eclipse or even Java/Android specific. You can get some sort of crash log like this in other languages/IDEs/Consoles
The problem is here:
Age = Integer.parseInt(age.getText().toString());
age.getText().toString() equals "" and which causes the NumberFormatException

How can I implent ArrayLists to a Spinner?

Help me anything is wrong about the whole thing.
I want to make a Spinner with the ArrayLists but it simply does not work!
I think it has something to do with the 'protected void onCreate' part.
I have tried so hard but I allways get the same error.
My Main Class:
package com.example.hvt;
import java.util.ArrayList;
import com.example.hvt.Klassenvertretung.Bezeichnung;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener {
public Spinner news, klassen;
public Button btnnews, btnklassen, btnzurueck1, btnzurueck2;
Bezeichnung bezeichnung;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> Bezeichnung = new ArrayList<String>();
String listString = "";
for (String s : Bezeichnung)
{
listString += s + "\t";
}
int listInt;
listInt = Integer.parseInt(listString);
Spinner klassen;
ArrayAdapter<String> klassenadapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item);
klassen = (Spinner) findViewById(R.id.klassenspinner);
bezeichnung.Hinzufügen(bezeichnung);
ArrayAdapter<String> klassen_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listInt);
klassen_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
klassen.setAdapter(klassenadapter);
klassen.setSelection(0);
klassen.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View v, int pos,
long id) {
if (pos == 1){
setContentView(R.layout.activity_5aklassen);
}
if (pos <= 1 ){
Toast.makeText(parent.getContext(),
"Deine Auswahl ist: " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
An Class in the same package(this Class picks information form the Internet| it should work because I have tested it in normal JAVA)
package com.example.hvt;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class Vertretungsplan {
public Vertretungsplan(String woche)
{
Woche = woche;
Einlesen(woche);
}
public String Woche;
public List<Klassenvertretung> Klassen = new ArrayList<Klassenvertretung>();
private void Hinzufügen(Klassenvertretung neuesElement)
{
Klassen.add(neuesElement);
}
private void Einlesen(String woche)
{
try
{
for (int webseite = 1; webseite < 10000; webseite++)
{
Klassenvertretung klassenvertretung = new Klassenvertretung();
String teilseite = "w000";
if (webseite < 10)
teilseite = teilseite + "0";
teilseite = teilseite + webseite;
Document doc = Jsoup.connect("http://www.hh.shuttle.de/hh/gyhr/Vertretungsplan/Vertretungsplan_Internet/" + woche + "/w/" + teilseite + ".htm").get();
Element h2 = doc.select("h2").get(0);
klassenvertretung.Bezeichnung = h2.text();
Element table = doc.select("table").get(1);
Element[] elemente = table.select("tr").toArray(new Element[0]);
for (int i = 1; i < elemente.length; i++)
{
Element[] tds = elemente[i].select("td").toArray(new Element[0]);
Vertretung vertretung = new Vertretung();
vertretung.Klasse = tds[0].text();
vertretung.Stunde = tds[1].text();
vertretung.Art = tds[2].text();
vertretung.Fach = tds[3].text();
vertretung.Raum = tds[4].text();
vertretung.stattFach = tds[5].text();
vertretung.stattRaum = tds[6].text();
vertretung.Informationen = tds[7].text();
klassenvertretung.Hinzufügen(vertretung);
}
Hinzufügen(klassenvertretung);
}
}
catch (IOException io)
{
}
finally
{
}
}
}
Another Class in the same package(this code should combine all the info too two ArrayLists (tested it in normal JAVA as well so there shouldn't be a mistake))
package com.example.hvt;
import java.util.ArrayList;
import java.util.List;
public class Klassenvertretung
{
public String Bezeichnung;
public static class Bezeichnung {
public List<Bezeichnung> Bezeichnung = new ArrayList<Bezeichnung>();
public void Hinzufügen(Bezeichnung neuesElement)
{
Bezeichnung.add(neuesElement);
}
}
public List<Vertretung> Vertretungen = new ArrayList<Vertretung>();
public void Hinzufügen(Vertretung neuesElement)
{
Vertretungen.add(neuesElement);
}
}
My last Class: (as you can see this is just a class where I defined some Object(they belong to the Class above))
package com.example.hvt;
public class Vertretung
{
public String Klasse;
public String Stunde;
public String Art;
public String Fach;
public String Raum;
public String stattFach;
public String stattRaum;
public String Informationen;
}
The Chat-Log:
11-15 00:26:26.160: I/dalvikvm(714): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
11-15 00:26:26.160: W/dalvikvm(714): VFY: unable to resolve virtual method 11377: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
11-15 00:26:26.160: D/dalvikvm(714): VFY: replacing opcode 0x6f at 0x0000
11-15 00:26:26.160: I/dalvikvm(714): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
11-15 00:26:26.160: W/dalvikvm(714): VFY: unable to resolve virtual method 11383: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
11-15 00:26:26.160: D/dalvikvm(714): VFY: replacing opcode 0x6f at 0x0000
11-15 00:26:26.170: I/dalvikvm(714): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
11-15 00:26:26.170: W/dalvikvm(714): VFY: unable to resolve virtual method 8956: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
11-15 00:26:26.170: D/dalvikvm(714): VFY: replacing opcode 0x6e at 0x000e
11-15 00:26:26.190: I/dalvikvm(714): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
11-15 00:26:26.190: W/dalvikvm(714): VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-15 00:26:26.190: D/dalvikvm(714): VFY: replacing opcode 0x6e at 0x0002
11-15 00:26:26.190: I/dalvikvm(714): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
11-15 00:26:26.190: W/dalvikvm(714): VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
11-15 00:26:26.190: D/dalvikvm(714): VFY: replacing opcode 0x6e at 0x0002
11-15 00:26:26.320: D/AndroidRuntime(714): Shutting down VM
11-15 00:26:26.320: W/dalvikvm(714): threadid=1: thread exiting with uncaught exception (group=0xb1a90d70)
11-15 00:26:26.330: E/AndroidRuntime(714): FATAL EXCEPTION: main
11-15 00:26:26.330: E/AndroidRuntime(714): Process: com.example.hvt, PID: 714
11-15 00:26:26.330: E/AndroidRuntime(714): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hvt/com.example.hvt.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.ActivityThread.access$800(ActivityThread.java:138)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.os.Handler.dispatchMessage(Handler.java:102)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.os.Looper.loop(Looper.java:136)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.ActivityThread.main(ActivityThread.java:5026)
11-15 00:26:26.330: E/AndroidRuntime(714): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 00:26:26.330: E/AndroidRuntime(714): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 00:26:26.330: E/AndroidRuntime(714): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-15 00:26:26.330: E/AndroidRuntime(714): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
11-15 00:26:26.330: E/AndroidRuntime(714): at dalvik.system.NativeStart.main(Native Method)
11-15 00:26:26.330: E/AndroidRuntime(714): Caused by: java.lang.NumberFormatException: Invalid int: ""
11-15 00:26:26.330: E/AndroidRuntime(714): at java.lang.Integer.invalidInt(Integer.java:137)
11-15 00:26:26.330: E/AndroidRuntime(714): at java.lang.Integer.parseInt(Integer.java:358)
11-15 00:26:26.330: E/AndroidRuntime(714): at java.lang.Integer.parseInt(Integer.java:331)
11-15 00:26:26.330: E/AndroidRuntime(714): at com.example.hvt.MainActivity.onCreate(MainActivity.java:39)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.Activity.performCreate(Activity.java:5242)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-15 00:26:26.330: E/AndroidRuntime(714): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
11-15 00:26:26.330: E/AndroidRuntime(714): ... 11 more
11-15 00:26:29.320: I/Process(714): Sending signal. PID: 714 SIG: 9
11-15 00:26:38.220: I/dalvikvm(803): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
11-15 00:26:38.220: W/dalvikvm(803): VFY: unable to resolve virtual method 11377: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
11-15 00:26:38.220: D/dalvikvm(803): VFY: replacing opcode 0x6f at 0x0000
11-15 00:26:38.220: I/dalvikvm(803): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
11-15 00:26:38.220: W/dalvikvm(803): VFY: unable to resolve virtual method 11383: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
11-15 00:26:38.230: D/dalvikvm(803): VFY: replacing opcode 0x6f at 0x0000
11-15 00:26:38.250: I/dalvikvm(803): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
11-15 00:26:38.250: W/dalvikvm(803): VFY: unable to resolve virtual method 8956: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
11-15 00:26:38.250: D/dalvikvm(803): VFY: replacing opcode 0x6e at 0x000e
11-15 00:26:38.270: I/dalvikvm(803): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
11-15 00:26:38.270: W/dalvikvm(803): VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-15 00:26:38.270: D/dalvikvm(803): VFY: replacing opcode 0x6e at 0x0002
11-15 00:26:38.280: I/dalvikvm(803): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
11-15 00:26:38.280: W/dalvikvm(803): VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
11-15 00:26:38.280: D/dalvikvm(803): VFY: replacing opcode 0x6e at 0x0002
11-15 00:26:38.400: D/AndroidRuntime(803): Shutting down VM
11-15 00:26:38.400: W/dalvikvm(803): threadid=1: thread exiting with uncaught exception (group=0xb1a90d70)
11-15 00:26:38.410: E/AndroidRuntime(803): FATAL EXCEPTION: main
11-15 00:26:38.410: E/AndroidRuntime(803): Process: com.example.hvt, PID: 803
11-15 00:26:38.410: E/AndroidRuntime(803): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hvt/com.example.hvt.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.ActivityThread.access$800(ActivityThread.java:138)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.os.Handler.dispatchMessage(Handler.java:102)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.os.Looper.loop(Looper.java:136)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.ActivityThread.main(ActivityThread.java:5026)
11-15 00:26:38.410: E/AndroidRuntime(803): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 00:26:38.410: E/AndroidRuntime(803): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 00:26:38.410: E/AndroidRuntime(803): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-15 00:26:38.410: E/AndroidRuntime(803): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
11-15 00:26:38.410: E/AndroidRuntime(803): at dalvik.system.NativeStart.main(Native Method)
11-15 00:26:38.410: E/AndroidRuntime(803): Caused by: java.lang.NumberFormatException: Invalid int: ""
11-15 00:26:38.410: E/AndroidRuntime(803): at java.lang.Integer.invalidInt(Integer.java:137)
11-15 00:26:38.410: E/AndroidRuntime(803): at java.lang.Integer.parseInt(Integer.java:358)
11-15 00:26:38.410: E/AndroidRuntime(803): at java.lang.Integer.parseInt(Integer.java:331)
11-15 00:26:38.410: E/AndroidRuntime(803): at com.example.hvt.MainActivity.onCreate(MainActivity.java:39)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.Activity.performCreate(Activity.java:5242)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-15 00:26:38.410: E/AndroidRuntime(803): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
11-15 00:26:38.410: E/AndroidRuntime(803): ... 11 more
the error is bcz of "nothing there to parse!!"
you are creating the ArrayList but not assaigning or adding any values to List at all..!!
your List is empty when it reaches the for loop: so it will return runtime exception as the Logcat shows.!
ArrayList<String> Bezeichnung = new ArrayList<String>();
// populate your List before using it, otherwise it is null/"";
// need some statements like
// Bezeichnung.add("string1");
// Bezeichnung.add("string1");
String listString = "";
for (String s : Bezeichnung)
{
listString += s + "\t";
}
also have some suggestions for you,
you have to follow Java Naming Conventions to avoid mistakes by naming conflict!
make sure you named them correct before running, it may cause generating some unexpected results, which you may not figure out easily!

Error on launching activity

My application crashes whenever I launch it! whats wrong with it? I have created a database and I am trying to populate my listview from the database I have created what is the problem with the code and what is the fix for it?
02-19 22:02:07.502: D/AndroidRuntime(330): Shutting down VM
02-19 22:02:07.502: W/dalvikvm(330): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-19 22:02:07.542: E/AndroidRuntime(330): FATAL EXCEPTION: main
02-19 22:02:07.542: E/AndroidRuntime(330): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-1.apk]
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 22:02:07.542: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-19 22:02:07.542: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-19 22:02:07.542: E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)
02-19 22:02:07.542: E/AndroidRuntime(330): Caused by: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-1.apk]
02-19 22:02:07.542: E/AndroidRuntime(330): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-19 22:02:07.542: E/AndroidRuntime(330): ... 11 more
02-19 22:10:47.354: D/AndroidRuntime(364): Shutting down VM
02-19 22:10:47.444: W/dalvikvm(364): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-19 22:10:47.482: E/AndroidRuntime(364): FATAL EXCEPTION: main
02-19 22:10:47.482: E/AndroidRuntime(364): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.os.Looper.loop(Looper.java:123)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 22:10:47.482: E/AndroidRuntime(364): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-19 22:10:47.482: E/AndroidRuntime(364): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-19 22:10:47.482: E/AndroidRuntime(364): at dalvik.system.NativeStart.main(Native Method)
02-19 22:10:47.482: E/AndroidRuntime(364): Caused by: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:10:47.482: E/AndroidRuntime(364): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-19 22:10:47.482: E/AndroidRuntime(364): ... 11 more
02-19 22:11:10.302: I/Process(364): Sending signal. PID: 364 SIG: 9
02-19 22:11:17.712: D/AndroidRuntime(413): Shutting down VM
02-19 22:11:17.712: W/dalvikvm(413): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-19 22:11:17.852: E/AndroidRuntime(413): FATAL EXCEPTION: main
02-19 22:11:17.852: E/AndroidRuntime(413): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.os.Looper.loop(Looper.java:123)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 22:11:17.852: E/AndroidRuntime(413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-19 22:11:17.852: E/AndroidRuntime(413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-19 22:11:17.852: E/AndroidRuntime(413): at dalvik.system.NativeStart.main(Native Method)
02-19 22:11:17.852: E/AndroidRuntime(413): Caused by: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:11:17.852: E/AndroidRuntime(413): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-19 22:11:17.852: E/AndroidRuntime(413): ... 11 more
02-19 22:11:19.852: I/Process(413): Sending signal. PID: 413 SIG: 9
Activity:
package com.example.database;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class AndroidSQLite extends Activity {
private DBHelper mySQLiteAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listContent = (ListView)findViewById(R.id.contentlist);
/*
* Create/Open a SQLite database
* and fill with dummy content
* and close it
*/
mySQLiteAdapter = new DBHelper(this);
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.deleteAll();
mySQLiteAdapter.insert("A for Apply");
mySQLiteAdapter.insert("B for Boy");
mySQLiteAdapter.insert("C for Cat");
mySQLiteAdapter.insert("D for Dog");
mySQLiteAdapter.insert("E for Egg");
mySQLiteAdapter.insert("F for Fish");
mySQLiteAdapter.insert("G for Girl");
mySQLiteAdapter.insert("H for Hand");
mySQLiteAdapter.insert("I for Ice-scream");
mySQLiteAdapter.insert("J for Jet");
mySQLiteAdapter.insert("K for Kite");
mySQLiteAdapter.insert("L for Lamp");
mySQLiteAdapter.insert("M for Man");
mySQLiteAdapter.insert("N for Nose");
mySQLiteAdapter.insert("O for Orange");
mySQLiteAdapter.insert("P for Pen");
mySQLiteAdapter.insert("Q for Queen");
mySQLiteAdapter.insert("R for Rain");
mySQLiteAdapter.insert("S for Sugar");
mySQLiteAdapter.insert("T for Tree");
mySQLiteAdapter.insert("U for Umbrella");
mySQLiteAdapter.insert("V for Van");
mySQLiteAdapter.insert("W for Water");
mySQLiteAdapter.insert("X for X'mas");
mySQLiteAdapter.insert("Y for Yellow");
mySQLiteAdapter.insert("Z for Zoo");
mySQLiteAdapter.close();
/*
* Open the same SQLite database
* and read all it's content.
*/
mySQLiteAdapter = new DBHelper(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{DBHelper.KEY_CONTENT};
int[] to = new int[]{R.id.text};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mySQLiteAdapter.close();
}
}
Database Class
package com.example.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DBHelper {
public static final String MYDATABASE_NAME = "MY_DATABASE";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_CONTENT = "Content";
//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_CONTENT + " text not null);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public DBHelper(Context c){
context = c;
}
public DBHelper openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public DBHelper openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close(){
sqLiteHelper.close();
}
public long insert(String content){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_CONTENT};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, null);
return cursor;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
you are Tryin to load MainActivity
java.lang.ClassNotFoundException: com.example.database.MainActivity
inside your Manifest.xml change MainActivity to AndroidSQLite.
And you must use an Asynctask for the Database operations, to avoid ANR Dialog.

thread exiting with uncaught nullpointerexception

This app works fine in Note 2 running 4.1 but i get a "Thread exiting with uncaught exception" and NullPointerexception in 4.2 running htc one.I assure you there are no faults in id matching(I've cross checked several times).The app basically outputs the highest resoulution image from Google.
here are the logcats
11-15 12:18:54.589: D/dalvikvm(14843): Late-enabling CheckJNI
11-15 12:18:54.700: W/ResourceType(14843): Skipping entry 0x7f040005 in package table 0 because it is not complex!
11-15 12:18:54.700: W/ResourceType(14843): Skipping entry 0x7f040005 in package table 0 because it is not complex!
11-15 12:18:54.710: W/ResourceType(14843): Skipping entry 0x7f040005 in package table 0 because it is not complex!
11-15 12:18:54.710: W/ResourceType(14843): Skipping entry 0x7f040005 in package table 0 because it is not complex!
11-15 12:18:54.750: D/TAG(14843): MainActivity has started.
11-15 12:18:54.800: D/libEGL(14843): loaded /system/lib/egl/libEGL_adreno200.so
11-15 12:18:54.800: D/libEGL(14843): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
11-15 12:18:54.810: D/libEGL(14843): loaded /system/lib/egl/libGLESv2_adreno200.so
11-15 12:18:54.810: I/Adreno200-EGL(14843): <qeglDrvAPI_eglInitialize:269>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_VANILLA.04.02.02.60.051_msm8960_JB_VANILLA_CL2997615_release_AU (CL2997615)
11-15 12:18:54.810: I/Adreno200-EGL(14843): Build Date: 04/11/13 Thu
11-15 12:18:54.810: I/Adreno200-EGL(14843): Local Branch:
11-15 12:18:54.810: I/Adreno200-EGL(14843): Remote Branch: quic/mako_jb_mr1
11-15 12:18:54.810: I/Adreno200-EGL(14843): Local Patches: NONE
11-15 12:18:54.810: I/Adreno200-EGL(14843): Reconstruct Branch: AU_LINUX_ANDROID_JB_VANILLA.04.02.02.60.051 + NOTHING
11-15 12:18:54.850: D/OpenGLRenderer(14843): Enabling debug mode 0
11-15 12:18:58.704: D/TAG(14843): In button listener
11-15 12:18:58.704: D/TAG(14843): String has been added!!!
11-15 12:18:58.714: D/TAG(14843): Making the fragment now!!
11-15 12:18:58.714: D/TAG(14843): Arguments passed!!
11-15 12:18:58.714: D/TAG(14843): Fragment made!!
11-15 12:18:58.724: D/AndroidRuntime(14843): Shutting down VM
11-15 12:18:58.734: W/dalvikvm(14843): threadid=1: thread exiting with uncaught exception (group=0x41bba930)
11-15 12:18:58.754: E/AndroidRuntime(14843): FATAL EXCEPTION: main
11-15 12:18:58.754: E/AndroidRuntime(14843): java.lang.NullPointerException
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.ListView.makeAndAddView(ListView.java:1831)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.ListView.fillDown(ListView.java:674)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.ListView.fillFromTop(ListView.java:735)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.ListView.layoutChildren(ListView.java:1652)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:702)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.Choreographer.doFrame(Choreographer.java:532)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.os.Handler.handleCallback(Handler.java:725)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.os.Handler.dispatchMessage(Handler.java:92)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.os.Looper.loop(Looper.java:137)
11-15 12:18:58.754: E/AndroidRuntime(14843): at android.app.ActivityThread.main(ActivityThread.java:5227)
11-15 12:18:58.754: E/AndroidRuntime(14843): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 12:18:58.754: E/AndroidRuntime(14843): at java.lang.reflect.Method.invoke(Method.java:511)
11-15 12:18:58.754: E/AndroidRuntime(14843): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-15 12:18:58.754: E/AndroidRuntime(14843): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-15 12:18:58.754: E/AndroidRuntime(14843): at dalvik.system.NativeStart.main(Native Method)
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("TAG","MainActivity has started.");
drawer_enter=(EditText)findViewById(R.id.drawer_enter);
list=(ListView)findViewById(R.id.left_drawer);
//image_button=(Button)findViewById(R.id.image_button);
//image_button.setOnClickListener(button);
drawer_enter.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(drawer_enter.getWindowToken(), 0);
Log.d("TAG","In button listener");
inflateString(drawer_enter.getText().toString());
MakeFrag();
return true;
}
return false;
}
});
drawer=(DrawerLayout)findViewById(R.id.drawer_layout);
listen=new ActionBarDrawerToggle(this,drawer,R.drawable.ic_launcher,0,0){
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("History");
}
public void onDrawerClosed(View drawerView) {
getActionBar().setTitle("Best Google Image");
}
};
drawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
drawer.setDrawerListener(listen);
list.setOnItemClickListener(new DrawerItemClickListener());
}
/*private OnClickListener button=new OnClickListener() {
#Override
public void onClick(View theView) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(drawer_enter.getWindowToken(), 0);
Log.d("TAG","In button listener");
//int num=inflateString(drawer_enter.getText().toString());
MakeFrag();
}
};*/
private class DrawerItemClickListener implements ListView.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
MakeDrawerFrag(position);
//drawer.closeDrawer(arg0);
}
}
public void MakeFrag(){
Log.d("TAG", "Making the fragment now!!");
Fragment fragment=new ImageFragment();
Bundle args=new Bundle();
args.putString(ImageFragment.ARG_IMAGE_SEARCH,drawer_enter.getText().toString());
fragment.setArguments(args);
FragmentManager fragmentManager=getFragmentManager();
Log.d("TAG", "Arguments passed!!");
fragmentManager.beginTransaction().replace(R.id.frame_layout, fragment).addToBackStack(null).commit();
Log.d("TAG", "Fragment made!!");
}
private void inflateString(String name)
{
int i=0;
for(i=0;i<names.length;i++){
if(names[i]==null){
names[i]=name;
break;
}
}
list.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,names));
Log.d("TAG","String has been added!!!");
}
ImageFragment.java
public class ImageFragment extends Fragment{
public static final String ARG_IMAGE_SEARCH = "image_search";
public Bitmap done=null;
private String search="",newSearch="";
private ImageView photo=null;
/*public ImageFragment() {
// Empty constructor required for fragment subclasses
}*/
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("TAG","making the view of the Fragment!!!");
search=getArguments().getString(ARG_IMAGE_SEARCH);
int i;
for(i=0;i<search.length();i++){
if(search.charAt(i)==' '){
newSearch+="%20";
}
else{
newSearch+=search.charAt(i);
}
}
Log.d("TAG",newSearch);
new ImageLoader().execute(newSearch);
View rootView = inflater.inflate(R.layout.image_view, container, false);
photo=(ImageView) rootView.findViewById(R.id.image_view);
photo.setImageBitmap(done);
getActivity().setTitle(getArguments().getString(ARG_IMAGE_SEARCH));
return rootView;
}
private class ImageLoader extends AsyncTask<String,Void,Void>{
#Override
protected Void doInBackground(String... args) {
// TODO Auto-generated method stub
try{
Log.d("TAG", "Background thread has started!!");
URL url=new URL("https://ajax.googleapis.com/ajax/services/search/images?" +
"v=1.0&q="+args[0]);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
String urlData;
StringBuilder builder=new StringBuilder();
BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((urlData=reader.readLine()) != null){
builder.append(urlData);
}
JSONObject first=new JSONObject(builder.toString());
int i,larg = 0;
for(i=0;i<2;i++){
if((first.getJSONObject("responseData").getJSONArray("results").getJSONObject(i).getInt("width"))>first.getJSONObject("responseData").getJSONArray("results").getJSONObject(i+1).getInt("width")){
larg=i;
}
else larg=i+1;
}
String image=first.getJSONObject("responseData").getJSONArray("results").getJSONObject(larg).getString("url");
URL imgUrl = new URL(image);
HttpURLConnection imgConnection=(HttpURLConnection)imgUrl.openConnection();
InputStream input=imgConnection.getInputStream();
done=BitmapFactory.decodeStream(input);
Log.d("TAG","Bitmap has been formed!!");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result){
photo.setImageBitmap(done);
}
I believe your problem is in the ImageFragment onCreate here:
search=getArguments().getString(ARG_IMAGE_SEARCH);
I believe you're getting a null pointer because you aren't getting any arguments -- you aren't getting any arguments because the Fragment isn't receiving them -- I'll show you how to fix it at the end.
Just to make sure it's not getting any arguments, do this -- Comment out everything in the fragment's onCreateView, then make it like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.image_view, container, false);
try {
search=getArguments().getString(ARG_IMAGE_SEARCH);
Log.d("onCreateView",search); // this will print out the search string to logcat
} catch (Exception e) {
e.printStackTrace(); // or throw an exception and print it to logcat
}
return rootView;
}
This will either verify that search is getting the arguments (which it probably isn't) and print our the search string, or throw an exception. If it is throwing an exception, then proceed...
To solve the problem, you first need to add this to your Fragment:
static ImageFragment newInstance(String string){
ImageFragment fragment = new ImageFragment();
Bundle args = new Bundle();
args.putString(ImageFragment.ARG_IMAGE_SEARCH, string);
fragment.setArguments(args);
return fragment;
}
Then in your activity, start a new ImageFragment, you call it like this:
Fragment newImageFrag = ImageFragment.newInstance(WHATEVER_STRING_ARGUMENT_YOU_NEED_TO_PASS);
// from here add or replace the fragment as normal
After you do these two things, when the newImageFrag gets created via the newInstance and the passed string, you will be able to call
search=getArguments().getString(ARG_IMAGE_SEARCH);
and actually get the argument. I hope I understood your question correctly, and I hope this helps.

Categories