【发布时间】:2015-03-26 17:16:25
【问题描述】:
我已经尝试了所有可能的解决方案,但仍然出现此错误。我知道我必须拥有所有正确的小写 xml 文件,但我仍然无法识别它们。我也尝试过删除android.R 并导入my.package.r 等。但没有任何效果。
这里是给出错误的两个 .java 文件;
第 18、25、26、27、28 行给出的错误。
package androidChat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class addassignment extends Activity {
DBAdapter db = new DBAdapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
}
public void addAssignment(View v) {
Log.d("test", "adding");
// get data from form
EditText nameTxt = (EditText) findViewById(R.id.editTitle);
EditText dateTxt = (EditText) findViewById(R.id.editDuedate);
EditText courseTxt = (EditText) findViewById(R.id.editCourse);
EditText notesTxt = (EditText) findViewById(R.id.editNotes);
db.open();
long id = db.insertRecord(nameTxt.getText().toString(), dateTxt
.getText().toString(), courseTxt.getText().toString(), notesTxt
.getText().toString());
db.close();
nameTxt.setText("");
dateTxt.setText("");
courseTxt.setText("");
notesTxt.setText("");
Toast.makeText(addassignment.this, "Assignment Added",
Toast.LENGTH_LONG).show();
}
public void viewAssignments(View v) {
Intent i = new Intent(this, AssignmentTracker.class);
startActivity(i);
}
}
并且; 第 31 行和第 33 行给出的错误。
package androidChat;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Toast;
public class AssignmentTracker extends Activity {
/** Called when the activity is first created. */
// DBAdapter db = new DBAdapter(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button addBtn = (Button) findViewById(R.id.add);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(AssignmentTracker.this,
addassignment.class);
startActivity(i);
}
});
try {
String destPath = "/data/data/" + getPackageName()
+ "/databases/AssignmentDB";
File f = new File(destPath);
if (!f.exists()) {
CopyDB(getBaseContext().getAssets().open("mydb"),
new FileOutputStream(destPath));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// DBAdapter db = new DBAdapter(this);
// ---add an assignment---
/*
* db.open(); long id = db.insertRecord("Hello World", "2/18/2012",
* "DPR 224", "First Android Project"); id =
* db.insertRecord("Workbook Exercises", "3/1/2012", "MAT 100",
* "Do odd numbers"); db.close();
*/
// ---get all Records---
/*
* db.open(); Cursor c = db.getAllRecords(); if (c.moveToFirst()) { do {
* DisplayRecord(c); } while (c.moveToNext()); } db.close();
*/
/*
* //---get a Record--- db.open(); Cursor c = db.getRecord(2); if
* (c.moveToFirst()) DisplayRecord(c); else Toast.makeText(this,
* "No Assignments found", Toast.LENGTH_LONG).show(); db.close();
*/
// ---update Record---
/*
* db.open(); if (db.updateRecord(1, "Hello Android", "2/19/2012",
* "DPR 224", "First Android Project")) Toast.makeText(this,
* "Update successful.", Toast.LENGTH_LONG).show(); else
* Toast.makeText(this, "Update failed.", Toast.LENGTH_LONG).show();
* db.close();
*/
/*
* //---delete a Record--- db.open(); if (db.deleteRecord(1))
* Toast.makeText(this, "Delete successful.", Toast.LENGTH_LONG).show();
* else Toast.makeText(this, "Delete failed.",
* Toast.LENGTH_LONG).show(); db.close();
*/
}
private class DBAdapter extends BaseAdapter {
private LayoutInflater mInflater;
// private ArrayList<>
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
return null;
}
}
public void CopyDB(InputStream inputStream, OutputStream outputStream)
throws IOException {
// ---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
public void DisplayRecord(Cursor c) {
Toast.makeText(
this,
"id: " + c.getString(0) + "\n" + "Title: " + c.getString(1)
+ "\n" + "Due Date: " + c.getString(2),
Toast.LENGTH_SHORT).show();
}
public void addAssignment(View view) {
Intent i = new Intent("com.pinchtapzoom.addassignment");
startActivity(i);
Log.d("TAG", "Clicked");
}
}
这是我的 AndroidManifest.xml;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.UTEP.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:allowTaskReparenting="false"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name="androidChat.AndroidChatApplicationActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="androidChat.AssignmentManager" >
</activity>
<activity
android:name=".AssignmentTracker"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".addassignment" >
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
我的 res/layout 文件如下;
add.xml
assignment_item.xml
assignmentmanager.xml
main.xml
【问题讨论】:
-
当 xml 文件存在语法错误时,不会生成 R.java。你能检查一下,看看这就是问题所在吗?
-
R.java 已生成,并且没有任何 xml 文件出现错误。
-
您是否在 xml 中使用了一些资源,例如图像,而错过了添加到 drawable 中?
-
我没有使用任何图像,虽然我注意到AndroidManifest.xml中的包名与包含java文件的包名不同,这可能是问题吗?
-
这绝对是个问题。但这不应该是 R.java 没有得到解决的原因。将 java 文件中的包名与 Manifest.xml 中的包名对齐。好像你已经做了一些类的复制粘贴。