i am beginner in android,and i write this code in ADT v22.6.2:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    //this is my code
    TextView tbx = (TextView)findViewById(R.id.tbxName);
    tbx.setText("H.H");
}

but when i launch program -> Unfortunately,app has stopped

Edit:

<FrameLayout 
xmlns:android="schemas.android.com/apk/res/android"; xmlns:tools="schemas.android.com/tools"; 
android:id="@+id/container" 
android:layout_width="match_parent"
android:layout_height="match_parent" 
tools:context="com.example.t.MainActivity" 
tools:ignore="MergeRootFrame" > 
<TextView 
android:id="@+id/tbx" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="" />
 \</FrameLayout>

please help me. thanks

有帮助吗?

解决方案

t is not initialized anywhere. You have only initialize tbx and also there is a possibility that TextView belongs to PlaceholderFragment.

Edit:

Form your comment you say you have tbx.setText("H.H");

Likely the textview belongs to fragment and you should initialize textview in PlaceholderFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    TextView tbx = (TextView)rootView.findViewById(R.id.tbxName);
    tbx.setText("H.H");
    return rootView;
}

Edit 2:

You problem has nothing to do with ADT v22.6.2. You are probably getting NullPointerException

You reference wrong id .

<TextView 
android:id="@+id/tbx" 

Change this

 TextView tbx = (TextView)findViewById(R.id.tbxName);

to

 TextView tbx = (TextView)findViewById(R.id.tbx);

其他提示

You have wrongly given the id of your TextView.

Just change your TextView id from R.id.tbxName to R.id.tbx as its given in your layout.

Instead of

TextView tbx = (TextView)findViewById(R.id.tbxName);

Change it

  TextView tbx = (TextView)findViewById(R.id.tbx);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top