I know a bunch of people have asked this same thing but I just don't know what's going on. I'm trying to make a calculator in Eclipse, but I keep getting a list of errors.
There are no errors in the file that the program notices, although there is an error in the layout.xml but it hasn't caused a problem before so that shouldn't cause a problem.
07-30 08:19:50.470: D/AndroidRuntime(2071): Shutting down VM
07-30 08:19:50.470: W/dalvikvm(2071): threadid=1: thread exiting with uncaught
exception (group=0x40a421f8)
07-30 08:19:50.480: E/AndroidRuntime(2071): FATAL EXCEPTION: main
07-30 08:19:50.480: E/AndroidRuntime(2071): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{com.example.se.miun.chris.calculator/com.example.se.miun.chris.
calculator.MainActivity}: java.lang.NullPointerException
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.access$600(ActivityThread.java:123)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.os.Handler.dispatchMessage(Handler.java:99)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.os.Looper.loop(Looper.java:137)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.main(ActivityThread.java:4424)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
java.lang.reflect.Method.invokeNative(Native Method)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
java.lang.reflect.Method.invoke(Method.java:511)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
dalvik.system.NativeStart.main(Native Method)
07-30 08:19:50.480: E/AndroidRuntime(2071): Caused by: java.lang.NullPointerException
07-30 08:19:50.480: E/AndroidRuntime(2071): at
com.example.se.miun.chris.calculator.MainActivity.onCreate(MainActivity.java:60)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.Activity.performCreate(Activity.java:4465)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-30 08:19:50.480: E/AndroidRuntime(2071): ... 11 more
This is my coding. It doesn't really do anything yet, but I wanted to just run it to see if it encountered any errors. This is the mainActivity.java file.
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
Button Seven;
Button Eight;
Button Nine;
Button Four;
Button Five;
Button Six;
Button One;
Button Two;
Button Three;
Button Zero;
Button Point;
Button Negative;
TextView TextBox;
int x;
int y;
String z;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Seven = (Button)findViewById(R.id.NumberSeven);
Seven.setOnClickListener(this);
Eight = (Button)findViewById(R.id.NumberEight);
Eight.setOnClickListener(this);
Nine = (Button)findViewById(R.id.NumberNine);
Nine.setOnClickListener(this);
Four = (Button)findViewById(R.id.NumberFour);
Four.setOnClickListener(this);
Five = (Button)findViewById(R.id.NumberFive);
Five.setOnClickListener(this);
Six = (Button)findViewById(R.id.NumberSix);
Six.setOnClickListener(this);
One = (Button)findViewById(R.id.NumberOne);
One.setOnClickListener(this);
Two = (Button)findViewById(R.id.NumberTwo);
Two.setOnClickListener(this);
Three = (Button)findViewById(R.id.NumberThree);
Three.setOnClickListener(this);
Zero = (Button)findViewById(R.id.NumberZero);
Zero.setOnClickListener(this);
Point = (Button)findViewById(R.id.Point);
Point.setOnClickListener(this);
Negative = (Button)findViewById(R.id.NNegative);
Negative.setOnClickListener(this);
TextBox = (TextView)findViewById(R.id.Screen);
x = (Integer) null;
y = (Integer) null;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View One) {
if(z == null){
x = 1;
TextBox.setText(x);
TextBox.setText("diggity");
}
else if(z != null) {
y = 1;
TextBox.setText(x);
TextBox.setText(z);
TextBox.setText(y);
}
}
}
line 60: x = (Integer) null;
This line will compile to this bytecode (disassembled by javap):
aconst_null
checkcast #2; //class java/lang/Integer
invokevirtual #3; //Method java/lang/Integer.intValue:()I
Third line will cause a NullPointerException becouse Integer object is actually your null constant :)
Primitive data types (int, long etc.) is the only non-object types in Java. null is used to show that the current variable (Object variable) is not backed by the actual object (no memory was allocated). For primitive types memory allocates immediately so they cant have this null state.
So you should check for "if(x == 0)" or define it as Integer.
P.S. And don't cast null to anything :)
It's like this
you're not getting much errors because the application can't launch. it cannot launch because it's onCreate() cannot finish.
onCreate() cannot finish because of a nullPointerException.
you cast null into integer twice, instead of simply instantiating a new integer which will default to 0. once you get rid of that, it should work.
see?
E/AndroidRuntime(2071): Caused by: java.lang.NullPointerException
E/AndroidRuntime(2071): at
MainActivity.onCreate(MainActivity.java:60)
and i bet that this is line 60
x = (Integer) null;
so change it to
x = new Integer();
Related
I'm getting familiar with programming Android and ContectProviders. I have created the code (in testing purposes) to check reading/writing to database
This is the code TestProvider.java
package com.example.testapp;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
public class TestProvider extends ContentProvider
{
private static final String DBNAME = "testdb";
private static final String SQL_CREATE_MAIN = "CREATE TABLE if not exists test(id INTEGER PRIMARY KEY, word TEXT)";
private MainDatabaseHelper helper;
#Override
public int delete(Uri uri, String selection, String[] selectionArgs)
{
return 0;
}
#Override
public String getType(Uri uri)
{
return null;
}
#Override
public Uri insert(Uri uri, ContentValues values)
{
return null;
}
#Override
public boolean onCreate()
{
helper = new MainDatabaseHelper(getContext());
return false;
}
#Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
{
return helper.getReadableDatabase().rawQuery("SELECT * FROM test", null);
}
#Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
{
return 0;
}
protected static final class MainDatabaseHelper extends SQLiteOpenHelper
{
MainDatabaseHelper(Context context)
{
super(context, DBNAME, null, 1);
}
public void onCreate(SQLiteDatabase db)
{
db.execSQL(SQL_CREATE_MAIN);
db.execSQL("INSERT INTO test (word) VALUES ('AAA')");
db.execSQL("INSERT INTO test (word) VALUES ('BBB')");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
}
}
And this is MainActivity.java
package com.example.testapp;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TestProvider provider = new TestProvider();
Cursor c = provider.query(Uri.parse("content://com.example.testapp.provider/test"), null, null, null, null);
do
{
Log.d("MainActivity", String.format("ID:%s / Word:%s", c.getInt(c.getColumnIndex("id")), c.getString(c.getColumnIndex("word"))));
}
while(c.moveToNext());
}
#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) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Stacktrace from logcat:
07-30 15:38:25.695: E/Trace(3533): error opening trace file: No such file or directory (2)
07-30 15:38:26.255: D/AndroidRuntime(3533): Shutting down VM
07-30 15:38:26.305: W/dalvikvm(3533): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-30 15:38:26.345: E/AndroidRuntime(3533): FATAL EXCEPTION: main
07-30 15:38:26.345: E/AndroidRuntime(3533): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testapp/com.example.testapp.MainActivity}: java.lang.NullPointerException
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.os.Looper.loop(Looper.java:137)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-30 15:38:26.345: E/AndroidRuntime(3533): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 15:38:26.345: E/AndroidRuntime(3533): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 15:38:26.345: E/AndroidRuntime(3533): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-30 15:38:26.345: E/AndroidRuntime(3533): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-30 15:38:26.345: E/AndroidRuntime(3533): at dalvik.system.NativeStart.main(Native Method)
07-30 15:38:26.345: E/AndroidRuntime(3533): Caused by: java.lang.NullPointerException
07-30 15:38:26.345: E/AndroidRuntime(3533): at com.example.testapp.TestProvider.query(TestProvider.java:45)
07-30 15:38:26.345: E/AndroidRuntime(3533): at com.example.testapp.MainActivity.onCreate(MainActivity.java:19)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.Activity.performCreate(Activity.java:5104)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-30 15:38:26.345: E/AndroidRuntime(3533): ... 11 more
AndroidManifest.xml (the part with the provider)
<provider android:name="com.example.testapp.TestProvider" android:authorities="com.example.testapp.provider" android:exported="false">
</provider>
Where is the problem? Thanks
the problem is this line. TestProvider provider = new TestProvider(); You should not instantiate the ContentProvider directly but use getContentResolver() from your Activity. Android instantiates it for it.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi hoping someone can help me with a error i'm getting when running my code. What want is when the user gets game over the score from the main class is taken to the gameover class to display the final score. without the intent the game over screen loads fine but when I add the intent upon gameover running the game the app crashes Here is my code where score is the int:
main class intent:
Intent myIntent = new Intent(MainActivity.this,gameover.class);
myIntent.putExtra("number", score);
MainActivity.this.startActivity(myIntent);
gameover class:
EndScore = (TextView) findViewById(R.id.show);
int numScore;
numScore = getIntent().getExtras().getInt("number");
String s = String.valueOf( numScore );
EndScore.setText(s);
I have been looking around the forums but can't see what I am doing wrong.
Log is as follows:
12-28 02:36:37.760: I/Adreno200-EGLSUB(17096): <ConfigWindowMatch:2081>: Format RGBA_8888.
12-28 02:36:37.770: D/memalloc(17096): /dev/pmem: Mapped buffer base:0x5189a000 size:4866048 offset:4251648 fd:68
12-28 02:36:37.860: D/memalloc(17096): /dev/pmem: Mapped buffer base:0x51e6e000 size:2949120 offset:2334720 fd:71
12-28 02:36:38.870: W/dalvikvm(17096): threadid=1: thread exiting with uncaught exception (group=0x40b0a9f0)
12-28 02:36:38.880: E/AndroidRuntime(17096): FATAL EXCEPTION: main
12-28 02:36:38.880: E/AndroidRuntime(17096): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.phil3992.colourguess/com.phil3992.colourguess.gameover}: java.lang.NullPointerException
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.os.Looper.loop(Looper.java:137)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-28 02:36:38.880: E/AndroidRuntime(17096): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 02:36:38.880: E/AndroidRuntime(17096): at java.lang.reflect.Method.invoke(Method.java:511)
12-28 02:36:38.880: E/AndroidRuntime(17096): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
12-28 02:36:38.880: E/AndroidRuntime(17096): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
12-28 02:36:38.880: E/AndroidRuntime(17096): at dalvik.system.NativeStart.main(Native Method)
12-28 02:36:38.880: E/AndroidRuntime(17096): Caused by: java.lang.NullPointerException
12-28 02:36:38.880: E/AndroidRuntime(17096): at com.phil3992.colourguess.gameover.onCreate(gameover.java:22)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.Activity.performCreate(Activity.java:4470)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-28 02:36:38.880: E/AndroidRuntime(17096): ... 11 more
The int is increment when the user scores a point then on game over it should send and be shown in the textview
Update whole gameover class:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class gameover extends Activity {
Button re_run;
TextView EndScore;
#Override
public void onCreate(Bundle savedInstanceState) {
re_run = (Button) findViewById(R.id.retry);
super.onCreate(savedInstanceState);
setContentView(R.layout.gameover);
EndScore = (TextView) findViewById(R.id.show);
int numScore;
numScore = getIntent().getExtras().getInt("number");
String s = String.valueOf( numScore );
EndScore.setText(s);
}
#SuppressWarnings("unused")
private void setButtonOnClickListeners(){
re_run.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
}
});
}
#Override
public void onBackPressed() {
// Do nothing
}
}
Put this line-
re_run = (Button) findViewById(R.id.retry);
Below
setContentView().
It looks as though the TextView EndScore (if you could in the future use lower case letters for variables) could not be found within the R.layout.gameover layout.
Be sure that the TextView is actually in that XML file.
Be sure that it is a TextView and not something else, like an EditText or something similar (although I think that would throw a different error).
EndScore is null according to the LogCat.
I have recently gotten back to developing the app i was working on and encountered a problem when trying to run it. The app has worked fine when run before so I am a little puzzled about this.
Log cat:
07-30 12:07:53.119: E/AndroidRuntime(8070): FATAL EXCEPTION: main
07-30 12:07:53.119: E/AndroidRuntime(8070): java.lang.RuntimeException: Unable to start activity ComponentInfo{simcas.fartberegneren/simcas.fartberegneren.MainActivity}: java.lang.NullPointerException
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.access$700(ActivityThread.java:159)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.os.Looper.loop(Looper.java:137)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.main(ActivityThread.java:5419)
07-30 12:07:53.119: E/AndroidRuntime(8070): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:07:53.119: E/AndroidRuntime(8070): at java.lang.reflect.Method.invoke(Method.java:525)
07-30 12:07:53.119: E/AndroidRuntime(8070): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
07-30 12:07:53.119: E/AndroidRuntime(8070): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
07-30 12:07:53.119: E/AndroidRuntime(8070): at dalvik.system.NativeStart.main(Native Method)
07-30 12:07:53.119: E/AndroidRuntime(8070): Caused by: java.lang.NullPointerException
07-30 12:07:53.119: E/AndroidRuntime(8070): at simcas.fartberegneren.MyAdapter.<init>(MyAdapter.java:38)
07-30 12:07:53.119: E/AndroidRuntime(8070): at simcas.fartberegneren.MainActivity.onCreate(MainActivity.java:32)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.Activity.performCreate(Activity.java:5372)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
07-30 12:07:53.119: E/AndroidRuntime(8070): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
07-30 12:07:53.119: E/AndroidRuntime(8070): ... 11 more
view_pager.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view_pager" />
</LinearLayout>
MyAdapter.java:
public class MyAdapter extends FragmentStatePagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public MyAdapter(FragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getActionBar();
mViewPager = pager;
mViewPager.setAdapter(this); // This is line 38 causing the nullpointer
mViewPager.setOnPageChangeListener(this);
MainActivity.java:
public class MainActivity extends FragmentActivity implements TabListener {
static Context context;
private MyAdapter myAdapter;
private ViewPager myViewPager;
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager);
myViewPager = new ViewPager(this);
myViewPager = (ViewPager) findViewById(R.id.view_pager);
final ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
myAdapter = new MyAdapter(this, myViewPager); // This is line 32
myAdapter.addTab(actionbar.newTab().setText(R.string.menu_speed), SpeedZonesFragment.class, null);
myAdapter.addTab(actionbar.newTab().setText(R.string.menu_dist), DistanceFragment.class, null);
myAdapter.addTab(actionbar.newTab().setText(R.string.menu_fuel), FuelConsumptionFragment.class, null);
What am I missing here?
It should be noted that the code havent changed since i successfully ran it on my device half a year ago.
EDIT:
It seems, that even though I add some code, the nullpointer is still from the same two lines, despite the line has changed! What is going on?
The constructor public MyAdapter(FragmentActivity activity, ViewPager pager) is being called with a null pager.
Make a break point here:
myViewPager = (ViewPager) findViewById(R.id.view_pager);
Check if it is returning your pager object and check the xml view_pager.xml for the pager ID make sure it is view_pager.
EDIT: I saw your posted XML, please make sure of it's name view_pager.xml because you didn't post that.
try this:- Put try catch block where you are setting the Adapter. And If possible take a static variable in Main Activity.
public static int selectedTab;
and in the
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
selectedTab = tab.getPosition();
Log.d("Selected Tab is", ">" + selectedTab);
}
and the place where you are setting adapter, put this type of code. In my case I have 7 tabs and each has view pager. So on the 4th tab I put this code which ignore the Null pointer exception. Actually It happens because, if on the first tab the data is not come, and we move on the 4th or 5th tab and after that if data will come on first tab. Then we get the NullPointer Exception. Sorry for my english, I hope you will understand.
if (MainActivity.selectedTab == 0 || MainActivity.selectedTab == 1|| MainActivity.selectedTab == 2
|| MainActivity.selectedTab == 6) {
} else {
try {
adapter = new GalleryAdapter(getActivity(),
R.layout.row_gallery, mGridArray);
mGridView.setAdapter(adapter);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
I have two framelayout in my main.xml file. I add framelayouts to the class that extends Fragment. my main class extends FragmentActivity and this is Oncreate method of it:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm =getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
Fragment f=new Freg1();
Fragment f2=new Freg1();
ft.add(R.id.frame1, f);
ft.add(R.id.frame2, f2);
ft.commit();
tf=Typeface.createFromAsset(this.getAssets(),"font/Byekan.ttf" );
tv1=(TextView) findViewById(R.id.textView1);
tv1.setTypeface(tf);
Log.i(TAG,"1");
lv1=(ListView) findViewById(R.id.listView1);
lv2=(ListView) findViewById(R.id.listView2);
Log.i(TAG,"2");
List<String> stringList = new ArrayList<String>(Arrays.asList(s1));
Log.i(TAG,"3");
ListAdapter listAdapter = new CustomListAdapter(MainActivity.this , R.layout.custom_list ,stringList);
Log.i(TAG,"4");
lv1.setAdapter(listAdapter);
Log.i(TAG,"5");
lv2.setAdapter(listAdapter);
Log.i(TAG,"6");
}
when i run the codes, it crashed after LOG no4. that mean setAdapter() method do not work. how can i resolve this problem?
this is my logcat resource:
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x40a13300)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taxitabriz/com.example.taxitabriz.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
... 11 more
thank for you that help me to resolve problem.
Your ListView lv1 is null as you can see here:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.NullPointerException
....
com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
The line 55 of MainActivity should be this call: lv1.setAdapter(listAdapter);
Make sure that your listView1 is included within the layout and initialized correctly prior to trying to set an Adapter to it.
So whenever I click the button on the startup page, it gives me a force close error. Here's the class for the main.xml layout file:
public class ForeverAloneActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnstrt = (Button) findViewById(R.id.toq1);
btnstrt.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent frstq = new Intent(v.getContext(), QuestionOne.class);
startActivityForResult(frstq, 0);
And this is what I believe is producing the error. This class is related to the page that that when pressing the button on the startup page, you are taken to:
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView (R.layout.frstq);
Button startQ2 = (Button) findViewById(R.id.toq2);
startQ2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent toQ2 = new Intent(v.getContext(), QuestionTwo.class);
final EditText number = (EditText) findViewById(R.id.editText1);
final Toast error = Toast.makeText(QuestionOne.this, "Please insert a value", Toast.LENGTH_SHORT);
if (number.getText().toString().equals("")) {error.show();
}else{
startActivityForResult(toQ2, 0);}
That if statement is there as on the next page, there is an EditText box. I tried to make it so that if there is nothing in the EditText box, it displays a toast message saying "Please insert a value". Until an integer is put into the EditText box, then the button will not work.
If someone can help, it will be much appreciated.
Logcat:
04-07 19:33:58.199: W/dalvikvm(362): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-07 19:33:58.221: E/AndroidRuntime(362): FATAL EXCEPTION: main
04-07 19:33:58.221: E/AndroidRuntime(362): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.kenning.foreveralone/com.kenning.foreveralone.QuestionOne}; have you declared this activity in your AndroidManifest.xml?
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Activity.startActivityForResult(Activity.java:2817)
04-07 19:33:58.221: E/AndroidRuntime(362): at com.kenning.foreveralone.ForeverAloneActivity$1.onClick(ForeverAloneActivity.java:22)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.view.View.performClick(View.java:2408)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.view.View$PerformClick.run(View.java:8816)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Handler.handleCallback(Handler.java:587)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Looper.loop(Looper.java:123)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-07 19:33:58.221: E/AndroidRuntime(362): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 19:33:58.221: E/AndroidRuntime(362): at java.lang.reflect.Method.invoke(Method.java:521)
04-07 19:33:58.221: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-07 19:33:58.221: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-07 19:33:58.221: E/AndroidRuntime(362): at dalvik.system.NativeStart.main(Native Method)
are you adding QuestionTwo in manifiest file ?
check your error - have you declared this activity in your AndroidManifest.xml?
Declare your activity in manifeast file. It seems you forgetted that
<activity android:name="QuestionTwo" />
also dont forget to include every activity in your manifeast file
Hey have you added the QuestionTwo class as an activity in menifest file. If after adding the
Intent toQ2 = new Intent(YourCurrentActivity.this,QuestionTwo.class);
then you must have not added the activity in menifest.B'coz it clearly shows in the log that Activity is not found. You have to declare the activity in menifest file.
Hope you'll get run your app.