应用程序挂起,无法正常运行,没有明显的软件错误
试图调用虚拟方法-android.view.Window $ Callback android.view.Window.getCallback()-空对象引用
我的MainActivity.java
import android.app.ProgressDialog;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.ghazalitodo.data.AlarmReminderContract;
import com.example.ghazalitodo.data.AlarmReminderDbHelper;
import static com.example.ghazalitodo.R.*;
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private FloatingActionButton mAddReminderButton = (FloatingActionButton) findViewById(R.id.fab);
private Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
AlarmCursorAdapter mCursorAdapter;
AlarmReminderDbHelper alarmReminderDbHelper = new AlarmReminderDbHelper(this);
ListView reminderListView = (ListView) findViewById(R.id.list);
ProgressDialog prgDialog;
TextView reminderText = (TextView) findViewById(R.id.reminderText);
private String alarmTitle = "";
private static final int VEHICLE_LOADER = 0;
public MainActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
setSupportActionBar(mToolbar);
mToolbar.setTitle(string.app_name);
View emptyView = findViewById(R.id.empty_view);
reminderListView.setEmptyView(emptyView);
mCursorAdapter = new AlarmCursorAdapter(this, null);
reminderListView.setAdapter(mCursorAdapter);
reminderListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, AddReminderActivity.class);
Uri currentVehicleUri = ContentUris.withAppendedId(AlarmReminderContract.AlarmReminderEntry.CONTENT_URI, id);
// Set the URI on the data field of the intent
intent.setData(currentVehicleUri);
startActivity(intent);
}
});
mAddReminderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent intent = new Intent(v.getContext(), AddReminderActivity.class);
//startActivity(intent);
addReminderTitle();
}
});
getSupportLoaderManager().initLoader(VEHICLE_LOADER, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
String[] projection = {
AlarmReminderContract.AlarmReminderEntry._ID,
AlarmReminderContract.AlarmReminderEntry.KEY_TITLE,
AlarmReminderContract.AlarmReminderEntry.KEY_DATE,
AlarmReminderContract.AlarmReminderEntry.KEY_TIME,
AlarmReminderContract.AlarmReminderEntry.KEY_REPEAT,
AlarmReminderContract.AlarmReminderEntry.KEY_REPEAT_NO,
AlarmReminderContract.AlarmReminderEntry.KEY_REPEAT_TYPE,
AlarmReminderContract.AlarmReminderEntry.KEY_ACTIVE
};
return new CursorLoader(this, // Parent activity context
AlarmReminderContract.AlarmReminderEntry.CONTENT_URI, // Provider content URI to query
projection, // Columns to include in the resulting Cursor
null, // No selection clause
null, // No selection arguments
null); // Default sort order
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
mCursorAdapter.swapCursor(cursor);
if (cursor.getCount() > 0){
reminderText.setVisibility(View.VISIBLE);
}else{
reminderText.setVisibility(View.INVISIBLE);
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mCursorAdapter.swapCursor(null);
}
public void addReminderTitle(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Set Reminder Title");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (input.getText().toString().isEmpty()){
return;
}
alarmTitle = input.getText().toString();
ContentValues values = new ContentValues();
values.put(AlarmReminderContract.AlarmReminderEntry.KEY_TITLE, alarmTitle);
Uri newUri = getContentResolver().insert(AlarmReminderContract.AlarmReminderEntry.CONTENT_URI, values);
restartLoader();
if (newUri == null) {
Toast.makeText(getApplicationContext(), "Setting Reminder Title failed", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Title set successfully", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
public void restartLoader(){
getSupportLoaderManager().restartLoader(VEHICLE_LOADER, null, this);
}
} ```
My Logcat error
```2019-07-07 05:16:58.919 4549-4549/com.delaroystudios.alarmreminder E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.delaroystudios.alarmreminder, PID: 4549
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.delaroystudios.alarmreminder/com.example.ghazalitodo.MainActivity}: 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:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
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>(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.ghazalitodo.MainActivity.<init>(MainActivity.java:39)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
答案 0 :(得分:0)
您需要在调用onCreate
之后初始化setContentview
方法中的组件。这是因为活动只能在设置相应的布局后才能找到视图。因此,您需要在findviewbyId
之后调用所有setContentView
方法。
根据docs,
onCreate(Bundle)是您初始化活动的地方。最重要的是,这里通常会使用定义您的UI的布局资源来调用setContentView(int),并使用findViewById(int)来检索该UI中需要以编程方式进行交互的小部件。
此外,您无需从API 26开始显式转换视图。您可以从here
中找到更多内容。
private FloatingActionButton mAddReminderButton;
private Toolbar mToolbar;
AlarmCursorAdapter mCursorAdapter;
AlarmReminderDbHelper alarmReminderDbHelper;
ListView reminderListView;
ProgressDialog prgDialog;
TextView reminderText;
private String alarmTitle = "";
private static final int VEHICLE_LOADER = 0;
public MainActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
mAddReminderButton = findViewById(R.id.fab);
mToolbar = findViewById(R.id.toolbar);
reminderListView = findViewById(R.id.list);
reminderText = findViewById(R.id.reminderText);
alarmReminderDbHelper = new AlarmReminderDbHelper(this);
...