Java null pointer exception on using simple Intent Extra - java

When I try to send some text over to the next Activity the program crashes saying "null pointer exception unable to start activity" but without using Intent Extra the Intent works fine.
Main Activity
public class MainActivity extends Activity implements OnClickListener{
EditText et;
public final static String KEY="com.example.myprotrial";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
Button btn =(Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
}
#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 void onClick(View arg0) {
Intent i = new Intent(MainActivity.this,MyActivity.class);
i.putExtra(KEY, et.getText().toString());
startActivity(i);
}
}
MyActivity
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Intent intent=getIntent();
String a = intent.getStringExtra(MainActivity.KEY);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(a);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
}
LogCat
12-09 13:58:33.115: E/AndroidRuntime(1588): FATAL EXCEPTION: main
12-09 13:58:33.115: E/AndroidRuntime(1588): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myprotrial/com.example.myprotrial.MyActivity}: java.lang.NullPointerException
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.os.Looper.loop(Looper.java:137)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-09 13:58:33.115: E/AndroidRuntime(1588): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 13:58:33.115: E/AndroidRuntime(1588): at java.lang.reflect.Method.invoke(Method.java:525)
12-09 13:58:33.115: E/AndroidRuntime(1588): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-09 13:58:33.115: E/AndroidRuntime(1588): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-09 13:58:33.115: E/AndroidRuntime(1588): at dalvik.system.NativeStart.main(Native Method)
12-09 13:58:33.115: E/AndroidRuntime(1588): Caused by: java.lang.NullPointerException
12-09 13:58:33.115: E/AndroidRuntime(1588): at com.example.myprotrial.MyActivity.onCreate(MyActivity.java:18)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.Activity.performCreate(Activity.java:5133)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-09 13:58:33.115: E/AndroidRuntime(1588): ... 11 more

The problem is that your LogCat doesn't tell on which line the NPE occurs.
From your code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Intent intent=getIntent();
String a = intent.getStringExtra(MainActivity.KEY);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(a);
}
The if the NPE is occurring on intent.getStringExtra then your intent is null. If it's on tv.setText(a) then most likely the activity_my layout doesn't have textView1 in it. Step through it in the debugger and watch the instance variables.

Related

Checkbox in the popupMenu

I have popupMenu and CheckBox. I need make write status CheckBox to boolean.
This code not working:
MenuItem fast_result;
boolean fast=false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.FastResult:
fast_result = item.getSubMenu().getItem(R.id.FastResult);//This is 182 line
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;
}
}
It is errors:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.alexvsalex.HelpforMath.RootsActivity.onOptionsItemSelected(RootsActivity.java:182)
at android.app.Activity.onMenuItemSelected(Activity.java:2502)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
at android.widget.AdapterView.performItemClick(AdapterView.java:292)
at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
at android.widget.AbsListView$1.run(AbsListView.java:3168)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
What to do?
The problem is solved:
case R.id.FastResult:
fast_result = item; //There was an error
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;

using Bundle object to parse JSONobject Fragment to Fragment

