Error with my Activity, FATAL EXCEPTION: main [duplicate] - java

This question already has answers here:
Your content must have a ListView whose id attribute is 'android.R.id.list'
(7 answers)
Closed 6 years ago.
I've got a problem when trying to run my app in Android Studio. I get a
FATAL EXCEPTION: main
more precisely there seems to be a problem at:
dancam.com.chords.ChordsListActivity.onCreate(ChordsListActivity.java:13)
which is:
setContentView(R.layout.activity_chords_list);
here is the ChordsListActivity class:
import android.app.ListActivity;
import android.os.Bundle;
public class ChordsListActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chords_list);
String[] accordi = new String[] {"Do maggiore", "Re maggiore", "Si maggiore"};
String[] note = new String[] {"note: Do, Mi, Sol", "note: La, Re, Fa#", "note: Fa#, Si, Re#"};
int[] immagini = new int[] {R.drawable.do_maggiore, R.drawable.re_maggiore, R.drawable.si_maggiore};
CustomListAdapter adapter = new CustomListAdapter(getApplicationContext(), accordi, note, immagini);
setListAdapter(adapter);
}
}
I was kinda able to solve it by extending Activity instead of ListActivity but then I cannot use the setListAdapter() method.
I'd really appreciate if someone could help me fix the code!
please let me know if you need me to post the custom Adapter or any other file.
Edit: added the whole stack trace
FATAL EXCEPTION: main
Process: dancam.com.chords, PID: 3080
java.lang.RuntimeException: Unable to start activity ComponentInfo{dancam.com.chords/dancam.com.chords.ChordsListActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ListActivity.onContentChanged(ListActivity.java:243)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:398)
at android.app.Activity.setContentView(Activity.java:2166)
at dancam.com.chords.ChordsListActivity.onCreate(ChordsListActivity.java:13)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

This is problem with ListView.
Go to R.layout.activity_chords_list and add <ListView> and set id android:id="#android:id/list"
Like Exception says: 'Your content must have a ListView whose id attribute is 'android.R.id.list'
It should works if rest of the code is ok.

When you extend from ListActivity, it already has ListView, but Activity doesn't. You have to create list inbActivity's layout- better use RecyclerView instead of List view. In your activity create RecyclerView reference, initialize it in onCreate. Create adapter for your data in activity (onCreate method). Finnaly, set adapter to listview reference. In order to modify data in a list, set new data in adapter and call .notifyDatasetChange () on it. Notice, for RecyclerView you will also need LayoutManager (see docs)

Related

Android setText problem error with editText [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
I'm trying to set a textview to a editText text. When I do that I have this error:
Process: com.example.game, PID: 9829
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.game.MainActivity.onClick(MainActivity.java:79)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
It's strange because when I do system.out.println with the text I get
I/System.out: Player1
Can someone help me ?
My code:
System.out.println(p1.getText().toString());
System.out.println(p1.getEditableText().toString());
j1.setText(p1.getEditableText().toString());
j2.setText(p2.getEditableText().toString());
p1 and p2 are editText and j1 and j2 are TextView. I don't know if it's the problem but they are in different resource layout.
All your views are needed to be initialized after setting setContentView(R.layout.activity_main)
However in your case you have them in different resource layout.
let's say you want to have views of layout_xyz in activity_main.
in activity_main
<include
android:id="#+id/xyzView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/layout_xyz" />
In MainActivity.java
initialize views from layout_xyz
View xyzView = findViewById(R.id.xyzView);
TextView j1FromXyz = xyzView.findViewById(R.id.j1);
TextView j2FromXyz = xyzView.findViewById(R.id.j2);
For more: Re-using layouts with <include/>

Not able to intent to next layout

i'm working on my project and i created an interface of my app.
now i want that when ever i click in image view of Automation i go to my next layout. So i tried to write this code
the layout in which i want to move after clicking Automation is this :
But when i'm running app it's saying
Application error
Unfortunately,animation(name of my project) has stopped.
Logcat:
Caused by: java.lang.NullPointerException
at com.example.animation.dashboard.onCreate(dashboard.java:25)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) 
at android.app.ActivityThread.access$600(ActivityThread.java:156) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:153) 
at android.app.ActivityThread.main(ActivityThread.java:5336) 
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:833) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
at dalvik.system.NativeStart.main(Native Method) 
What's mistake i'm making here ? i'm new to android so kindly help me out here.
Your view variable is not defined and you are trying to use it as it is null. But on the another hand, I dont think you need this, just go with findViewById without this view
It looks like view_rooms is not an activity - the second argument in your intent needs to be the context of the destination.
So if you want to go to new activity change your intent like this:
startActivity(new Intent(dashboard.this, SecondActivity.class));
You are using this in the dashboard activity line number 25
imgauto = view.findViewById(R.id.autoimg)
while your variable view is empty you have not assigned it something and also you are in activity so use it directly like this
imgauto = findViewById(R.id.autoimg)
try this and i am sure it will work perfectly.

Android can not get version name, return String null

