Мое приложение продолжает зависать на линии startActivity
. Я использую mastedetail / фрагменты здесь. Я также попытался использовать getactivity()
вместо контекста. и getactivity().finish
, но это тоже не сработало, оно даже не доходит до следующего класса, в котором происходит сбой startactivity(intent)
.
Хотя строка намерений указывает на файл reserve.java.
if (mItem.id.equals("3")) {
rootView = inflater.inflate(R.layout.reserve, container, false);
//((WebView)rootView.findViewById(R.id.item_detail)).loadUrl(mItem.item_url);
context = rootView.getContext(); // Assign your rootView to context
Button btDate = (Button) rootView.findViewById(R.id.btnDate);
btDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
//Pass the context and the Activity class you need to open from the Fragment Class, to the Intent
Intent intent = new Intent(context, reserve.class);
startActivity(intent);
// startActivity(new Intent(context, reserve.class));
}
});
}
return rootView;
reserve.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow android:layout_width="wrap_content">
<View android:layout_height="60dp" />
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:background="#b0c4de"
android:gravity="fill_horizontal"
android:padding="50dp"
android:text="@string/txtTitle"
android:textSize="50sp"
android:typeface="serif" />
<TextView />
</TableRow>
<TableRow>
<View android:layout_height="50dp" />
<Button
android:id="@+id/btnDate"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:padding="20dp"
android:text="@string/btnBig"
android:textSize="36sp" />
</TableRow>
<TableRow>
<View android:layout_height="60dp" />
<TextView
android:id="@+id/txtReservation"
android:layout_gravity="center"
android:padding="20dp"
android:textSize="36sp" />
</TableRow>
</TableLayout>
</LinearLayout>
Reserve.java:
public class reserve extends AppCompatActivity {
TextView reservations = (TextView) findViewById(R.id.txtReservation);
Button btDate = (Button) findViewById(R.id.btnDate);
Calendar c = Calendar.getInstance();
DateFormat fntDate = DateFormat.getDateInstance();
DatePickerDialog.OnDateSetListener d = new
DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int
monthOfYear, int dayOfMonth) {
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, monthOfYear);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
reservations.setText("Your Reservation is set for" +
fntDate.format(c.getTime()));
}
};
public View onCreateView(Bundle savedInstanceState) {
btDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
new DatePickerDialog(reserve.this,
d,c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH)).show();
}
});
return reservations;
}
}
Ошибка после срабатывания старта, вылетает в reserve.java на этой строке
TextView reservations = (TextView) findViewById(R.id.txtReservation);
журнал ошибок для этого сбоя:
FATAL EXCEPTION: main
Process: com.example.gg.classicalindianmusictickets, PID:20130
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo
{
com.example.gg.classicalindianmusictickets/
com.example.gg.classicalindianmusictickets.reserve}:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.view.Window$Callback android.view.Window.getCallback()' on a null
object reference
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2820)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)
at
android.app.servertransaction.LaunchActivityItem.execute(
LaunchActivityItem.java:78)
at android.app.servertransaction.
TransactionExecutor.executeCallbacks(TransactionExecutor.java:101)
at android.app.servertransaction.
TransactionExecutor.execute(TransactionExecutor.java:73)
at android.app.ActivityThread$H.handleMessage(
ActivityThread.java:1786)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main
(ActivityThread.java:6656)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run
(RuntimeInit.java:438)
at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.NullPointerException: Attempt to invoke
virtual method 'android.view.Window$Callback
android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init
(AppCompatDelegateImplBase.java:117)
at android.support.v7.app.AppCompatDelegateImplV9.<init>
обновлен Reserve.java
public class reserve extends AppCompatActivity {
public View onCreateView(Bundle savedInstanceState) {
final TextView reservations = (TextView) findViewById(R.id.txtReservation);
Button btDate = (Button) findViewById(R.id.btnDate);
final Calendar c = Calendar.getInstance();
final DateFormat fntDate = DateFormat.getDateInstance();
final DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, monthOfYear);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
reservations.setText("Your Reservation is set for" + fntDate.format(c.getTime()));
}
};
btDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
new DatePickerDialog(reserve.this, d,c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
}
});
return reservations;
}
}