Why the APK crash when I open it [duplicate] - android

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Null pointer Exception - findViewById()
(12 answers)
Closed 3 years ago.
I can not open the APK normally, but the debugger has no serious problems. I consider that the problem may occur in here.
MainActivity.java:
private TextView text;
public static Toast toast = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
text.setText("IP address : "+getLocalHostIp());
new ServerListener().start();
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="horizontal"
tools:context=".MainActivity">
<TextView
android:id="#+id/setText"
android:layout_width="match_parent"
android:layout_height="51dp"
android:layout_weight="1" />
</LinearLayout>

You are using the wrong id for textView in your code which is causing the app to crash.
You've defined setText as id for the TextView in your .xml file android:id="#+id/setText", and you are using "text" as the id to map the TextView in your code text = (TextView) findViewById(R.id.text);, which obviously can not be found and is the cause of the problem.
to solve this, do one of the following:
Change text = (TextView) findViewById(R.id.text); to text = (TextView) findViewById(R.id.setText);
or change android:id="#+id/setText" to android:id="#+id/text"

Your id for TextView is wrong, you can use below code
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="51dp"
android:layout_weight="1" />

Related

Set Button Text On Create not set text as define

I have a code to change text as on create
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Button btnbut = FindViewById<Button>(Resource.Id.btnbut);
TextView txtvw = FindViewById<TextView>(Resource.Id.txtView);
txtvw.Text = "Hello World";
btnbut.Text = "Hello WOrld";
}
}
My main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout1">
<TextView
android:layout_width="match_parent"
android:layout_height="47.5dp"
android:id="#+id/txtView"
android:layout_marginLeft="1.0dp"
android:layout_marginRight="0.0dp" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnbut" />
</LinearLayout>
When I run and open app with emulator the Text has remain the same as define on main.axml not the text define OnCreate.
Your code is correct and should result in:
Try performing a clean all/build all on your solution and retry your app on the emulator again.
Note: If it is still not updating the app on the emulator correctly:
Within the emulator:
Setting / Apps / Select your app / *Uninstall*
i think i got your i dont use xamrain but i think this is the error try changing
SetContentView(Resource.Layout.Main);
to
SetContentView(Resource.Layout.My main);
coz your layout name is "My main" not "main"

Error in findViewById in Android

I am getting error in speed = (TextView) findViewById(R.id.speed); saying "speed cannot be resolved or it is not a field".
My layout is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SpeedometerActivity" >
<Button
android:id="#+id/download"
android:layout_width="150dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="13dp"
android:layout_marginLeft="300dp"
android:text="Begin Test" />
<TextView
android:id="#+id/speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/meter"
android:layout_marginLeft="191dp"
android:layout_marginTop="55dp"
android:layout_toRightOf="#+id/meter"
android:text="TextView"
android:textSize="20dp"
android:textColor="#000000"/>
</RelativeLayout>
My MainActivity:
public class MainActivity extends Activity {
TextView speed;
Button download;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.speedometer);
download = (Button)findViewById(R.id.download);
}
#Override
protected void onResume() {
super.onResume();
h = new Handler();
new speedTask().execute();
speed = (TextView) findViewById(R.id.speed);
}
}
Please anybody help me to solve this problem.
"speed cannot be resolved or it is not a field"
Check if any errors in your resource files. Follow the suggestion by blackbelt. If you have errors in resource files your R.java will not be generated. Fix it. also check if you have import android.R; if so remove it.
Further you have
<TextView
android:id="#+id/speed"
Its a textview
Casting it to button
speed = (Button)findViewById(R.id.speed);
Should be
TextView speed =(TextView) findViewById(R.id.speed);
You should check if the R class you imported is the one of your project and not the android's one. Also you are casting to the wrong object, as correctly stated from #Raghunandan
Here you are type cast TextView to Button. So please change your code as below.
TextView speed = (Button)findViewById(R.id.speed);
Note : Here you are not able to import your project R file, first please check your xml file and try to resolve xml error first.
Please remove import android.R; from your activity if you have import it.
Go to your MainActivity.Java file and Press CTRL+SHIFT+O. It will automatically import all necessary packages.
It has created a duplicate so give as follows
Button speed1;
speed1=(Button)findViewById(R.id.speed);
It will work!

Using a layout with an activity

So I just started playing around with android and I'm trying to see what things I can do. I was following the very first android tutorial: http://developer.android.com/training/basics/firstapp/index.html and at the very end, you programmatically define a TextView. I wanted to change this to be defined in a new layout, so I wrote this (it is named display_message.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"
android:orientation="vertical" >
<TextView android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
And in the DisplayMessage class, I changed it to this:
public class DisplayMessage extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.display_message);
// Get message from intent
Intent intent = getIntent();
String message = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE);
// Get the text view
TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(message);
}
}
However, Eclipse says that it doesn't know what R.layout.display_message is, nor does it know what R.id.text_view is. Is there somewhere else I need to define them or something? Where did I mess up?
It seems correct but be sure that the R class imported is the correct one.
Sometimes Eclipse imports android.R but the R file you need to import is your.package.name.R

