I am new in android development and have spent couple of hours on this. (eclipse + adt) Here are some extra words so i can post this. Getting Null pointer exception while calling run_time.setText("t");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_run);
TextView run_time = (TextView) findViewById(R.id.run_time);
try {
run_time.setText("t");
}catch(Exception e) {
Log.i("Log", e.getMessage()+"Error!"); // LogCat message
}
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
layout activity_start_run.xml
<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" >
<TextView
android:id="@+id/run_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ghjghjghjghjghjghj" />
</LinearLayout>
This Null Pointer Exception is because your resource is not loaded properly try to clean project and run again will resolve.
R.id.run_time this is loaded from R.java which is auto generated file. Clean and rebuilt project will fix this.
MainActivity.java
package com.example.stacktest;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView run_time = (TextView) findViewById(R.id.run_time);
try {
run_time.setText("t");
}catch(Exception e) {
Log.i("Log", e.getMessage()+"Error!"); // LogCat message
}
}
}
and activity_main.xml
<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" >
<TextView
android:id="@+id/run_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ghjghjghjghjghjghj" />
</LinearLayout>