i am using Bundle object to parse jsonobject one fragment to another fragment.but when running App it crash..can any one help me....
Fragment 1
after getting my jsonobject using Async task using button i open the my second Fragment
public void onClick(View v) {
Log.d(TAG,"More button clicked");
Fragment_account_details fragment = new Fragment_account_details();
Bundle bundle = new Bundle();
String accountDetails = name_value_list.toString();
bundle.putString("accountDetails", accountDetails);
fragment.setArguments(bundle);
getFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment.newInstance(),
fragment.TAG).commit();
};
Fragment 2
public class Fragment_account_details extends Fragment {
TextView name, officePhone,fax,email;
public static Fragment_account_details newInstance() {
return new Fragment_account_details();
}
public final static String TAG = Fragment_account_details.class
.getSimpleName();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_account_details, container,false);
name = (TextView) rootView.findViewById(R.id.text_name);
fax = (TextView) rootView.findViewById(R.id.text_fax);
email = (TextView) rootView.findViewById(R.id.text_email);
String accountDetails = getArguments().getString("accountDetails");
return rootView;
}
}
Logcat
12-09 11:18:22.291: D/Fragment_Entry_Account(1486): More button clicked
12-09 11:18:22.307: D/AndroidRuntime(1486): Shutting down VM
12-09 11:18:22.307: W/dalvikvm(1486): threadid=1: thread exiting with uncaught exception (group=0xa4cacb20)
12-09 11:18:22.311: E/AndroidRuntime(1486): FATAL EXCEPTION: main
12-09 11:18:22.311: E/AndroidRuntime(1486): Process: com.wakensys.sugercrm_wakensys, PID: 1486
12-09 11:18:22.311: E/AndroidRuntime(1486): java.lang.NullPointerException
12-09 11:18:22.311: E/AndroidRuntime(1486): at com.wakensys.sugercrm_wakensys.fragments.Fragment_account_details.onCreateView(Fragment_account_details.java:41)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.os.Handler.handleCallback(Handler.java:733)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.os.Handler.dispatchMessage(Handler.java:95)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.os.Looper.loop(Looper.java:136)
12-09 11:18:22.311: E/AndroidRuntime(1486): at android.app.ActivityThread.main(ActivityThread.java:5001)
12-09 11:18:22.311: E/AndroidRuntime(1486): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 11:18:22.311: E/AndroidRuntime(1486): at java.lang.reflect.Method.invoke(Method.java:515)
12-09 11:18:22.311: E/AndroidRuntime(1486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-09 11:18:22.311: E/AndroidRuntime(1486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-09 11:18:22.311: E/AndroidRuntime(1486): at dalvik.system.NativeStart.main(Native Method)

Force close after clear memory and resume app

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

App cannot pass values between activities

I am working on an app in which the user inputs a number into one activity, and the number shows up on the next activity. This is what I have so far.
First activity: EnterText.java
public class EnterText extends Activity {
//This is the string that will be passed to the next activity
public static String mynumber = "com.example.ListviewExplorer.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_text);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.enter_text, menu);
return true;
}
//This is where onClick, the button will invoke sendMessage
//sendMessage is supposed to start the next activity
public void sendMessage(View view){
Intent intent = new Intent (this, DisplayText.class);
EditText editText = (EditText) findViewById(R.id.editText1);
mynumber = editText.getText().toString();
intent.putExtra("MyNumber",mynumber);
startActivity(intent);
}
}
Second activity: DisplayText.java
public class DisplayText extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras=getIntent().getExtras();
String mynumber = extras.getString("MyNumber");
TextView textview = (TextView)findViewById(R.id.textView1);
textview.setText(EnterText.mynumber);
setContentView(R.layout.activity_display_text);
// Show the Up button in the action bar.
setupActionBar();
}
}
The problem is that this app crashes when I press the button on the first activity and it calls sendMessage; the second activity isn't even displayed.
Does anyone know why this is occurring?
LogCat is posted below.
Get to it!
05-29 11:57:59.385: W/dalvikvm(3046): threadid=1: thread exiting with uncaught exception (group=0x42d5d140)
05-29 11:57:59.425: E/AndroidRuntime(3046): FATAL EXCEPTION: main
05-29 11:57:59.425: E/AndroidRuntime(3046): Process: com.example.listviewexplorer, PID: 3046
05-29 11:57:59.425: E/AndroidRuntime(3046): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listviewexplorer/com.example.listviewexplorer.DisplayText}: java.lang.NullPointerException
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.os.Handler.dispatchMessage(Handler.java:102)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.os.Looper.loop(Looper.java:136)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-29 11:57:59.425: E/AndroidRuntime(3046): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 11:57:59.425: E/AndroidRuntime(3046): at java.lang.reflect.Method.invoke(Method.java:515)
05-29 11:57:59.425: E/AndroidRuntime(3046): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-29 11:57:59.425: E/AndroidRuntime(3046): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-29 11:57:59.425: E/AndroidRuntime(3046): at dalvik.system.NativeStart.main(Native Method)
05-29 11:57:59.425: E/AndroidRuntime(3046): Caused by: java.lang.NullPointerException
05-29 11:57:59.425: E/AndroidRuntime(3046): at com.example.listviewexplorer.DisplayText.onCreate(DisplayText.java:21)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.Activity.performCreate(Activity.java:5231)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-29 11:57:59.425: E/AndroidRuntime(3046): ... 11 more
You need to assign the layout before using it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// assign layout
setContentView(R.layout.activity_display_text);
// then you can use it
TextView textview = (TextView)findViewById(R.id.textView1);
textview.setText(EnterText.mynumber);
...
}
Since the layout has not been defined it is at that stage null and when you try to access Views inside it you get that NullPointerException thrown.
TextView textview = (TextView)findViewById(R.id.textView1);
textview.setText(EnterText.mynumber);
setContentView(R.layout.activity_display_text);
Before setContentView() findViewById() will return null no matter what, and invoking a method on null reference causes the NPE as seen in your stacktrace.
Move the setContentView() above the findViewById(), assuming that textView1 is actually in activity_display_text layout.
This fixes the NPE crash. To actually pass a value, have a look at some of the other answers.