First of all sorry for my English, I am a new android developer (hobbies) and I have a problem.
I am sorry but I do not know how to do a better description of my issue. Inded, since 3 days I try to fix this issue but I have no more idea.
I believe to think android studio is crazy, or its probably to me.
So in my project, I have tried to implement an auto update system but to do that I already achieve the part to get the version from my text on server.
But since I have implement the code to get the current version of my app "app name", I have the same error
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{erim.ups/erim.ups.PreLoadScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2361)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:156)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:110)
at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:222)
at android.app.AlertDialog$Builder.(AlertDialog.java:452)
at erim.ups.PreLoadScreen.(PreLoadScreen.java:44)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5461) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
But the line 44 is to declare sharedPreferences, I dont understand what is my problem.
final SharedPreferences data = this.getSharedPreferences("Mes_Parametres", MODE_PRIVATE);
Could you please help me ?
Link of my source: preloadScreen.java
In general, do not call methods that you inherit from Activity until after super.onCreate() has been called in the onCreate() method.
Replace:
final SharedPreferences sharedPrefs = getSharedPreferences("Mes_Parametres", MODE_PRIVATE);
final SharedPreferences data = this.getSharedPreferences("Mes_Parametres", MODE_PRIVATE);
final String monParametreUrl = data.getString("ParametreUrl", null);
with:
SharedPreferences sharedPrefs;
SharedPreferences data;
String monParametreUrl;
Then, change your onCreate() to:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPrefs = getSharedPreferences("Mes_Parametres", MODE_PRIVATE);
data = this.getSharedPreferences("Mes_Parametres", MODE_PRIVATE);
monParametreUrl = data.getString("ParametreUrl", null);
setContentView(R.layout.activity_pre_load_screen);
// rest of your onCreate() code goes here
}
Also, in the future, when you ask a question here on Stack Overflow, please put the relevant source code in the question, rather than as a link. After all, as soon as you delete that file in DropBox, your question can no longer help other developers nearly as well.

Why throws NullPointerException?

I work with SVG format, added a library, all the methods recognized, did everything as in the example and then run NullPointerException ??
I can not understand why ...
Here is my onCreate method in Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
RelativeLayout rlHumanBody = (RelativeLayout) findViewById(R.id.control);
\\But in this line error
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.frontfigure);
Drawable drawable = svg.createPictureDrawable();
rlHumanBody.setBackground(drawable);
}
is such a mistake
FATAL EXCEPTION: main
Process: com.example.android.camera2basic, PID: 4396
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.
android.camera2basic/com.example.android.camera2basic.CameraActivity}:
com.applantation.android.svg.SVGParseException: java.lang.NullPointerException:
Attempt to invoke virtual method 'float java.lang.Float.floatValue()' on a null
object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java :903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: com.applantation.android.svg.SVGParseException: java.lang.
NullPointerException: Attempt to invoke virtual method 'float java.lang.Float.
floatValue()' on a null object reference
at com.applantation.android.svg.SVGParser.parse(SVGParser.java:211)
at com.applantation.android.svg.SVGParser.getSVGFromResource(SVGParser.java:1 10)
at com.example.android.camera2basic.CameraActivity.onCreate(CameraActivity.java:222)
the last line here indicates the line 222 is a line in my onCreate method in which code and crashes.
What am I doing wrong?
Just put the "savedInstanceState" in an "if" block and see what it does.
You might actually be passing a "null" state somehow (not very 'pro' in android, so sorry for the dumb suggestion)
in the class(is it an Activity?) view there is no such view. It usually happens because of one of three reasons:
1) you forgot to setContentView
2) you try to get view before you called setContentView
3) you use a wrong layout in setContentView
please check once for third point

Null object reference on setText in Android app

My app crashes on this setText here. I think probably because it cannot find the id so I looked into to R.java and found the id there. I'm confused why it still crashes.
TextView city = (TextView)findViewById(R.id.city_result);
city.setText(msg[0]);
R.java
public static final int city_result=0x7f080041;
and this code is in the public static final class id method.
Any help will be appreciated! Thanks!!
================
More Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather_report);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
//get message from intent
Intent intent = getIntent();
String[] message = intent.getStringArrayExtra(MainActivity.EXTRA_MESSAGE);
setResult(message);
}
public void setResult(String[] msg)
{
//test
TextView city = (TextView)findViewById(R.id.city_result);
city.setText(msg[0]);
}
This file I just started, elsewhere is just system generated new file code, I didn't edit yet.
Here's logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chen.weather/com.example.chen.weather.WeatherReport}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.chen.weather.WeatherReport.setResult(WeatherReport.java:78)
at com.example.chen.weather.WeatherReport.onCreate(WeatherReport.java:32)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText
Means city TextView object is null due to :
1. setContentView is not called before using findViewById:
2. Not using layout in which TextView is defined with city_result id. make sure you are passing right layout in setContentView
check that your activity is correct!!
setContentView(R.layout.activity_weather_report);
Which layout did you put the TextView? I doubt that in this case, you put the TextView in PlaceHolder fragment layout (not the activity_weather_report.xml layout). If so, putting the TextView in activity_weather_report.xml will solve your problem.
Problem is just the id
Make sure that you are using correct id for TextView in Layout xml file .
I had same issue and it resolved by solving spelling mistake .
it is showing this exception because your textview is not getting data at time of onCeate method.
declare your textview in onCreate method,
put yourtextview.setText(xyz) wherever you are getting that "xyz" value.
Hope this will work.

Categories