Reputation: 3152
I have read several errors of this kind, but I do not find a solution to my particular problem. My activities are all added to my manifest, and I did not start the emulator from an abstract class. My app used to work, until I tried to add spinners to it (which I probably did not do right). I run the app and it crashes before it even opens. Any idea what is wrong? I am a beginner, so please share plain language, thanks very much.
So here is my main activity:
package com.noni.gridimagesearch;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
public class SearchActivity extends Activity {
EditText etQuery;
GridView gvResults;
Button btnSearch;
ArrayList<ImageResult> imageResults = new ArrayList<ImageResult>();
ImageResultArrayAdapter imageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
setupViews();
imageAdapter = new ImageResultArrayAdapter(this, imageResults);
gvResults.setAdapter(imageAdapter);
gvResults.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View parent,
int position, long rowId) {
Intent i = new Intent(getApplicationContext(),
ImageDisplayActivity.class);
ImageResult imageResult = imageResults.get(position);
i.putExtra("result", imageResult);
startActivity(i);
}
});
GridView gvResults = (GridView) findViewById(R.id.gvResults);
gvResults.setOnScrollListener(new EndlessScrollListener() {
@Override
public void onLoadMore(int page, int TotalItemsCount) {
customLoadMoreDataFromApi(page);
}
});
setupAdvancedOptionsListener();
} //end onCreate method
Button advancedSearch = (Button) findViewById(R.id.advancedSearch);
public void setupAdvancedOptionsListener() {
advancedSearch.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent i = new Intent (SearchActivity.this, AdvancedOptionsActivity.class);
startActivity(i);
}
});
}
public void customLoadMoreDataFromApi(int offset) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search, menu);
return true;
}
public void setupViews() {
etQuery = (EditText) findViewById(R.id.etQuery);
gvResults = (GridView) findViewById(R.id.gvResults);
btnSearch = (Button) findViewById(R.id.btnSearch);
}
public void onImageSearch(View v) {
imageResults.clear();
String query = etQuery.getText().toString();
Toast.makeText(this, "Searching for " + query, Toast.LENGTH_SHORT).show();
AsyncHttpClient client = new AsyncHttpClient();
int offset = 0;
client.get("https://ajax.googleapis.com/ajax/services/search/images?rsz=8&" +
"start=" + offset + "&v=1.0&imgsz=medium&q=" + Uri.encode(query),
new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject response) {
JSONArray imageJsonResults = null;
try {
imageJsonResults = response.getJSONObject("responseData")
.getJSONArray("results");
imageAdapter.addAll(ImageResult
.fromJSONArray(imageJsonResults));
Log.d("DEBUG", imageResults.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is my activity with the spinners:
package com.noni.gridimagesearch;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class AdvancedOptionsActivity extends Activity implements AdapterView.OnItemSelectedListener {
private Spinner colorFilter, imageSize, imageType;
//private Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advanced_options);
colorFilter = (Spinner) findViewById(R.id.color_filter);
ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this,
R.array.color_filter, android.R.layout.simple_spinner_item);
colorFilter.setAdapter(adapter);
colorFilter.setOnItemSelectedListener(this);
imageSize = (Spinner) findViewById(R.id.image_size);
ArrayAdapter<?> adapter2 = ArrayAdapter.createFromResource(this,
R.array.image_size, android.R.layout.simple_spinner_item);
imageSize.setAdapter(adapter2);
imageSize.setOnItemSelectedListener(this);
imageType = (Spinner) findViewById(R.id.image_type);
ArrayAdapter<?> adapter3 = ArrayAdapter.createFromResource(this,
R.array.image_type, android.R.layout.simple_spinner_item);
imageType.setAdapter(adapter3);
imageType.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> adapterView, View view, int i,
long l) {
TextView myText = (TextView) view;
Toast.makeText(this, "You selected " + myText.getText(),
Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.advanced_options, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
Here is the LogCat:
04-01 06:43:26.638: E/AndroidRuntime(5247): FATAL EXCEPTION: main
04-01 06:43:26.638: E/AndroidRuntime(5247): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.noni.gridimagesearch/com.noni.gridimagesearch.SearchActivity}: java.lang.NullPointerException
04-01 06:43:26.638: E/AndroidRuntime(5247): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
04-01 06:43:26.638: E/AndroidRuntime(5247): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-01 06:43:26.638: E/AndroidRuntime(5247): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-01 06:43:26.638: E/AndroidRuntime(5247): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-01 06:43:26.638: E/AndroidRuntime(5247): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 06:43:26.638: E/AndroidRuntime(5247): at android.os.Looper.loop(Looper.java:137)
04-01 06:43:26.638: E/AndroidRuntime(5247): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-01 06:43:26.638: E/AndroidRuntime(5247): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 06:43:26.638: E/AndroidRuntime(5247): at java.lang.reflect.Method.invoke(Method.java:511)
04-01 06:43:26.638: E/AndroidRuntime(5247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-01 06:43:26.638: E/AndroidRuntime(5247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-01 06:43:26.638: E/AndroidRuntime(5247): at dalvik.system.NativeStart.main(Native Method)
04-01 06:43:26.638: E/AndroidRuntime(5247): Caused by: java.lang.NullPointerException
04-01 06:43:26.638: E/AndroidRuntime(5247): at android.app.Activity.findViewById(Activity.java:1825)
04-01 06:43:26.638: E/AndroidRuntime(5247): at com.noni.gridimagesearch.SearchActivity.<init>(SearchActivity.java:66)
04-01 06:43:26.638: E/AndroidRuntime(5247): at java.lang.Class.newInstanceImpl(Native Method)
04-01 06:43:26.638: E/AndroidRuntime(5247): at java.lang.Class.newInstance(Class.java:1319)
04-01 06:43:26.638: E/AndroidRuntime(5247): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
04-01 06:43:26.638: E/AndroidRuntime(5247): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
04-01 06:43:26.638: E/AndroidRuntime(5247): ... 11 more
Here is my activity_search.xml:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SearchActivity">
<EditText
android:id="@+id/etQuery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="@string/query_hint"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onImageSearch"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/search_label" />
<GridView
android:id="@+id/gvResults"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnSearch"
android:layout_marginTop="36dp"
android:numColumns="3" >
</GridView>
<Button
android:id="@+id/advancedSearch"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_alignLeft="@+id/etQuery"
android:layout_below="@+id/btnSearch"
android:background="#D3F5F4"
android:text="@string/advanced_search"
android:textSize="12sp" />
</RelativeLayout>
Upvotes: 0
Views: 102
Reputation: 1996
You have setup all views in onCreate() by setupViews()
public void setupViews() {
etQuery = (EditText) findViewById(R.id.etQuery);
gvResults = (GridView) findViewById(R.id.gvResults);
btnSearch = (Button) findViewById(R.id.btnSearch);
}
the individual code segment like:
//GridView gvResults = (GridView) findViewById(R.id.gvResults);
gvResults.setOnScrollListener(new EndlessScrollListener() {
and
//Button advancedSearch = (Button) findViewById(R.id.advancedSearch);
public void setupAdvancedOptionsListener() {
are not necessary. Just delete them.
Edit:
declare Button advancedSearch;
as a member variable.
and add advancedSearch = (Button) findViewById(R.id.advancedSearch);
in setupViews() method.
Upvotes: 1
Reputation: 133570
I guess line 66 is
Button advancedSearch = (Button) findViewById(R.id.advancedSearch);
You do not have a button with id advancedSearch
in activity_search.xml
. SO initialization fails leading to NullPointerException
.
Edit:
After looking at the source code. It looks like you have the Button initialized outside onCreate
. Initialize it inside onCreate
and make sure you have the braces }
in the right place.
It looks like you have misplaced }
.
Get rid of this
setupAdvancedOptionsListener();
and this
public void setupAdvancedOptionsListener() {
Change to
Button advancedSearch = (Button) findViewById(R.id.advancedSearch);
advancedSearch.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent i = new Intent (MainActivity.this, AdvancedOptionsActivity.class);
startActivity(i);
}
});
} // brace of onCreate end here.
Edit:
What you could have done. Call this in onCreate
setupAdvancedOptionsListener();
} // end of onCreate
Then
public void setupAdvancedOptionsListener() {
Button advancedSearch = (Button) findViewById(R.id.advancedSearch);
advancedSearch.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent i = new Intent (MainActivity.this, AdvancedOptionsActivity.class);
startActivity(i);
}
});
}
Upvotes: 2
Reputation: 24853
Try this..
Initilize below Button inside OnCreate
Button advancedSearch;
inside OnCreate
advancedSearch = (Button) findViewById(R.id.advancedSearch);
advancedSearch.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent i = new Intent (MainActivity.this, AdvancedOptionsActivity.class);
startActivity(i);
}
});
Upvotes: 1