NullPointerException not found using ListActivity and ArrayAdapter

Hi there stackoverflow ,
Now I ran into another problem ,I am working on a ListActivity from a custom ArrayAdapter so I can fully customize my Listview to include TextViews , switches , checkboxes .... I got a NullPointerException without I know from where its originating ! Here is my current code :
public class MenuActivity extends SherlockListActivity {
ListView scroll;
int time;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
scroll = this.getListView();
this.setListAdapter(new MyAdapter(this.getApplicationContext()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
finish();
}
}
public View find(int x){
return findViewById(x);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
switch (position){
case (0):{
Toast.makeText(getApplicationContext(), "Hello you just chose \"Splash\"", Toast.LENGTH_SHORT).show();
}break;
case (1):{
Toast.makeText(getApplicationContext(), "Its another one here !", Toast.LENGTH_SHORT).show();
}break;
case (2):{
Toast.makeText(getApplicationContext(), String.valueOf(id) +" and "+ position, Toast.LENGTH_SHORT).show();
}break;
}
}
public void showToast(String toast){
Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
}
}
class MyAdapter extends ArrayAdapter<String>{
LayoutInflater inf = LayoutInflater.from(this.getContext());
public MyAdapter(Context context) {
super(context , 0 ,stringArray);
inf.from(context);
// TODO Auto-generated constructor stub
}
public static String[] stringArray= new String[]{"Splash" , "Another Example" , "Future Checkable"};
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null){
inf.inflate(R.layout.normal_row, parent , false);
}
switch(position){
case(0):
convertView = (LinearLayout) inf.inflate(R.layout.row_switch, parent,false);
break;
}
return convertView;
}
}
And My StackTrace :
12-09 17:52:55.631: E/AndroidRuntime(11921): FATAL EXCEPTION: main
12-09 17:52:55.631: E/AndroidRuntime(11921): java.lang.NullPointerException
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.AbsListView.obtainView(AbsListView.java:2161)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.ListView.makeAndAddView(ListView.java:1831)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.ListView.fillDown(ListView.java:674)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.ListView.fillFromTop(ListView.java:735)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.ListView.layoutChildren(ListView.java:1652)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.View.layout(View.java:14003)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewGroup.layout(ViewGroup.java:4375)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.View.layout(View.java:14003)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewGroup.layout(ViewGroup.java:4375)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.View.layout(View.java:14003)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewGroup.layout(ViewGroup.java:4375)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.View.layout(View.java:14003)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewGroup.layout(ViewGroup.java:4375)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.Choreographer.doFrame(Choreographer.java:532)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.os.Handler.handleCallback(Handler.java:725)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.os.Handler.dispatchMessage(Handler.java:92)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.os.Looper.loop(Looper.java:137)
12-09 17:52:55.631: E/AndroidRuntime(11921): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-09 17:52:55.631: E/AndroidRuntime(11921): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 17:52:55.631: E/AndroidRuntime(11921): at java.lang.reflect.Method.invoke(Method.java:511)
12-09 17:52:55.631: E/AndroidRuntime(11921): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-09 17:52:55.631: E/AndroidRuntime(11921): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-09 17:52:55.631: E/AndroidRuntime(11921): at dalvik.system.NativeStart.main(Native Method)
Change this:
if (convertView == null){
inf.inflate(R.layout.normal_row, parent , false);
}
to:
if (convertView == null){
convertView = inf.inflate(R.layout.normal_row, parent , false);
}
Also, regarding having two types of rows in the ListView you should look at implementing the getViewTypeCount() and getItemViewType() methods of the adapter(as your current code will not work that well).

Categories