I'm an android newbie
I was following this tutorial Android splash screen howto, it's a little outdated, but anyways.
I just add the new layout file splash.xml into res/layout, and then the image into drawable/mdpi, then I change the main image into MainActivity.java, ie:
setContentView(R.layout.activity_main);
to
setContentView(R.layout.splash);
Being activity_main the name of the default layout, activity_main.xml, and splash the name of the other layout called splash.xml.
But application stops, says the application has unfortunately stopped, and I can't even access it.
I don't know what am I missing here, here's the interesting code on MainActivity.java:
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
relativeLayout=(RelativeLayout) findViewById(R.id.containerImg);
relativeLayout.setDrawingCacheEnabled(true);
cameraSurfaceView = (SurfaceView)
findViewById(R.id.surfaceView1);
// cameraSurfaceView.setLayoutParams(new FrameLayout.LayoutParams(640, 480));
cameraSurfaceHolder = cameraSurfaceView.getHolder();
cameraSurfaceHolder.addCallback(this);
// cameraSurfaceHolder.setType(SurfaceHolder.
// SURFACE_TYPE_PUSH_BUFFERS);
btnCapture = (Button)findViewById(R.id.button1);
btnCapture.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
camera.takePicture(cameraShutterCallback,
cameraPictureCallbackRaw,
cameraPictureCallbackJpeg);
}
});
}
My activity_main.xml:
<FrameLayout 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"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/containerImg"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SurfaceView
android:id="#+id/surfaceView1"
android:layout_width="1276px"
android:layout_height="745px"
android:layout_centerInParent="true" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/surfaceView1"
android:layout_alignLeft="#+id/surfaceView1"
android:layout_marginLeft="20px"
android:src="#drawable/mark3" />
</RelativeLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:background="#drawable/camera" />
</FrameLayout>
My splash.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:src="#drawable/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"/>
</LinearLayout>
Any ideas?
Maybe I should use the same activity_main.xml for this purposes, not really sure...
Thanks in advance!
EDIT
Logcat:
3774-3774/com.kkoci.photo E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kkoci.photo/com.kkoci.photo.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
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:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.kkoci.photo.MainActivity.onCreate(MainActivity.java:65)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
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:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
relativeLayout=(RelativeLayout) findViewById(R.id.containerImg);
relativeLayout.setDrawingCacheEnabled(true);
cameraSurfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
these are the views present in your activity_main xml file.. when you change your layout file why you are accessing these?
Related
So, I have this ListView, which I would like that each one of its items had a background (the same background for all of them). So far I've been trying to use this code:
listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<CharSequence> lv = ArrayAdapter.createFromResource(this,
R.array.countries_array, android.R.layout.simple_list_item_1);
listView.setAdapter(lv);
for (Integer i = 0; i < listView.getCount(); ++i) {
View listItem = listView.getChildAt(i);
listItem.setBackgroundResource(R.drawable.edittext_custom);
}
It's simple, I increment i and then use it to know the position in which it is and it will set that items background, and it repeats until all items have the same background. Sound easy. Is there a better way to do this? If not, can someone please help figure it out why this simple code doesn't work? I'm pretty sure I'm missing something basic.
Exception:
05-18 15:08:45.659 10157-10157/com.example.fabiocompany.goquiz W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41917da0)
05-18 15:08:45.679 10157-10157/com.example.fabiocompany.goquiz E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.fabiocompany.goquiz, PID: 10157
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fabiocompany.goquiz/com.example.fabiocompany.goquiz.TopicosActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.fabiocompany.goquiz.TopicosActivity.onCreate(TopicosActivity.java:47)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#drawable/bggoquiz"
tools:context="com.example.fabiocompany.goquiz.TopicosActivity"
android:focusable="true"
android:focusableInTouchMode="true">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/searchView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:divider="#android:color/transparent"
android:dividerHeight="20.0sp"
/>
<SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchView"
android:background="#drawable/edittext_custom"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
If you need something else, please do tell, I'll try to help.
Instead of doing all this you could create a new xml file named listitem.xml with textview as below
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tv_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background = "<give your desired background>"
/>
Modify your ArrayAdapter to
ArrayAdapter<String> lv = new ArrayAdapter<String>(this,
R.layout.listitem, R.id.tv_item, <your array>);
As you are applying same background to all items simply setting it ones in xml will do the work. You do not require to loop through list.
Problem is with your this method,.listView.getChildAt(i); this works only on the items which are currently visible
So this is how you need to use them.
final int numVisibleChildren = listView.getChildCount();
final int firstVisiblePosition = listView.getFirstVisiblePosition();
for (int i = firstVisiblePosition; i < firstVisiblePosition + numVisibleChildren; i++) {
View view = listView.getChildAt(i - firstVisiblePosition);
listItem.setBackgroundResource(R.drawable.edittext_custom);
}
Assuming that listView.getChildAt is returning null because the corresponding position has an invisible (or non-existent) object, then simply test for null ...
for (Integer i = 0; i < listView.getCount(); ++i) {
View listItem = listView.getChildAt(i);
if (listItem != null) {
listItem.setBackgroundResource(R.drawable.edittext_custom);
}
}
Hey im a begginer in android coding and ive been following this series on youtube. Whenever I run my project as an android application and go to the emulator to select it, it gives me "Sorry- The application (Appname) has stopped unexpectedly. Please try again" Eclipse doesnt give me any error line to where the problem is and so it is very frustrating.. Ive been looking on the internet for answers to this, but I havent been able to fix it. I think it has something to do with my Manifest. Whenever i run my application there are errors on my LogCat.
My application is a small app that when selected will first open a logo screen for 3 seconds then it will take you to an interface where there are 2 buttons to choose from. Both buttons lead to this thing that edits text.
Ive posted all my code below and please guys keep in mind when you answer that I am a begginer and might not understand some terms. Thank you!
Here is my LogCat when i try to start it
07-20 20:19:17.382: D/dalvikvm(254): GC_EXTERNAL_ALLOC freed 663 objects / 51912 bytes in 177ms
07-20 20:19:19.791: D/AndroidRuntime(254): Shutting down VM
07-20 20:19:19.801: W/dalvikvm(254): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-20 20:19:19.811: E/AndroidRuntime(254): FATAL EXCEPTION: main
07-20 20:19:19.811: E/AndroidRuntime(254): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.thepasics/com.example.thepasics.Menu}: java.lang.ClassNotFoundException: com.example.thepasics.Menu in loader dalvik.system.PathClassLoader[/data/app/com.example.thepasics-2.apk]
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.os.Looper.loop(Looper.java:123)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-20 20:19:19.811: E/AndroidRuntime(254): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 20:19:19.811: E/AndroidRuntime(254): at java.lang.reflect.Method.invoke(Method.java:521)
07-20 20:19:19.811: E/AndroidRuntime(254): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-20 20:19:19.811: E/AndroidRuntime(254): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-20 20:19:19.811: E/AndroidRuntime(254): at dalvik.system.NativeStart.main(Native Method)
07-20 20:19:19.811: E/AndroidRuntime(254): Caused by: java.lang.ClassNotFoundException: com.example.thepasics.Menu in loader dalvik.system.PathClassLoader[/data/app/com.example.thepasics-2.apk]
07-20 20:19:19.811: E/AndroidRuntime(254): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
07-20 20:19:19.811: E/AndroidRuntime(254): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
07-20 20:19:19.811: E/AndroidRuntime(254): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-20 20:19:19.811: E/AndroidRuntime(254): ... 11 more
My Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thepasics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.thepasics.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thePasics.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TutorialOne"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thePasics.TUTORIALONE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I have 3 Java files which are Menu.java Main.java TutorialOne.java. This one is the Main one
package com.example.thepasics;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class Main extends Activity {
MediaPlayer logoMusic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
logoMusic = MediaPlayer.create(Main.this, R.raw.music);
logoMusic.start();
Thread logoTimer = new Thread(){
public void run(){
try{
sleep(2000);
Intent menuIntent = new Intent("com.example.thePasics.MENU");
startActivity(menuIntent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
logoMusic.release();
}
}
This One is the menu.java
package com.example.thepasics;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class menu extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Button sound
final MediaPlayer buttonSound = MediaPlayer.create(menu.this, R.raw.buttonsound);
//Setting up button references
Button tut1 = (Button) findViewById(R.id.button1);
Button tut2 = (Button) findViewById(R.id.button2);
tut1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.thepasics.TUTORIALONE"));
}
});
tut2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.thepasics.TutorialOne"));
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
The third one is the TutorialOne.java
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class TutorialOne extends Activity implements OnCheckedChangeListener{
TextView textOut;
EditText textIn;
RadioGroup gravityG, styleG;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
textOut = (TextView) findViewById(R.id.tvChange);
textIn = (EditText) findViewById(R.id.editText1);
gravityG = (RadioGroup) findViewById(R.id.rgGravity);
gravityG.setOnCheckedChangeListener(this);
styleG = (RadioGroup) findViewById(R.id.rgStyle);
styleG.setOnCheckedChangeListener(this);
Button gen = (Button) findViewById(R.id.bGenerate);
gen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textOut.setText(textIn.getText());
}
});
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.rbLeft:
textOut.setGravity(Gravity.LEFT);
break;
case R.id.rbCenter:
textOut.setGravity(Gravity.CENTER);
break;
case R.id.rbRight:
textOut.setGravity(Gravity.RIGHT);
break;
case R .id.rbNormal:
textOut.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL), Typeface.NORMAL);
break;
case R .id.rbItalic:
textOut.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC), Typeface.ITALIC);
break;
case R .id.rbBold:
textOut.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD), Typeface.BOLD);
break;
}
}
}
My 3 xml files are splash, tutorial1 and main
this one is main.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:background="#drawable/backgroundwithoutericapp"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Choose a function" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button1"
android:textSize="25dp"
android:textStyle="bold"
android:id="#+id/button1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button2"
android:textSize="25dp"
android:textStyle="bold"
android:id="#+id/button2"/>
</LinearLayout>
second one is splash.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:background="#drawable/background">
</LinearLayout>
third one is tutorial1.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:background="#drawable/tutorialonebackground"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/tvStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:text="Style"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGravity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:text="Gravity"
android:textSize="25dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<RadioGroup
android:id="#+id/rgStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<RadioButton
android:id="#+id/rbNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Normal" />
<RadioButton
android:id="#+id/rbItalic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Italic" />
<RadioButton
android:id="#+id/rbBold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bold" />
</RadioGroup>
<RadioGroup
android:id="#+id/rgGravity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<RadioButton
android:id="#+id/rbLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left" />
<RadioButton
android:id="#+id/rbCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Center" />
<RadioButton
android:id="#+id/rbRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="#+id/tvChange"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Type in Text and Press the Button Below" />
<Button
android:id="#+id/bGenerate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Generate" />
</LinearLayout>
You have specified wrong class name in manifest.xml
Replace
<activity
android:name="Menu"
android:label="#string/app_name" >
with
<activity
android:name="com.example.thepasics.menu"
android:label="#string/app_name" >
Your Manifest and menu activity don't match. Your manifest has
<activity
android:name="Menu" ...
which first of all isn't a valid name. It could be either android:name=".Menu" to refer to com.com.example.thepasics.Menu or you could use the fully qualified name (as you did for com.example.thepasics.Main).
In addition, your class is named menu, not Menu - remember it is case sensitive. Java convention has class names starting with a capital letter, so it should probably be corrected to Menu.
Your Menu class is actually called "menu" and not "MENU".
public class menu extends Activity{
Try changing "com.example.thePasics.MENU" to "com.example.thePasics.menu" throughout your app.
I only have looked into your Logcat:
07-20 20:19:19.801: W/dalvikvm(254): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-20 20:19:19.811: E/AndroidRuntime(254): FATAL EXCEPTION: main
07-20 20:19:19.811: E/AndroidRuntime(254): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.thepasics/com.example.thepasics.Menu}: java.lang.ClassNotFoundException: com.example.thepasics.Menu in loader dalvik.system.PathClassLoader[/data/app/com.example.thepasics-2.apk]
Your app is crashing becuase your com.example.thepasics.Menu is not being found, and also use android:name=".Menu" instead of android:name="Menu".
I am using this library https://github.com/chrisbanes/Android-PullToRefresh for implementing a refreshable grid view and i am getting this error:
android.view.InflateException: Binary XML file line #8: Error inflating class com.handmark.pulltorefresh.library.PullToRefreshGridView
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at com.example.retrievetweets.FragmentPhotos.onCreateView(FragmentPhotos.java:55)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:238)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
... 24 more
Caused by: java.lang.NullPointerException
at com.handmark.pulltorefresh.library.internal.IndicatorLayout.<init>(IndicatorLayout.java:66)
at com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.addIndicatorViews(PullToRefreshAdapterViewBase.java:355)
at com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.updateUIForMode(PullToRefreshAdapterViewBase.java:328)
at com.handmark.pulltorefresh.library.PullToRefreshBase.init(PullToRefreshBase.java:1142)
at com.handmark.pulltorefresh.library.PullToRefreshBase.<init>(PullToRefreshBase.java:113)
at com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.<init>(PullToRefreshAdapterViewBase.java:74)
at com.handmark.pulltorefresh.library.PullToRefreshGridView.<init>(PullToRefreshGridView.java:35)
... 27 more
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- The PullToRefreshGridView replaces a standard GridView widget. -->
<com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="#+id/pull_refresh_grid"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="1dp"
android:columnWidth="80dp"
android:stretchMode="columnWidth"
android:gravity="fill"
ptr:ptrMode="both"
ptr:ptrDrawable="#drawable/default_ptr_rotate"
/>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:indeterminateOnly="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
And here is the java code for the method onCreateView in FragmentPhotos (the line 55 is this one "ll = (RelativeLayout) inflater.inflate(R.layout.activity_grid_view,"):
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ref = "";
fa = getActivity();
ll = (RelativeLayout) inflater.inflate(R.layout.activity_grid_view,
container, false);
bar = (ProgressBar) ll.findViewById(R.id.progressBar);
mPullRefreshGridView = (PullToRefreshGridView) ll
.findViewById(R.id.pull_refresh_grid);
mGridView = mPullRefreshGridView.getRefreshableView();
// Set a listener to be invoked when the list should be refreshed.
mPullRefreshGridView
.setOnRefreshListener(new OnRefreshListener<GridView>() {
#Override
public void onRefresh(
PullToRefreshBase<GridView> refreshView) {
// TODO Auto-generated method stub
if (NetworkReceiver.mobileConnected) {
new GetDataTaskWhitoutLoading().execute();
} else {
Toast.makeText(
getActivity(),
"Ahora mismo no se pueden cargar nuevos datos."
+ " Comprueba la conexi—n a Internet.",
Toast.LENGTH_LONG).show();
mPullRefreshGridView.onRefreshComplete();
}
}
});
mGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
// Do something in response to the click
Intent intent = new Intent(fa, ImageSelected.class);
intent.putExtra("URL_image", mListItems.get(position)
.subSequence(0, mListItems.get(position).length() - 6));
startActivity(intent);
}
});
mListItems = new ArrayList<String>();
mAdapter = new FotosItemAdapter(fa, R.layout.image_group, mListItems);
mGridView.setAdapter(mAdapter);
if (NetworkReceiver.mobileConnected) {
GetDataTask.newInstance(bar).execute();
}
return ll;
}
Thank you!!
Are you importing in your classhpath on eclipse?
Pull to refhresh is a lib for android,then import like this:
project properties> android> add lib.
In my case it solved by adding this to my project.properties :
target=android-19
android.library.reference.1=../library
that library is the name of the handmark library.
hope be useful.
Please have a look at the following code
activity_form.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"
tools:context=".Form" >
<ImageView
android:id="#+id/logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/logo" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/logo"
android:layout_marginTop="10dp"
android:text="#string/title" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_marginTop="10dp"
/>
</RelativeLayout>
list_layout.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="horizontal" >
<ImageView
android:id="#+id/listImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/star" />
<TextView
android:id="#+id/listText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Form.java
package com.example.callmanager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Form extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
ListView lv = (ListView)findViewById(android.R.id.list);
String arr[] = getResources().getStringArray(R.array.branch_list);
lv.setAdapter(new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.listText,arr));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_form, menu);
return true;
}
private class MyAdapter extends ArrayAdapter
{
public MyAdapter(Context context, int resource, int textViewResourceId,
Object[] objects) {
super(context, resource, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View contentView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_layout, parent,false);
TextView tv = (TextView)findViewById(R.id.listText);
ImageView iv = (ImageView)findViewById(R.id.listImage);
String arr[] = getResources().getStringArray(R.array.branch_list);
tv.setText(arr[position]);
if(arr[position].equals("Anuradhapura"))
iv.setImageResource(R.drawable.star);
return view;
}
}
}
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Esoft Call Manager</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title"><b>Select a Branch To Call</b></string>
<string-array name="branch_list">
<item>Anuradhapura</item>
<item>Avissawella</item>
</string-array>
</resources>
When I run this code, I get the 'NullPointException'. I am adding the complete log file here
02-08 21:04:01.463: D/dalvikvm(490): GC_EXTERNAL_ALLOC freed 43K, 53% free 2551K/5379K, external 1949K/2137K, paused 82ms
02-08 21:04:01.713: D/AndroidRuntime(490): Shutting down VM
02-08 21:04:01.713: W/dalvikvm(490): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-08 21:04:01.733: E/AndroidRuntime(490): FATAL EXCEPTION: main
02-08 21:04:01.733: E/AndroidRuntime(490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.callmanager/com.example.callmanager.Form}: java.lang.NullPointerException
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.os.Looper.loop(Looper.java:123)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-08 21:04:01.733: E/AndroidRuntime(490): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 21:04:01.733: E/AndroidRuntime(490): at java.lang.reflect.Method.invoke(Method.java:507)
02-08 21:04:01.733: E/AndroidRuntime(490): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-08 21:04:01.733: E/AndroidRuntime(490): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-08 21:04:01.733: E/AndroidRuntime(490): at dalvik.system.NativeStart.main(Native Method)
02-08 21:04:01.733: E/AndroidRuntime(490): Caused by: java.lang.NullPointerException
02-08 21:04:01.733: E/AndroidRuntime(490): at com.example.esoftcallmanager.Form.onCreate(Form.java:24)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-08 21:04:01.733: E/AndroidRuntime(490): ... 11 more
I am adding the folder structure below if it helps
Why I am getting this error? How to solve this? Please help!
try this:
change this
ListView lv = (ListView)findViewById(android.R.id.list);
for:
ListView lv = (ListView)findViewById(R.id.list);
Normally they use android.R.id.list when class extends a ListActivity
Change:
ListView lv = (ListView)findViewById(android.R.id.list);
to
ListView lv = (ListView)findViewById(R.id.list);
Unless you have an id in your XML in the format of android:id="#android:id/myID", you don't need to use android.R. Whenever you use one of your own IDs, you can simply use R.id.Whatever
The issue is right here. ListView lv = (ListView)findViewById(android.R.id.list); you reference the Id android.R.id.list
however in your layout you reference android:id="#+id/list" which creates an Id in the Resources class called list. so you should be referencing R.id.list if you debug you will find that the lv variable is null because it wasn't found because you referenced the wrong id.
If your are using your own layout you don't need any more to write android.R just remove android and reorganize your class
Try this.
I hope it will solve your NPE.
Change it
ListView lv = (ListView)findViewById(android.R.id.list);
to
ListView lv = (ListView)findViewById(R.id.list);
Actually You have defined the list view in the layout activity_form.xml have name list so you need to write the R.id.list.
I managed to solve this by my self. Following edits were required.
activity_form.xml
The "id" of the ListView should be
android:id="#android:id/list"
Form.java
It should call the ListView in this way
ListView lv = (ListView) findViewById(android.R.id.list);
Inside getView(), the initialization of the TextView and ImageView should be like this
TextView tv = (TextView) view.findViewById(R.id.listText);
ImageView iv = (ImageView) view.findViewById(R.id.listImage);
Note the view.findViewById() part.
Below i have pasted copy of my java,xml and logcat of the application.
Here goes my java code
package my.example.myproject;
import my.example.myproject.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*
* #see SystemUiHider
*/
public class FullscreenActivity extends Activity {
/**
* Whether or not the system UI should be auto-hidden after
* {#link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
*/
private static final boolean AUTO_HIDE = true;
/**
* If {#link #AUTO_HIDE} is set, the number of milliseconds to wait after
* user interaction before hiding the system UI.
*/
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
/**
* If set, will toggle the system UI visibility upon interaction. Otherwise,
* will show the system UI visibility upon interaction.
*/
private static final boolean TOGGLE_ON_CLICK = true;
/**
* The flags to pass to {#link SystemUiHider#getInstance}.
*/
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
/**
* The instance of the {#link SystemUiHider} for this activity.
*/
private SystemUiHider mSystemUiHider;
Button iol,help,about;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
final View controlsView = findViewById(R.id.fullscreen_content_controls);
iol = (Button)findViewById(R.id.iolCalculation);
help = (Button)findViewById(R.id.sHelp);
about = (Button)findViewById(R.id.sAbout);
Thread iolthread=new Thread(){
public void run(){
try{
iol.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.selection);
}
});
Intent iolIntent=new Intent(FullscreenActivity.this,Selection.class);
startActivity(iolIntent);
}
finally{
finish();
}
}
};
iolthread.start();
// Set up an instance of SystemUiHider to control the system UI for
// this activity.
mSystemUiHider = SystemUiHider.getInstance(this, controlsView,
HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider
.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
// Cached values.
int mControlsHeight;
int mShortAnimTime;
#Override
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean visible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.
if (mControlsHeight == 0) {
mControlsHeight = controlsView.getHeight();
}
if (mShortAnimTime == 0) {
mShortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
controlsView
.animate()
.translationY(visible ? 0 : mControlsHeight)
.setDuration(mShortAnimTime);
} else {
// If the ViewPropertyAnimator APIs aren't
// available, simply show or hide the in-layout UI
// controls.
controlsView.setVisibility(visible ? View.VISIBLE
: View.GONE);
}
if (visible && AUTO_HIDE) {
// Schedule a hide().
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
}
});
// Set up the user interaction to manually show or hide the system UI.
controlsView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide(100);
}
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
#Override
public void run() {
mSystemUiHider.hide();
}
};
/**
* Schedules a call to hide() in [delay] milliseconds, canceling any
* previously scheduled calls.
*/
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
}
XML
<FrameLayout 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:background="#color/black"
tools:context=".FullscreenActivity"
android:id="#+id/frame_layout">
<LinearLayout
android:id="#+id/fullscreen_content_controls"
style="?buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent" >
</LinearLayout>
<Button
style="#style/ButtonBarButton"
android:layout_width="222dp"
android:layout_height="62dp"
android:layout_marginLeft="45dp"
android:layout_marginTop="75dp"
android:background="#color/black"
android:paddingLeft="20dp"
android:id="#+id/iolCalculation"
android:text="#string/Button"
android:textColor="#color/WhiteSmoke"
android:textStyle="bold" />
<Button
style="#style/ButtonBarButton"
android:layout_width="222dp"
android:layout_height="62dp"
android:layout_marginLeft="45dp"
android:id="#+id/sHelp"
android:layout_marginTop="139dp"
android:background="#color/Black"
android:gravity="center"
android:paddingLeft="10dp"
android:text="#string/Button1"
android:textColor="#color/WhiteSmoke"
android:textStyle="bold" />
<Button
style="#style/ButtonBarButton"
android:layout_width="222dp"
android:layout_height="62dp"
android:id="#+id/sAbout"
android:layout_marginLeft="45dp"
android:layout_marginTop="203dp"
android:background="#color/Black"
android:paddingLeft="10dp"
android:text="#string/Button2"
android:textColor="#color/WhiteSmoke"
android:textStyle="bold" >
</Button>
</FrameLayout>
logcat
12-16 10:48:26.975: E/Trace(767): error opening trace file: No such file or directory (2)
12-16 10:51:38.199: E/AndroidRuntime(767): FATAL EXCEPTION: main
12-16 10:51:38.199: E/AndroidRuntime(767): java.lang.NullPointerException
12-16 10:51:38.199: E/AndroidRuntime(767): at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:168)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.view.View.performClick(View.java:4202)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.view.View$PerformClick.run(View.java:17340)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.os.Handler.handleCallback(Handler.java:725)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.os.Looper.loop(Looper.java:137)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-16 10:51:38.199: E/AndroidRuntime(767): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 10:51:38.199: E/AndroidRuntime(767): at java.lang.reflect.Method.invoke(Method.java:511)
12-16 10:51:38.199: E/AndroidRuntime(767): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-16 10:51:38.199: E/AndroidRuntime(767): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-16 10:51:38.199: E/AndroidRuntime(767): at dalvik.system.NativeStart.main(Native Method)
i have tried every possible solution in stackoverflow but in vain.help me. I think mostly there is a problem in xml to java display content.Also do i need to put in id for every layout in xml and how does the java code detect the layout to be displayed
here goes my selection java code and xml
package com.example.iolcalci;
import android.app.Activity;
import android.os.Bundle;
public class Selection extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selective);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:orientation="vertical" >
<Spinner
android:id="#+id/formulae"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="#color/LightSkyBlue"
android:prompt="#string/prompt"
android:entries="#array/formulas" />
<TextView
android:id="#+id/k2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/k1"
android:layout_below="#+id/k1"
android:layout_marginTop="38dp"
android:text="#string/K2"
android:textColor="#color/White"
android:textSize="25sp" />
<TextView
android:id="#+id/al_const"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/al"
android:layout_below="#+id/al"
android:layout_marginTop="44dp"
android:text="#string/Rx"
android:textColor="#color/White"
android:textSize="20sp" />
<TextView
android:id="#+id/al"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/k2"
android:layout_below="#+id/k2"
android:layout_marginTop="38dp"
android:text="#string/AL"
android:textColor="#color/White"
android:textSize="25sp" />
<EditText
android:id="#+id/k2_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/al"
android:layout_alignRight="#+id/al_editText"
android:layout_alignTop="#+id/k2"
android:layout_marginLeft="120dp"
android:background="#color/black"
android:ems="10"
android:inputType="numberDecimal"
android:textColorLink="#color/white" />
<EditText
android:id="#+id/al_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/formulae"
android:layout_alignTop="#+id/al"
android:layout_marginLeft="120dp"
android:ems="10"
android:inputType="numberDecimal" />
<EditText
android:id="#+id/al_const_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/al_const"
android:layout_alignRight="#+id/al_editText"
android:layout_marginLeft="120dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="#+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#color/LightSkyBlue"
android:text="#string/Result" />
<EditText
android:id="#+id/k1_editText"
android:layout_width="wrap_content"
android:layout_height="37dp"
android:layout_alignLeft="#+id/k2_editText"
android:layout_alignRight="#+id/formulae"
android:layout_below="#+id/formulae"
android:layout_marginTop="30dp"
android:background="#color/black"
android:ems="10"
android:inputType="numberDecimal"
android:textColorLink="#color/White" />
<TextView
android:id="#+id/k1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/k1_editText"
android:layout_alignParentLeft="true"
android:layout_marginLeft="27dp"
android:text="#string/K1"
android:textColor="#color/white"
android:textSize="25sp" />
</RelativeLayout>
THE NEW LOGCAT
12-21 10:44:50.419: W/Trace(766): Unexpected value from nativeGetEnabledTags: 0
12-21 10:44:50.573: W/Trace(766): Unexpected value from nativeGetEnabledTags: 0
12-21 10:44:50.573: W/Trace(766): Unexpected value from nativeGetEnabledTags: 0
12-21 10:44:50.923: D/AndroidRuntime(766): Shutting down VM
12-21 10:44:50.923: W/dalvikvm(766): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-21 10:44:50.933: E/AndroidRuntime(766): FATAL EXCEPTION: main
12-21 10:44:50.933: E/AndroidRuntime(766): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iolcalci/com.example.iolcalci.MainActivity}: java.lang.NullPointerException
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.os.Looper.loop(Looper.java:137)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-21 10:44:50.933: E/AndroidRuntime(766): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 10:44:50.933: E/AndroidRuntime(766): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 10:44:50.933: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-21 10:44:50.933: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-21 10:44:50.933: E/AndroidRuntime(766): at dalvik.system.NativeStart.main(Native Method)
12-21 10:44:50.933: E/AndroidRuntime(766): Caused by: java.lang.NullPointerException
12-21 10:44:50.933: E/AndroidRuntime(766): at com.example.iolcalci.MainActivity.onCreate(MainActivity.java:23)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.Activity.performCreate(Activity.java:5104)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-21 10:44:50.933: E/AndroidRuntime(766): ... 11 more
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.iolcalci"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.iolcalci.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="Selection"
android:exported="false"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.example.iolcalci.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Judging from your code I'm going to go with the conjecture that this line is causing the error:
setContentView(R.id.selection);
There's no need to change the contentView of the activity from which you're calling the "selection" activity. You should read up on intents. I'm assuming that when the button is clicked, you want the new activity and it's content to pop up. For that, do this :
try{
iol.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent iolIntent=new Intent(FullscreenActivity.this,Selection.class);
startActivity(iolIntent);
}
});
}
Also set your button's layout width and layout height to "wrap_content". Definite measurements won't work well because there are many resolutions possible.
android:layout_height="wrap_content"
android:layout_width="wrap_content"
In case of more errors, post the new logcat, please.
I found this in your selection.java:
setContentView(R.layout.selective);
This means you should have a selective.xml file in your res/layout/ folder.
This selective.xml file should contain a definition of EditText.
Example,
<EditText
android:id="#+id/k1_editText"
android:layout_width="wrap_content"
android:layout_height="37dp"
android:layout_alignLeft="#+id/k2_editText"
android:layout_alignRight="#+id/formulae"
android:layout_below="#+id/formulae"
android:layout_marginTop="30dp"
android:background="#color/black"
android:ems="10"
android:inputType="numberDecimal"
android:textColorLink="#color/White" />
You need to reference this in the activity file with this piece of code:
EditText ed=new EditText findViewById(R.id.k2_editText);
Now the EditText field will be displayed.
If you want to make your EditText object global, declare it in the class and then initialize it after setting content view. Like this,
public class Selection extends Activity {
EditText ed;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selective);
EditText ed=new EditText findViewById(R.id.k2_editText);
}
Make sure you always initialize a widget object AFTER the creation of the activity and setting content view, because otherwise the compiler is going to have to initialize something which hasn't been referenced yet. The layout file is referenced only after setting content view. Only after setting the layout file can you initialize objects to whatever is present inside the layout file.
If you like the answer, do vote it up.