R无法解析为变量(Android)

时间:2015-03-26 17:16:25

标签: java android xml

我已尝试过所有可能的解决方案,但我仍然遇到此错误。我知道我必须使用小写的所有正确的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

2 个答案:

答案 0 :(得分:0)

1)首先,尝试编译代码。有时错误“R无法解析为变量”只能通过第一次编译代码来解决。

2)可能是您的包名称存在问题。它显示package androidChat; 它应该类似于package com.example.exampleapp;

尝试发布剩余的xml文件。可能这可以帮助我解决你的问题。

更新:在您的第一个.java文件中,为什么不在onCreate()方法中初始化EditText。

答案 1 :(得分:0)

我发现了问题,我不得不更改包名称以匹配manifest.xml中的名称,它似乎可以立即解决问题。谢谢你的帮助。