Pergunta

Simple timepicker function is failing with Fatal Exception - NullPointerException and java.lang.reflect.InvocationTargetException on following line :

int hour = time_picker.getCurrentHour();

Fragment_main.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: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="com.example.calapp1.MainActivity$PlaceholderFragment" >

<TimePicker
    android:id="@+id/timePicker1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="80dp" />

<TextClock
    android:id="@+id/textClock1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="TextClock" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="60dp"
    android:text="Show Time"
    android:onClick="setTime"
     />

MainActivity.java :

public class MainActivity extends Activity {

TimePicker time_picker;

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

    time_picker = (TimePicker) findViewById(R.id.timePicker1);

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

}

public void setTime(View v)
{

    int hour = time_picker.getCurrentHour();
    int minute = time_picker.getCurrentMinute();

    try{
    Toast.makeText(getBaseContext(), "select time is "+hour, Toast.LENGTH_LONG).show();
    }
    catch(Exception e){
        e.printStackTrace();
        Toast.makeText(getBaseContext(), "problem", Toast.LENGTH_LONG).show();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

}

Can you please assist?

Thank you, Yogesh.

Foi útil?

Solução

You timepicker is in the fragment layout and not in the activity layout. Activity onCreate() is too early to find it in the activity view hierarchy.

Move the findViewById() to fragment onCreateView() and change it to rootView.findViewById().

Outras dicas

Please be noted that you have your timepicker in Fragment_main.xml and you're trying it in the onCreate where the contentview is set as setContentView(R.layout.activity_main); so please move the line to onCreateView() and use rootView.findViewById(). That will srely solve the issue.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top