How to show selected View in an Activity first

I have a activity in Android TextView followed ListView again TextView. The content of first TextView fills the whole screen and in order to see the ListView and second TextView the user has to scrolldown. Now I wish to show the second TextView when activity start instead of first TextView.
Instead I want TextView2 to be visible when activity starts.
EDITED
Sample code as per advice of #Urban
public class FocusTestActivity extends Activity {
private static final String TAG = "FocusTestActivity";
ScrollView scroll;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text1 = (TextView) findViewById(R.id.text1);
TextView text2 = (TextView) findViewById(R.id.text2);
TextView text3 = (TextView) findViewById(R.id.text3);
String hello = "We provide services to K-12 education sector."
+ "Our services include Feasibility report, Marketing, Curriculum design"
+ "Teachers planning, Design and Technology, Parents expectation"
+ "management, Transport planning, Day-to-day operations and Statutory"
+ "compliances. Please find a brief introduction attached with mail. Also visit"
+ "www.wissenways.com We can help you with overall process of setting-up school. We have"
+ "experience include establishing International, pre-school and CBSE"
+ "schools from scratch. Please feel free to contact if you need some help in your school venture."
+ "Best of Luck!<br/><br/>";
text1.setText(Html.fromHtml(hello));
text2.setText(Html.fromHtml(hello));
text3.setText(Html.fromHtml(hello));
ScrollView scroll = (ScrollView) findViewById(R.id.scrollcontainer);
}
public void onStart() {
super.onStart();
Log.d(TAG,"Inside onStart");
scroll.post(new Runnable() {
#Override
public void run() {
scroll.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
}
XML Code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollcontainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:textSize="17sp"/>
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:textSize="17sp"/>
<TextView
android:id="#+id/text3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:textSize="17sp"/>
</LinearLayout>
</ScrollView>
LOGCAT output:
getScrollView().post(new Runnable() {
#Override
public void run() {
getScrollView().fullScroll(ScrollView.FOCUS_DOWN);
}
});
Using this code in the onCreate should start the scrollview (in which your list and two text views are) at the bottom position. I dont know why you would want to do that, and im guessing there could be a better way to do what you want rather than forcing the scroll position to the bottom, but anyways...
This is the question from where the above code is taken from, you couldve searched for it.
Hope this helped.
Here are 3 simple options i can give you:
Change there places
Make another activity with the second textview and launch that activity first then show you current one
Show a Toast with the text in the second textview at the start of your activity (problem with this one is the user closes it and you want to show it again or user wants to see it again you need to launch a new toast)
I don't fully understand your difficulty with this problem
EDIT:
when must the first textview display at the top? if never just do the first option :\ if under some event, have a empty textview at the top and at the bottom and by code change it's text to what you want at the moment you want
You could also use in your xml file a RealtiveLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:id="#+id/txtview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ListView
android:id="#id/android:list"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#id/textview2/>
<TextView
android:id="#+id/txtview1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#id/android:list"/>
</RelativeLayout>

Android UI Beginner question

I'm new to Android/java, coming from C#/Visual Studio.
It's not a too hard jump from C# to java while coding the logic, but with the UI, I'm having problems.
I've now added a simple TextView to my main.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" android:orientation="vertical">
<TextView android:layout_width="wrap_content" android:editable="true" android:layout_height="wrap_content" android:id="#+id/textView1" android:text="TextView">
</TextView>
</LinearLayout>
Now I wanted to access the textView1 by code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.textView1 = (TextView)this.findViewById(android.R.id.textView1);
}
private TextView textView1;
but I get the error: textView1 cannot be resolved.
What am I doing wrong?
Thanks
James
Replace findViewById(android.R.id.textView1) by findViewById(R.id.textView1);
android.R is resorce packege of SDK while R is of your app which will create along with successfully build of app .
and set the background color to #ffffff in xml .... it seems more attractive..
Instead of android.R.id.textView1, just use R.id.textView1. The android prefix is for resources from Android itself, rather than from your project.
Replace findViewById(android.R.id.textView1) by findViewById(R.id.textView1);
android.R is resorce packege of SDK while R is of your app which will create along with successfully build of app .
try this
private TextView textView1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.textView1 = (TextView)this.findViewById(R.id.textView1);
textView1.setText("Hello World !!!");
}

Categories

Resources