我想创建应用程序,其中来自B Activity的数据通过OnActivityResult方法放入A activity.A活动将此数据保存到sqlite并使用此数据创建新的textview。在OnCreate Activity中我想要显示所有创建的textview.But的方法当我运行应用程序时,我有错误。
06-01 19:16:25.133 643-670/? E/power﹕ Can not write CPU_PERF_LEVEL_LOCK. (Invalid argument)
06-01 19:16:25.233 14012-14012/com.example.user.campus E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:16:25.243 643-1443/? E/Surface﹕ Invalid SurfaceControl
06-01 19:16:25.263 14012-14012/com.example.user.campus E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.campus/com.example.user.campus.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5751)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.user.campus.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5165)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5751)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
at dalvik.system.NativeStart.main(Native Method)
06-01 19:16:25.273 643-669/? E/EmbeddedLogger﹕ App crashed! Process: com.example.user.campus
06-01 19:16:25.273 643-669/? E/EmbeddedLogger﹕ App crashed! Package: com.example.user.campus v1 (1.0)
06-01 19:16:25.273 643-669/? E/EmbeddedLogger﹕ Application Label: Campus
06-01 19:16:27.145 643-670/? E/power﹕ request perf lock level(0) is invalid. (Invalid argument)
06-01 19:16:29.908 14044-14044/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:19:02.661 14151-14151/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:23:42.489 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.509 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.519 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.529 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.539 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.549 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.750 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.770 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.780 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.790 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.800 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.910 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.950 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.990 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:43.010 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:43.020 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:43.080 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:43.120 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:43.170 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:43.190 1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:24:40.631 14259-14259/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:24:41.002 14285-14285/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:24:54.837 14322-14322/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:24:54.997 14338-14338/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:24:55.417 14350-14350/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:28:05.230 14412-14412/? E/Trace﹕ error opening trace file: No such file or directory (2)
MainActivity: 包com.example.user.campus;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.provider.CalendarContract;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
Button btnadd;
LinearLayout llMain;
DBHelper dbHelper;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
db = dbHelper.getWritableDatabase();
Cursor c;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
btnadd = (Button) findViewById(R.id.button);
btnadd.setOnClickListener(this);
dbHelper = new DBHelper(this);
c = db.query("mytable", null, null, null, null, null, null);
if (c.moveToFirst()) {
int idColIndex = c.getColumnIndex("id");
int nameColIndex = c.getColumnIndex("name");
int dateColIndex = c.getColumnIndex("date");
do {
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
TextView textView = new TextView(this);
textView.setText(c.getString(nameColIndex)+" "+c.getString(dateColIndex));
llMain.addView(textView,layoutParams);
} while (c.moveToNext());
} else
c.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
Intent intent = new Intent(this, result.class);
startActivityForResult(intent, 1);
break;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
if (data == null) {
return;
}
String name = data.getStringExtra("name");
String date = data.getStringExtra("date");
super.onActivityResult(requestCode, resultCode, data);
cv.put("name", name);
cv.put("date", date);
long rowID = db.insert("mytable", null, cv);
}
class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "myDB", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table mytable ("
+ "id integer primary key autoincrement,"
+ "name text,"
+ "date text" + ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
编辑:
public class edit extends ActionBarActivity implements View.OnClickListener{
EditText etName;
EditText etData;
Button btnEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
etName = (EditText) findViewById(R.id.editText3);
etData = (EditText) findViewById(R.id.editText4);
btnEdit = (Button) findViewById(R.id.button3);
btnEdit.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_edit, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("name", etName.getText().toString());
intent.putExtra("date",etData.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
}
答案 0 :(得分:0)
在这里查看您的代码:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
btnadd = (Button) findViewById(R.id.button);
btnadd.setOnClickListener(this);
dbHelper = new DBHelper(this);
db = dbHelper.getWritableDatabase();
Cursor c;
c = db.query("mytable", null, null, null, null, null, null);
在使用之前,您没有新的DBHelper()。 改为:
/**
* @OnlyCurrentDoc Limits the script to only accessing the current spreadsheet.
*/
var SIDEBAR_TITLE = 'Sidebar Musicbox';
/**
* Adds a custom menu with items to show the sidebar and dialog.
* @param {Object} e The event parameter for a simple onOpen trigger.
*/
function onOpen(e) {
SpreadsheetApp.getUi()
.createAddonMenu()
.addItem('Show sidebar', 'showSidebar')
.addToUi();
showSidebar();
}
/**
* Runs when the add-on is installed; calls onOpen() to ensure menu creation and
* any other initializion work is done immediately.
*
* @param {Object} e The event parameter for a simple onInstall trigger.
*/
function onInstall(e) {
onOpen(e);
}
/**
* Opens a sidebar. The sidebar structure is described in the Sidebar.html
* project file.
*/
function showSidebar() {
var ui = HtmlService.createTemplateFromFile('Sidebar')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle(SIDEBAR_TITLE);
SpreadsheetApp.getUi().showSidebar(ui);
}