I am trying to build a small simple program with an image and a textview under it. Like a countdown. I’ve been trying to runt this program, and everything works except when I get to setText, the program crashes. What could be the problem? I have done a lot of searching and changing, but still get the same problem.
package com.ob.IsIt;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class Isit1337 extends Activity {
ImageView image;
TextView t;
String until;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
until = "fnkar inte";
t=(TextView)findViewById(R.layout.main);
run();
}
private void run()
{
if(IsIt.check())
{
image.setEnabled(false);
}
until = IsIt.timeLeft();
t.setText("test");
}
}
The IsIt works, the IsIt.check returns a boolean and the IsIt.timeLeft returns a String.
The xml code looks like this.
<?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:gravity="bottom|center|center_vertical|top"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="277dp"
android:layout_height="441dp"
android:src="@drawable/blackno" />
<TextView
android:id="@+id/t"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.46"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Thank you in advance!
I’m guessing that you’re getting a NullPointerException. (When asking this kind of question, it’s better to also paste the relevant LogCat output.) Change this line:
to this: